In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to generate any shape word cloud in Python, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
What is a word cloud? To put it simply, a paragraph of text will be provided, an image generated according to the frequency of keywords, and people can understand the gist of the article at a glance. Today we use python to implement such a project. If successful, the resulting result will be as follows
PYTHON steps for generating Chinese word cloud
You need to install Python on your computer, and then use pip tools to install the required jieba, wordcloud, PIL modules.
1. Installation dependency
$pip install jieba
$pip install wordcloud
$pip install PIL
two。 Add articles that you want to use to generate text
The editor made a text out of the lyrics of the theme song and put it in the data catalog. Ok,DJ drop the beat
Memories go back and forth, but this heart hurts.
May the rest of my life have no regrets and fly with the fragrance of the flowers.
A pot of sake covered with dust
After thinking about it, I will spend the rest of my life with no regrets.
The birth and demise of the Spring and Autumn period
Get drunk again when the flowers bloom
Do not want to dye is right and wrong how things go against one's wishes
When the flower in my heart withered, it can't go back.
Memories go back and forth, but this heart hurts.
3. Select the shape of the word cloud
Hold, now find a picture and use it to define the shape of the word cloud you finally generated. Ok, I found a pirated picture of DJ and put it in the root directory.
4. Code part
A) Dictionary of words and weights in jieba
Def tokenize_content (content):
Jieba.analyse.set_stop_words ("data/stop_words.txt")
Tags = jieba.analyse.extract_tags (content, topK=50, withWeight=True)
Word_tokens_rank = dict ()
For tag in tags:
Word_tokens_rank [tag [0]] = tag [1]
Return word_tokens_rank
The first step is to remove some stop words, such as colloquial words or words with little meaning, which can be customized in stop_words.txt under the data directory.
The second step is to extract the first 50 words according to weight and convert them into dictionary form.
B) use the generated dictionary above and pass in word_cloud to generate the final effect
Def generate_wordcloud (tags, mask):
Word_cloud = WordCloud (width=512, height=512, random_state=10, background_color='white', font_path=font,stopwords=STOPWORDS, mask=mask)
Word_cloud.generate_from_frequencies (tags)
Plt.figure (figsize= (10,8), facecolor='white', edgecolor='blue')
Plt.imshow (word_cloud)
Plt.axis ('off')
Plt.tight_layout (pad=0)
Plt.show ()
Note that if you are using Apple computer, macOSX system. Need to add
Import matplotlib
Matplotlib.use ('TkAgg')
Matters needing attention:
1: note that if you are using Apple computer, macOSX system. It needs to be added when the package is introduced.
Import matplotlib
Matplotlib.use ('TkAgg')
2: if it is a windows system, you need to find a specific Chinese font and define the Chinese font path, such as the following.
Font = ringing C:\ Windows\ Fonts\ simhei.ttf' about how to generate any shape in Python word cloud is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.