[wordcloud library] According to the 2021 work report, the wordcloud word cloud library is used to display the keywords in the report

Using wordcloud can make a document better display the keywords in it, so that users can know the key points more clearly and straightforwardly.

Problem Description: 

Search for the 2021 work report, save it as a text file, use the jieba library to segment the report, and then use the wordcloud word cloud library to highlight the high-frequency words in the report to make the result more intuitive, and then save it as a picture.

You need to save the 2021 work report as a text file with the correct path, and then modify the file name in the program according to the path and file name.

In order to display the generated word cloud image in a map or other shape, you need to select a specified image as the background. Here you can use a layout with a darker color and a solid color, so that the generated image is more beautiful. Since it would be illegal to put a map here, no pictures will be attached here. If necessary, please private message the blogger.

The program code is as follows:

#词云库应用
import jieba
import wordcloud
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
I=np.array(Image.open("地图.png"))
excludes = {"各位代表"} #排除词库
txt = open("2021工作报告.txt", "r", encoding="utf-8")#保存的报告文件名
txt1=txt.read()
txt.close()
words = jieba.lcut(txt1)
ls=" ".join(words)
c=wordcloud.WordCloud(stopwords=excludes,font_path="C:/Windows/Fonts/STXINGKA.ttf",#字体设置电脑磁盘字体库中的一种字体
                      background_color='white',colormap='autumn',
                      mask=I,height=400,width=854,min_word_length=2,
                      prefer_horizontal=0.8,relative_scaling=0.8)
c.generate(ls)
c.to_file("wordcloudtest1.png")
plt.imshow(c)
plt.axis('off')
plt.show()

 The part of the program running result is as follows:

 

Putting the map will violate the rules, only part of the display is intercepted here 

The obtained pictures are quite beautiful. Modifying some parameters of the word cloud database can change the effect of the generated pictures. If you have any questions, please leave a message to ask questions, and the blogger will restore them as soon as possible.

Don't forget to give the blogger a like! 

Guess you like

Origin blog.csdn.net/qq_59049513/article/details/122526856