In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to generate Python word cloud map". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to generate Python word cloud map"!
Code example
First of all, import the basic library, here select the jiebe word segmentation library, for the text word segmentation processing.
Import jiebaprint (list (jieba.cut ("Natural language is an important direction in the field of computer science and artificial intelligence.")
The running result of the code is as follows, which realizes the word segmentation of each statement.
['natural language', 'yes', 'computer science', 'domain', 'and', 'artificial intelligence', 'domain', 'medium', 'one', 'important', 'direction','] Then read the experimental text that we need to do the word cloud.
# read the text with open (r "C:\ Users\ shangtianqiang\ Desktop\ experimental text .txt", encoding= "utf-8") as file: text=file.read () print (text) where the imported experimental text is segmented and the delimiter is empty.
# text participle wordlist_jieba=jieba.cut (text) space_wordlist= ".join (wordlist_jieba) print (space_wordlist)
After the sentence is successfully segmented, the word segmentation function can be defined, which will be used later.
# text segmentation def chinese_jieba (text): wordlist_jieba=jieba.cut (text) space_wordlist= ". Join (wordlist_jieba) return space_wordlistspace_wordlist immediately imports the word cloud library. After reading the text content, set the background color of the word cloud, the size of the picture, control the number of words displayed, and so on.
From wordcloud import WordCloud
# read text content with open (r "C:\ Users\ shangtianqiang\ Desktop\ experimental text .txt", encoding= "utf-8") as file: text=file.read ()
# set the background color, width, height, and word count of the word cloud wordcloud=WordCloud (font_path= "C:/Windows/Fonts/simfang.ttf", background_color= "white", width=600,height=300,max_words=30) .generate (text)
# generate image image=wordcloud.to_image ()
# display picture image.show () finally, encapsulate the debugged code and take a look at the final result.
# Segmentation the text and add the picture background from wordcloud import WordCloudimport jiebaimport numpyimport PIL.Image as Image
# split the string def chinese_jieba (text): wordlist_jieba=jieba.cut (text) space_wordlist= "" .join (wordlist_jieba) return space_wordlist
# read text content with open (r "C:\ Users\ shangtianqiang\ Desktop\ experimental text .txt", encoding= "utf-8") as file: text=file.read () text=chinese_jieba (text)
# here you need to import an image as the background image mask_pic=numpy.array (Image.open (r "C:\ Users\ shangtianqiang\ Desktop\ picture 1.png"))
# set the parameter mask to: mask_picwordcloud = WordCloud (font_path= "C:/Windows/Fonts/simfang.ttf", background_color= "white", width=600,height=300,max_words=30,mask=mask_pic) .generate (text)
# generate image image=wordcloud.to_image ()
# display picture image.show ()
The word cloud results shown here are not intuitive. By changing our background picture and making the text words of the experiment more simple, we can get the following results by debugging a few more times.
At this point, I believe you have a deeper understanding of "how to generate Python word cloud map". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.