-
Notifications
You must be signed in to change notification settings - Fork 7
/
Weibo_wordCloud.py
31 lines (29 loc) · 1.35 KB
/
Weibo_wordCloud.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
# -*- coding: utf-8 -*-
'''
@author: Yalei Meng E-mail: [email protected]
@license: (C) Copyright 2017, HUST Corporation Limited.
@desc:
@DateTime: Created on 2017/10/7,at 13:53 '''
import cv2
import jieba
from wordcloud import WordCloud,ImageColorGenerator
stop = set()
with open('./weibo1010.txt', 'r',encoding='utf-8') as f, open('./stopwords.txt', 'r',encoding='utf-8') as s:
text = f.read()
for line in s.readlines():
if line[:-1] not in stop:
stop.add(line[:-1])
# 首先使用 jieba 中文分词工具进行分词
wordlist = (jieba.cut(text, cut_all = False)) # cut_all, True为全模式,False为精确模式
wordlist_space_split = ' '.join(wordlist) #使用空格连接区分出来的各个词语。仿照英文的风格。
src = cv2.imread('./hua.jpg') #图片是生成词云的掩膜。
my_wordcloud = WordCloud( font_path='C:/Windows/Fonts/simkai.ttf',
background_color='white', max_words=130, mask=src,
max_font_size=250, random_state= 130, stopwords= stop,min_font_size=15
).generate(wordlist_space_split)
image_colors = ImageColorGenerator(src)
my_wordcloud.recolor(color_func= image_colors)
fileName = './new_wb20.png'
my_wordcloud.to_file(fileName )
cv2.imshow('word cloud',cv2.imread(fileName))
cv2.waitKey()