1 # -*- coding: utf-8 -*-
2 """
3 Created on Mon Apr 6 22:45:36 2020
4
5 @author: 49594
6 """
7
8 # coding:utf-8
9
10
11 from wordcloud import WordCloud
12 import matplotlib.pyplot as plt
13 import jieba
14
15
16
17
18 # 生成詞雲
19 def create_word_cloud(filename):
20
21 text = open("{}.txt".format(filename), 'rb' ).read()
22 #結巴分詞
23
24
25 wordlist = jieba.cut(text, cut_all=True) # 結巴分詞
26 wl = " ".join(wordlist)
27
28
29 # 設置詞雲
30 wc = WordCloud(
31 # 設置背景顏色
32 background_color="black",
33 # 設置最大顯示的詞雲數
34
35 max_words=2000,
36 # 這種字體都在電腦字體中,一般路徑
37 font_path='simsun.ttf',
38 height=1200,
39 width=1600,
40 # 設置字體最大值
41 max_font_size=100,
42 # 設置有多少種隨機生成狀態,即有多少種配色方案
43 random_state=100,
44 )
45
46 myword = wc.generate(wl) # 生成詞雲
47 # 展示詞雲圖
48 plt.imshow(myword)
49 plt.axis("off")
50 plt.show()
51 wc.to_file('py_book.png') # 把詞雲保存下
52
53
54 if __name__ == '__main__':
55 create_word_cloud('三國演義')
56
1 # -*- coding: utf-8 -*-
2 """
3 Created on Mon Apr 6 22:45:36 2020
4
5 @author: 49594
6 """
7
8 # coding:utf-8
9
10
11 from wordcloud import WordCloud
12 import matplotlib.pyplot as plt
13 import jieba
14 import numpy as np
15 from PIL import Image
16
17
18
19 # 生成詞雲
20 def create_word_cloud(filename):
21
22 text = open("{}.txt".format(filename), 'rb' ).read()
23 #結巴分詞
24
25
26 wordlist = jieba.cut(text, cut_all=True) # 結巴分詞
27 wl = " ".join(wordlist)
28 cloud_mask = np.array(Image.open(".png"))
29
30
31 # 設置詞雲
32 wc = WordCloud(
33 # 設置背景顏色
34 background_color="black",
35 # 設置最大顯示的詞雲數
36 mask=cloud_mask,
37 #設置背景圖片
38 max_words=2000,
39 # 這種字體都在電腦字體中,一般路徑
40 font_path='simsun.ttf',
41 height=1200,
42 width=1600,
43 # 設置字體最大值
44 max_font_size=100,
45 # 設置有多少種隨機生成狀態,即有多少種配色方案
46 random_state=100,
47 )
48
49 myword = wc.generate(wl) # 生成詞雲
50 # 展示詞雲圖
51 plt.imshow(myword)
52 plt.axis("off")
53 plt.show()
54 wc.to_file('py_book.png') # 把詞雲保存下
55
56
57 if __name__ == '__main__':
58 create_word_cloud('三國演義')
59