Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to generate Python word Cloud Picture

Shulou Source: shulou.com Published: 2022-06-01 20:18:40 10月01日 Update

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!

Tags: Text pictures generation experiments content background results domain cloud images code utf-8 processing important artificial intelligence direction intelligence science nature natural language Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Redmi Linux Apple