Implementation (recommended) of the word cloud generated by python and generation by python
The end of the review is too busy to write the scrapy framework for some time. Today we will introduce how to use python to generate word cloud. Although there are many word cloud generation tools on the internet, but is it a sense of accomplishment to write in python.
Today, we are going to generate the word cloud of inspirational songs. Baidu Library has found 20 songs, such as stubborn and familiar to everyone.
The required python libraries include jieba (a Chinese Dictionary), wordcocould, matplotlib, PIL, and numpy.
The first thing we need to do is read the lyrics. I saved the lyrics to the inspirational song text in the file directory.
Read him now
# Encoding = gbklyric = ''f = open ('./Lizhi songs. txt', 'R') for I in f: lyric + = f. read ()
Add # encoding = gbk to prevent the following Operation errors SyntaxError: Non-UTF-8 code starting with '\ xc0'
Then, we use the jieba word segmentation to extract words with high word frequencies from songs.
import jieba.analyseresult=jieba.analyse.textrank(lyric,topK=50,withWeight=True)keywords = dict()for i in result: keywords[i[0]]=i[1]print(keywords)
Expected result:
Then we can use libraries such as wrodcloud to generate word clouds.
First, find an image to generate the word cloud shape.
from PIL import Image,ImageSequenceimport numpy as npimport matplotlib.pyplot as pltfrom wordcloud import WordCloud,ImageColorGeneratorimage= Image.open('./tim.jpg')graph = np.array(image)wc = WordCloud(font_path='./fonts/simhei.ttf',background_color='White',max_words=50,mask=graph)wc.generate_from_frequencies(keywords)image_color = ImageColorGenerator(graph)plt.imshow(wc)plt.imshow(wc.recolor(color_func=image_color))plt.axis("off")plt.show()
Save the generated image
wc.to_file('dream.png')
Complete code:
# Encoding = gbkimport jieba. analysefrom PIL import Image, ImageSequenceimport numpy as npimport matplotlib. pyplot as pltfrom wordcloud import WordCloud, ImageColorGeneratorlyric = ''f = open ('. /Lizhi songs. txt ', 'R') for I in f: lyric + = f. read () result = jieba. analyze. textrank (lyric, topK = 50, withWeight = True) keywords = dict () for I in result: keywords [I [0] = I [1] print (keywords) image = Image. open ('. /tim.jpg ') graph = np. array (image) wc = WordCloud (font_path = '. /fonts/simhei. ttf', background_color = 'white', max_words = 50, mask = graph) wc. generate_from_frequencies (keywords) image_color = ImageColorGenerator (graph) plt. imshow (wc) plt. imshow (wc. recolor (color_func = image_color) plt. axis ("off" )plt.show(%wc.to_file('dream.png ')
The implementation method (recommended) of the above python generation word cloud is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.