-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordCloud.py
72 lines (52 loc) · 1.73 KB
/
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
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
from black import main
from wordcloud import WordCloud,ImageColorGenerator
import imageio
import jieba
# 1.文本读取与分词
def jieba_(txtfile,stopfile=None):
content = open(txtfile, 'rb').read()
if stopfile is not None:
stop_words = []
with open(stopfile, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
stop_words.append(line.strip())
# jieba 分词
word_list = jieba.lcut(content)
words = []
for word in word_list:
if word not in stop_words:
words.append(word)
else:
# jieba 分词
words = jieba.lcut(content)
word_cloud = ','.join(words)
return word_cloud
# 2.获取图片形状
# 3.词云生成
def create_WC(word_cloud,cloud_mask,output):
# 定义词云的一些属性
wc = WordCloud(
# 背景图分割颜色为白色
background_color='white',
# 背景图样
mask=cloud_mask,
# 显示最大词数
max_words=200,
# 显示中文
font_path='./fonts/simhei.ttf',
# 最大尺寸
max_font_size=100
)
# 词云函数
x=wc.generate(word_cloud)
# 调用wordcloud库中的ImageColorGenerator()函数,提取模板图片各部分的颜色
image_colors = ImageColorGenerator(cloud_mask)
# 给词云对象按模板图片的颜色重新上色
wc_color = wc.recolor(color_func=image_colors)
# 将词云图片导出到当前文件夹
wc_color.to_file(output)
def main(txtfile,imagepath,output,stopfile=None):
word_cloud= jieba_(txtfile,stopfile=None)
cloud_mask=imageio.imread(imagepath)
create_WC(word_cloud,cloud_mask,output)