##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = Python实现标签云 = 来源:http://sujitpal.blogspot.com/2007/04/building-tag-cloud-with-python.html {{{ #!python # !/usr/bin/env python # -*- coding: utf-8 -*- # !/usr/bin/env python # -*- coding: utf-8 -*- import string def main(): # get the list of tags and their frequency from input file taglist = getTagListSortedByFrequency('/tmp/tag.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, 'tags.html') def getTagListSortedByFrequency(inputfile): inputf = open(inputfile, 'r') taglist = [] for line in inputf: #原文中使用readlines,改为直接迭代文件对象,更pythonic line = line[:-1] (tag, count) = line.split('|') 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) index = index + distrib ranges.append(range) return ranges def writeCloud(taglist, ranges, outputfile): outputf = open(outputfile, 'w') outputf.write("\n") rangeStyle = ['smallesTag', '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%3yoursiteurl' if tag[1] >= range[0] and tag[1] <= range[1]: outputf.write('' + tag[0] + '') break rangeIndex = rangeIndex + 1 outputf.close() if __name__ == '__main__': main() }}}