-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathword-cloud.py
82 lines (70 loc) · 2.42 KB
/
word-cloud.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""
Created on Mon May 4 20:57:46 2015
@author: elliott
"""
from wordcloud import WordCloud
import pandas as pd
import os
import numpy as np
os.chdir('...')
# parameters
MIN_DOCFREQ = 50 # minimum document frequency to be included
ONLY_PHRASES = True # keep true if dont include single words
# load data
tstats = pd.read_pickle('tstats.pkl')
id2word = pd.read_pickle('id2word.pkl')
P = len(id2word)
words = list(['']*P)
for i,w in id2word.items():
words[i] = w
# optional: filter on document frequency
docfreqs = pd.read_pickle('docfreqs.pkl')
freqvector = [0] * P
for k, v in docfreqs.items():
freqvector[k] = v
freqvector = np.array(freqvector)
keep = freqvector > 1000
tstats = tstats[keep]
words = [words[i] for i in range(P) if keep[i]]
# split up positive-effect and negative-effect words
pos = tstats > 0
neg = tstats < 0
tpos = tstats[pos]
wordpos = words[pos]
tneg = np.abs(tstats[neg]) # reverse sign for negative effect words
wordneg = words[neg]
maincol = np.random.randint(0,360) # this is the "main" color
def colorfunc(word=None, font_size=None, position=None,
orientation=None, font_path=None, random_state=None):
color = np.random.randint(maincol-10, maincol+10)
if color < 0:
color = 360 + color
return "hsl(%d, %d%%, %d%%)" % (color,np.random.randint(65, 75)+font_size / 7, np.random.randint(35, 45)-font_size / 10)
# build scores tuples
# positive effect words
scores = list(zip(tpos,wordpos))
scores = [s for s in scores if np.isfinite(s[0])]
if ONLY_PHRASES:
scores = [s for s in scores if '_' in s[1]]
scores.sort()
scores.reverse()
scores = [(b,np.log(a)) for (a,b) in scores]
print(scores[:10])
wordcloud = WordCloud(background_color="white", ranks_only=False,max_font_size=100,
color_func=colorfunc,
height=600,width=1000).generate_from_frequencies(scores[:100])
wordcloud.to_file('pos-words.png')
# negative effect words
scores = list(zip(tneg,wordneg))
scores = [s for s in scores if np.isfinite(s[0])]
if ONLY_PHRASES:
scores = [s for s in scores if '_' in s[1]]
scores.sort()
scores.reverse()
scores = [(b,np.log(a)) for (a,b) in scores]
print(scores[:10])
wordcloud = WordCloud(background_color="white", ranks_only=False,max_font_size=100,
color_func=colorfunc,
height=600,width=1000).generate_from_frequencies(scores[:100])
wordcloud.to_file('neg-words.png')