This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
/
makeWordCloud.py
59 lines (43 loc) · 1.58 KB
/
makeWordCloud.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
import matplotlib.pyplot as plt
import random
import re
import sys
from scipy.misc import imread
from cleanTweets import clean_text
from wordcloud import WordCloud, STOPWORDS
def generate_wordcloud(text):
def my_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
'''
To change colors change the range for random ints below:
Hue values are between 0 and 360
Follows rainbow:
Red Orange Yellow Green Blue Indigo Violet
0 50 100 150 200 250 300 360
'''
hue_lower = 0
hue_upper = 150
saturation = 500
light_lower = 80
light_upper = 120
return "hsl(%d, %d%%, %d%%)" % (random.randint(hue_lower, hue_upper), saturation, \
random.randint(light_lower, light_upper))
stopwords = STOPWORDS.copy()
stopwords.add("us")
stopwords.add("one")
stopwords.add("will")
stopwords.add("u")
rand_num = random.randint(1,100)
wc = WordCloud(max_words=100, stopwords=stopwords, margin=10, random_state=rand_num,\
width=2000, height=1200).generate(text)
fig = plt.figure(figsize=(32, 20), dpi=100)
plt.imshow(wc.recolor(color_func=my_color_func, random_state=1))
# Save image
outfilename = "tmp.png"
wc.to_file(outfilename)
plt.axis("off")
plt.show()
if __name__ == "__main__":
with open("tweets.txt", 'rb') as f:
tweet_list = f.readlines()
clean_tweets = clean_text(tweet_list)
generate_wordcloud(clean_tweets)