-
Notifications
You must be signed in to change notification settings - Fork 72
/
weibo_cloud.py
51 lines (37 loc) · 1.13 KB
/
weibo_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
# coding: utf-8
import json
import jieba.analyse
import matplotlib as mpl
from scipy.misc import imread
from wordcloud import WordCloud
# mpl.use('TkAgg')
import matplotlib.pyplot as plt
def keywords(mblogs):
text = []
for blog in mblogs:
keyword = jieba.analyse.extract_tags(blog['text'])
text.extend(keyword)
return text
def gen_img(texts, img_file):
data = ' '.join(text for text in texts)
image_coloring = imread(img_file)
wc = WordCloud(
background_color='white',
mask=image_coloring,
font_path='/Library/Fonts/STHeiti Light.ttc'
)
wc.generate(data)
# plt.figure()
# plt.imshow(wc, interpolation="bilinear")
# plt.axis("off")
# plt.show()
wc.to_file(img_file.split('.')[0] + '_wc.png')
if __name__ == '__main__':
keyword = '微软'
mblogs = json.loads(open('result_{}.json'.format(keyword), 'r', encoding='utf-8').read())
print('微博总数:', len(mblogs))
words = []
for blog in mblogs:
words.extend(jieba.analyse.extract_tags(blog['text']))
print("总词数:", len(words))
gen_img(words, 'edge.png')