-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordCloud.py
46 lines (35 loc) · 1.26 KB
/
wordCloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python
from operator import itemgetter
import re
import cgi
form = cgi.FieldStorage()
inputListOfWords = form.getfirst('wordCloudText', 'empty')
inputListOfWords = (cgi.escape(inputListOfWords)).lower()
wordDictionary = {}
listOfWordsToIgnore = ['he', 'so', 'to', 'no', 'of', 'we', 'in', 'an', 'she', 'on', 'not', 'his', 'as', 'you', 'the', 'mr', 'me', 'if', 'be', 'or', 'him', 'and', 'her', 'at', 'is']
def buildDictionary(wordList, ignoreList):
p = re.compile('[a-zA-Z0-9]+')
tmpDict = {}
wordStringArray = p.findall(wordList)
for item in wordStringArray:
if (item not in ignoreList):
if(tmpDict.has_key(item)):
newValue = tmpDict[item] + 1
tmpDict[item] = newValue
else:
tmpDict.update({item:1})
return tmpDict
#--- RUNABLE ---#
wordDictionary = buildDictionary(inputListOfWords, listOfWordsToIgnore)
print """\
Content-Type: text/html\n
<html><body>
"""
print """\
<p>The submitted text gives this list of words:</p>
"""
for key, value in sorted(wordDictionary.iteritems(), key=lambda (k,v): (v,k), reverse=True):
print "Key: %s > Value: %s <br />\n" % (key, value)
print """\
</body></html>
"""