Problem Based Learning

 

Python Tag Cloud

Page history last edited by dorai@... 1 yr ago
def makeTagCloud(counts):
    """Using the counts for each of the tags, write a simple HTML page to
    standard output containing a tag cloud representation. The CSS
    describes ten levels, each of which has differing font-size's,
    line-height's and font-weight's.

    Note that the dictionary keys are sorted before the tag cloud is
    generated.

    Arguments:
    - counts: dictionary of tags and their counts.
    """
    fontSize = [12, 16, 20, 24, 30, 36, 48, 64, 96, 96]
    

    print ""
    print ""
    print "Rich Burridge's Blog's Tag Cloud"
    print ""
    print ""
    print ""

    keys = counts.keys()
    keys.sort()
    for i in range(0, len(keys)):
        tag = keys[i]
        count = counts[tag]
        if count < 1:
            print " %s " % tag
        elif count < 2:
            print " %s " % tag
        elif count < 3:
            print " %s " % tag
        elif count < 4:
            print " %s " % tag
        elif count < 5:
            print " %s " % tag
        elif count < 10:
            print " %s " % tag
        elif count < 20:
            print " %s " % tag
        elif count < 30:
            print " %s " % tag
        elif count < 50:
            print " %s " % tag
        else:
            print " %s " % tag

    print ""
    print ""


def main():
   
	test ={
		"Contact": 4,
		"Capabilities": 4,
		"Projects": 4,
		"Products": 4,
		"Home": 4,
		"Inc": 3,
		"web": 2,
		"knowledge": 2,
		"Alliance": 2,
		"Bootstrap": 2,
		"organizations": 2,
		"built": 2,
		"solutions": 2,
		"Web": 2,
		"iMorph": 2,
		"counters": 1,
		"page": 1,
		"free": 1,
		"2004": 1,
		"1998": 1,
		"c": 1,
		"repositories": 1,
		"dynamic": 1,
		"build": 1,
		"track": 1,
		"helps": 1,
		"InfoMinder": 1,
		"product": 1,
		"information": 1,
		"source": 1,
		"rich": 1,
		"becoming": 1,
		"rapidly": 1,
		"Wide": 1,
		"World": 1,
		"member": 1,
		"its": 1,
		"partners": 1,
		"use": 1,
		"navigator": 1,
		"generation": 1,
		"next": 1,
		"HyperScope": 1,
		"also": 1,
		"addition": 1,
		"OpenCourse": 1,
		"Ameritech": 1,
		"like": 1,
		"collaboratories": 1,
		"several": 1,
		"year": 1,
		"last": 1,
		"Over": 1,
		"Engelbart": 1,
		"Douglas": 1,
		"Dr": 1,
		"vision": 1,
		"inspired": 1,
		"work": 1,
		"components": 1,
		"collaborative": 1,
		"building": 1,
		"specialize": 1,
		"teams": 1,
		"effectiveness": 1,
		"augment": 1,
		"necessary": 1,
		"tools": 1,
		"provide": 1,
		"goal": 1,
		"Imorph": 1,
		"Welcome": 1,
		"delivered": 1,
		"XML": 1}
	makeTagCloud(test)

if __name__ == "__main__":
    main()

Comments (1)

profile picture

dorai@... said

at 12:05 pm on Feb 12, 2008

I am not particularly happy with the code. Of course, it is not very pythonic. But it works. So it will stay here till we do something better.

Several improvements are possible. I will add a link to possible improvements page.

You don't have permission to comment on this page.