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 is # stutter word 23 is 24 25 wordlist = jieba.cut (text, cut_all = True) # stutter word 26 is WL = " " .join (wordlist) 27 28 29 # set word cloud 30 WC = wordcloud ( 31 is # set the background color 32 BACKGROUND_COLOR = " Black " , 33 is # set the maximum display number of word cloud 34 is 35 MAX_WORDS = 2000 , 36 #Such fonts are fonts in the computer, the general path of 37 [ font_path = ' the simsun.ttf ' , 38 is height = 1200 , 39 width = 1600 , 40 # Set Font maximum value 41 is max_font_size = 100 , 42 is # provided with a random generator state how many , i.e., how many color schemes 43 is random_state 100 = , 44 is ) 45 46 is myWord = wc.generate (WL) # generates a word cloud 47 # display word cloud 48 plt.imshow (myWord) 49 plt.axis ( " OFF " ) 50 plt.show () 51 is wc.to_file ( ' py_book.png ' ) # save the word cloud 52 is 53 is 54 is IF the __name__ == ' __main__ ' : 55 create_word_cloud ( ' Three Kingdoms ' ) 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 defcreate_word_cloud (filename): 21 is 22 is text = Open ( " {} .txt " .format (filename), ' RB ' ) .read () 23 is # stutter word 24 25 26 is wordlist = jieba.cut (text, cut_all = True) # stuttering word 27 WL = " " .join (wordlist) 28 cloud_mask = np.array (Image.open ( " .png " )) 29 30 31 is # set word cloud 32 WC = wordcloud ( 33 is # set the background color 34 = BACKGROUND_COLOR " Black " , 35 # set the maximum display number of word cloud 36 mask = cloud_mask, 37 [ # setting a background image 38 is MAX_WORDS = 2000 , 39 # this font font in the computer, the general path 40 font_path = ' the simsun.ttf ' , 41 is height = 1200 , 42 is width = 1600 , 43 is # set Font maximum value 44 is max_font_size = 100 , 45 # is provided with a status of how many randomly generated, i.e., how many color schemes 46 = 100 random_state , 47 ) 48 49 myWord = wc.generate (WL) # generates a word cloud 50 # display word cloud 51 is plt.imshow (myWord) 52 is plt.axis ( " OFF " ) 53 is plt.show () 54 is WC. to_file ( ' py_book.png ' ) # the word cloud storage at 55 56 is 57 is IF the __name__ == ' __main__ ' : 58 create_word_cloud ( ' Three Kingdoms ' ) 59