2010-10-23 52 views
0

我構建了流行雲但它無法正常工作。 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() 
+3

如果你的問題是這樣緊急,爲什麼不花時間正確格式化,以便它可讀?那麼也許有人可能會幫助你。 – 2010-10-23 13:33:28

+4

你也沒有說過它做錯了什麼。 – 2010-10-23 13:47:23

+1

他的代碼來自http://sujitpal.blogspot.com/2007/04/building-tag-cloud-with-python.html,如果有人關心它是如何工作的。 – Mark 2010-10-23 13:51:17

回答