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