Word cloud Library Learning -- python exercises

Posted by Jackomo0815 on Tue, 07 Dec 2021 22:26:09 +0100

After downloading, open the command line, use the cd command to switch to the path of the file, and execute pip install wordcloud

catalogue

Word cloud No. 1: and that government of the people, by the people, for the people, shall not perish from the earth

No. 2 word cloud: facing the sea, spring flowers bloom (text part: from tomorrow on, be a happy person. Feed horses, chop firewood and travel around the world. From tomorrow on, care about food and vegetables. I have a house facing the sea, spring flowers bloom)

3. Word cloud: central document of Rural Revitalization Strategy (text read from external document: the document is in ziliao folder)

4. Word cloud: introduction word cloud of Tongji University (Chinese participle)

No. 5 word cloud: central document of Rural Revitalization Strategy (five pointed star shape)

In addition to the No. 4 word Yunfan, remove the statistics of "Tongji" and "University":

Word cloud No. 1: and that government of the people, by the people, for the people, shall not perish from the earth

import wordcloud   #Import word cloud
import jieba
w=wordcloud.WordCloud(width=1000,
                      height=700,
                      background_color='black',
                      font_path='msyh.ttc')
txt=("and that government of the people, by the people, for the people, shall not perish from the earth")
txtlist=jieba.lcut(txt)
string=" ".join(txtlist)
w.generate(string)
w.to_file('../picture/1.ciyun.png') #Picture path

No. 2 word cloud: facing the sea, spring flowers bloom (text part: from tomorrow on, be a happy person. Feed horses, chop firewood and travel around the world. From tomorrow on, care about food and vegetables. I have a house facing the sea, spring flowers bloom)

import wordcloud   #Import word cloud
import jieba
w=wordcloud.WordCloud(width=1000,
                      height=700,
                      background_color='white',
                      font_path='msyh.ttc')
txt=("From tomorrow on, be a happy person. Feed horses, chop firewood and travel around the world. From tomorrow on, care about food and vegetables. I have a house facing the sea and flowers bloom in spring")
txtlist=jieba.lcut(txt)
string=" ".join(txtlist)
w.generate(string)
w.to_file('../picture/2.ciyun.png') #Picture path

No. 3 word cloud: central document of Rural Revitalization Strategy (text read from external document: the document is in ziliao folder)

#No. 3 word cloud: central document of Rural Revitalization Strategy

import wordcloud   #Import word cloud
w=wordcloud.WordCloud(width=1000,
                      height=700,
                      background_color='white',
                      font_path='msyh.ttc')
f = open("../ziliao/Opinions on implementing the Rural Revitalization Strategy.txt", "r",encoding='utf-8')  //open documents
txt=f.read()
w.generate(txt)
w.to_file("../picture/3.ciyun.png")

Word cloud No. 4: introduction word cloud of Tongji University (Chinese participle)

Introduction to Tongji University (text part): Tongji University, hereinafter referred to as "Tongji", is a national key university directly under the Ministry of education of the people's Republic of China and jointly built by the Ministry of education, the State Oceanic Administration and Shanghai. It has a long history and outstanding reputation. It is a national "double first-class", "211 Project" and "985 Project" It is also one of the Chinese universities with the most stringent admission standards

import wordcloud   #Import word cloud
import jieba
w=wordcloud.WordCloud(width=1000,
                      height=700,
                      background_color='white',
                      font_path='msyh.ttc')
txt=("Tongji University( Tongji University),Tongji, for short, is a national key university directly under the Ministry of education of the people's Republic of China and jointly built by the Ministry of education, the State Oceanic Administration and Shanghai. It has a long history and outstanding reputation. It is a key university under the national "double first-class", "211 Project" and "985 Project". It is also one of the Chinese universities with the most stringent admission standards

")
txtlist=jieba.lcut(txt)
string=" ".join(txtlist)
w.generate(string)
w.to_file('../picture/4.ciyun.png') #Picture path

No. 5 word cloud: central document of Rural Revitalization Strategy (five pointed star shape)

import wordcloud   #Import word cloud
import jieba
import imageio
mk=imageio.imread("../ziliao/wujiaoxing.png")  //import picture
w=wordcloud.WordCloud(width=1000,
                      height=700,
                      background_color='white',
                      font_path='msyh.ttc',
                      mask=mk,)
f = open("../ziliao/rural vitalization.txt")  //import documents
txt=f.read()
list1 = jieba.lcut(txt)
txt_1 = " ".join(list1)
w.generate(txt_1)
w.to_file("../picture/5.ciyun.png")

In addition to the No. 4 word Yunfan, remove the statistics of "Tongji" and "University":

Because I can't upload ziliao documents, I won't write examples below. The main syntax is almost the same as above

Remove some words    stopwords={' ',' '}

import wordcloud   #Import word cloud
import jieba
w=wordcloud.WordCloud(width=1000,
                      height=700,
                      background_color='white',
                      font_path='msyh.ttc'
                      stopwords={'Tongji','university'} )
txt=("Tongji University( Tongji University),Tongji, for short, is a national key university directly under the Ministry of education of the people's Republic of China and jointly built by the Ministry of education, the State Oceanic Administration and Shanghai. It has a long history and outstanding reputation. It is a key university under the national "double first-class", "211 Project" and "985 Project". It is also one of the Chinese universities with the most stringent admission standards

")
txtlist=jieba.lcut(txt)
string=" ".join(txtlist)
w.generate(string)
w.to_file('../picture/4.ciyun.png') #Picture path

Topics: Python Pycharm