ポピュラー クラウドを構築しましたが、正しく動作しません。txtファイルは次のとおりです。
1 Top Gear
3 Scrubs
3 The Office (US)
5 Heroes
5 How I Met Your Mother
5 Legend of the Seeker
5 Scrubs
.....
私のポピュラークラウドでは、名前は頻度の時間を書いています。たとえば、Legend of the Seeker は 5 回書かれ、サイズが大きくなります。すべての単語は 1 回書かれ、サイズは人気番号 (5) に従っている必要があります。しかし、すべての単語は一度に書かれるべきであり、そのサイズはその人気に応じたものでなければなりません. どうすれば修正できますか?
また、私のプログラムはその条件を提供する必要があります:
通常、同じ頻度の用語は、ゴルフと空手など、同じ色で表示されます。異なる周波数は通常、バスケットボール、クリケット、ホッケーなどの異なる色で表示されます。各雲の下部には、雲の値を表示するために使用される色の頻度/カウントが出力されます。
私のコードはここに従います。
#!/usr/bin/python
import string
def main():
# get the list of tags and their frequency from input file
taglist = getTagListSortedByFrequency('tv.txt')
# find max and min frequency
ranges = getRanges(taglist)
# write out results to output, tags are written out alphabetically
# with size indicating the relative frequency of their occurence
writeCloud(taglist, ranges, 'tv.html')
def getTagListSortedByFrequency(inputfile):
inputf = open(inputfile, 'r')
taglist = []
while (True):
line = inputf.readline()[:-1]
if (line == ''):
break
(count, tag) = line.split(None, 1)
taglist.append((tag, int(count)))
inputf.close()
# sort tagdict by count
taglist.sort(lambda x, y: cmp(x[1], y[1]))
return taglist
def getRanges(taglist):
mincount = taglist[0][1]
maxcount = taglist[len(taglist) - 1][1]
distrib = (maxcount - mincount) / 4;
index = mincount
ranges = []
while (index <= maxcount):
range = (index, index + distrib-1)
index = index + distrib
ranges.append(range)
return ranges
def writeCloud(taglist, ranges, outputfile):
outputf = open(outputfile, 'w')
outputf.write("<style type=\"text/css\">\n")
outputf.write(".smallestTag {font-size: xx-small;}\n")
outputf.write(".smallTag {font-size: small;}\n")
outputf.write(".mediumTag {font-size: medium;}\n")
outputf.write(".largeTag {font-size: large;}\n")
outputf.write(".largestTag {font-size: xx-large;}\n")
outputf.write("</style>\n")
rangeStyle = ["smallestTag", "smallTag", "mediumTag", "largeTag", "largestTag"]
# resort the tags alphabetically
taglist.sort(lambda x, y: cmp(x[0], y[0]))
for tag in taglist:
rangeIndex = 0
for range in ranges:
url = "http://www.google.com/search?q=" + tag[0].replace(' ', '+') + "+site%3Asujitpal.blogspot.com"
if (tag[1] >= range[0] and tag[1] <= range[1]):
outputf.write("<span class=\"" + rangeStyle[rangeIndex] + "\"><a href=\"" + url + "\">" + tag[0] + "</a></span> ")
break
rangeIndex = rangeIndex + 1
outputf.close()
if __name__ == "__main__":
main()