-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcloudgen.py
64 lines (53 loc) · 2.04 KB
/
wordcloudgen.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/python
import csv, re, string
import pickle
from wordcloud import WordCloud, STOPWORDS
def main():
data = {}
data['questions'] = []
data['stakeholders'] = []
data['wordclouds'] = {}
stopwords = STOPWORDS.copy()
#Filter out standalone words 2 letters or shorter
shortword = re.compile(r'\W*\b\w{1,2}\b')
with open('alldata.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
stakeholder = row['Code']
data['stakeholders'].append(stakeholder)
data[stakeholder] = {}
data[stakeholder]['alltext'] = ''
for key in row:
if key != 'Code':
question = key
if question not in data['questions']:
data['questions'].append(question)
data[question] = ''
response = shortword.sub('', row[key].lower().translate(None,string.punctuation))
data[stakeholder][question] = response
data[stakeholder]['alltext'] += response
data[stakeholder]['alltext'] += ' '
data[question] += response
data[question] += ' '
#Generate word clouds:
for question in sorted(data['questions']):
if question is not '5a':
#Number of words per question
# print question, ':', len(data[question].split())
try:
data['wordclouds'][question] = WordCloud(stopwords=stopwords).generate(data[question])
except:
print question
for stakeholder in data['stakeholders']:
try:
data['wordclouds'][stakeholder] = WordCloud(stopwords=stopwords).generate(data[stakeholder]['alltext'])
except:
print stakeholder
pickle.dump(data, open('alldata.pickle', 'wb'))
# Display the generated image:
# the matplotlib way:
# import matplotlib.pyplot as plt
# plt.imshow(wordcloud)
# plt.axis("off")
if __name__ == "__main__":
main()