forked from minimaxir/stylistic-word-clouds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcloud_yelp_neg.py
executable file
·37 lines (30 loc) · 1.26 KB
/
wordcloud_yelp_neg.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
import numpy as np
import csv
import random
from PIL import Image
from wordcloud import WordCloud, STOPWORDS
from palettable.colorbrewer.sequential import Reds_9
def color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return tuple(Reds_9.colors[random.randint(2,8)])
csv_path = "yelp_words_by_stars_1gram.csv"
fa_path = "/Users/maxwoolf/Downloads/exported2048/"
font_path = "/Users/maxwoolf/Fonts/OpenSans-CondBold.ttf"
icon = "thumbs-down"
words_array = []
with open(csv_path, 'rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['stars'] is '1' and row['word'] not in STOPWORDS:
words_array.append((row['word'].upper(), float(row['count'])))
# http://stackoverflow.com/questions/7911451/pil-convert-png-or-gif-with-transparency-to-jpg-without
icon_path = fa_path + "%s.png" % icon
icon = Image.open(icon_path)
mask = Image.new("RGB", icon.size, (255,255,255))
mask.paste(icon,icon)
mask = np.array(mask)
wc = WordCloud(font_path=font_path, background_color="white", max_words=2000, mask=mask,
max_font_size=300, random_state=42)
# generate word cloud
wc.generate_from_frequencies(words_array)
wc.recolor(color_func=color_func, random_state=3)
wc.to_file("yelp_neg_wordcloud.png")