#导入需要模块 import jieba import numpy as np import matplotlib.pyplot as plt from PIL import Image from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator text_road = str (the INPUT ( ' Enter the path to the article: ' )) picture_road = STR (INPUT ( ' Enter the path of the image: ' )) # Loading articles need to analyze text = Open (text_road, ' R & lt ' , encoding = ' UTF-. 8 ' ) .read () # Are articles word wordlist_after_jieba = jieba.cut (text, cut_all = False) wl_space_split = " ".join(wordlist_after_jieba) # Being read configuration data photos by numpy.array function into Array-NP mask = np.array (Image.open (picture_road)) # Selection mask word, which does not appear in a word cloud stopwords = SET (stopwords) # can be added to a plurality of stop words stopwords.add ( " a " ) # Create a word cloud objects WC = wordcloud ( background_color="white", font_path='/Library/Fonts/Arial Unicode.ttf', MAX_WORDS = 1000, # up to display the number of words mask = mask, stopwords=stopwords, max_font_size = 100 # font maximum ) # Generate a word cloud wc.generate (text) # Establishing a color scheme from background image_colors = ImageColorGenerator (mask) # The word cloud background color to scheme wc.recolor (color_func = image_colors) # Display Word cloud plt.imshow (WC, interpolation = ' Bilinear ' ) # Close axis plt.axis ( " OFF " ) # Display images plt.show () # Save word cloud wc.to_file ( ' word cloud .png ' )
# Import module needs to import jiebaimport numpy as np import matplotlib.pyplot as plt from PIL import Image from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator text_road = str (input ( 'Enter the path of the article:')) picture_road = str (input ( ' enter the path to the picture: ')) # load the required paper analyzes the text = open (text_road,' r ', encoding =' utf-8 ') read () # are articles word wordlist_after_jieba = jieba.cut (text,. cut_all = False) wl_space_split = "" .join (wordlist_after_jieba) # numpy.array being read by the function configuration data into photos np-arraymask = np.array (Image.open (picture_road)) # word selection screen, not display stopwords = set (sTOPWORDS) # can be added to a plurality of stop words stopwords.add ( "<br/>") # create a word cloud word cloud objects inside wc = wordCloud (background_color = "white", font_path = '/ Library / fonts / Arial Unicode.ttf ', max_words = 1000, # shows the number of words up mask = mask, stopwords = stopwords, max_font_size = 100 # maximum font) # word cloud generated wc.generate (text) # establish the background color scheme image_colors = ImageColorGenerator (mask) # display color is set to a word cloud word cloud plt.imshow (wc, interpolation = 'bilinear' background program is wc.recolor (color_func = image_colors) # ) # Close axis plt.axis ( "off") # display image plt.show () # save word cloud wc.to_file ( 'word cloud .png')