python流行度云不起作用
我建立了一个人气云,但它无法正常工作。 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
.....
在我的人气云中,名字写的是它们出现的频率。例如,《探索者传奇》被写入了 5 次,并且它们的大小也增加了。每个单词都应该写一次,并且大小必须根据流行度数字(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()
I built a popularity cloud but it doesn't work properly. The txt file is;
1 Top Gear
3 Scrubs
3 The Office (US)
5 Heroes
5 How I Met Your Mother
5 Legend of the Seeker
5 Scrubs
.....
In my popularity cloud, names are written their frequency times. For example, Legend of the Seeker is written 5 times and their size increases. Every word is supposed to be written one time and the size must be according to popularity number (5). But every word should be written one time and its size must be according to its popularity. How can I fix it?
And also my program should provide that condition:
Terms with the same frequency are typically displayed in the same colour e.g. Golf and Karate. Different frequencies are typically shown in different colours e.g. Basketball, Cricket and Hockey. At the bottom of each cloud output the frequency/count in the colour used to display the values in the cloud.
My code follows here.
#!/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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否可以按颜色分类,但只需要 4 行代码就可以在运行代码的目录中生成标签云。
https://github.com/atizo/PyTagCloud
I'm not sure this can categorize by color, but it only takes 4 lines of code to generate a tag cloud in the directory you run the code in.
https://github.com/atizo/PyTagCloud