Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python to generate word cloud

Shulou Source: shulou.com Published: 2022-06-01 04:59:29 09月10日 Update

This article is about how to use python to generate word clouds. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Generate word cloud

The word cloud, also known as the word cloud, is the visual prominent presentation of the "keywords" with high frequency in the text data, forming the rendering of the keywords to form a color picture similar to the cloud, so that you can appreciate the main expression meaning of the text data at a glance.

But as an old code farmer, I still like to use my own code to generate my own word cloud. Is it complicated? Will it take a long time? A lot of text describes a variety of methods, but in fact it only takes 10 lines of python code.

Install the necessary libraries first

Pip install wordcloudpip install jiebapip install matplotlibimport matplotlib.pyplot as pltfrom wordcloud import WordCloudimport jiebatext_from_file_with_apath = open ('/ Users/linuxmi/linuxmi.txt'). Read () wordlist_after_jieba = jieba.cut (text_from_file_with_apath, cut_all = True) wl_space_split = .join (wordlist_after_jieba) my_wordcloud = WordCloud () .generate (wl_space_split) plt.imshow (my_wordcloud) plt.axis (off) plt.show ()

That's all, a word cloud generated goes like this:

Read these 10 lines of code:

1x 3 lines, imported the library of drawing matplotlib, the word cloud generation library wordcloud and the lexicon of jieba, respectively

4 lines, is to read the local file

Line 5: 6, use jieba for word segmentation, and separate the results of the word segmentation with spaces

7 lines to generate word clouds for the text after word segmentation

Line 8: 10, use pyplot to show the word cloud picture.

Thank you for reading! This is the end of the article on "how to use python to generate word cloud". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Generation code text key keywords content data text more articles good complex practical necessary that's all cloud images code generation pictures actually actually Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS OPPO Reno MySQL Huawei