Python draw word cloud

Libraries to be used to draw word clouds:

1, wordcloud
2, imageio (this third-party library can import photos in many formats, and then export them into photos in various formats, very easy to use)

3. matplotlib (data display)
4. jieba (Chinese text segmentation)

This is the text data we want to count (can be saved as txt for practice):

美观,美观,美观,美观,美观,美观,美观,
个性,个性,个性,个性,个性,个性,个性,个性,个性,个性,个性,个性,个性,个性,个性,个性,
性价比,性价比,性价比,性价比,性价比,性价比,性价比,性价比,性价比,性价比,
酷,酷,酷,酷,酷,酷,酷,酷,酷,酷,酷,酷,酷,
1.对于爱情或友情,我只相信一句话:日久见人心,留到最后的才是好的。 我会冷暖自知,更会好自为之。
2.我只是单身 但我并不孤独
3.当我的朋友都去约会,只剩下我自己的时候,我还是会有一丢丢失落和孤独的。我的他,你快点来,不然这么可爱的我好像也要不可爱了。
4.单身是最好的增值期,在不被爱的日子里,你更要爱惜自己
5.两亿分之一的我,很享受现在的生活,希望两亿分之一的你,好好的照顾好自己,该来的总会来
6.我们单身,并不是因为情商低,也并不是因为忘不了谁。而只是稍微清楚自己想要的是什么, 而不是随便抓着个人就告诉自己处着试试。我们,总会遇见那个对的人,相扶到老
7.总嚷着要找个对象,却从不主动勾搭。没喜欢的人,也懒得接受别人的追求。倒也不是那么“宁缺毋滥”,却还是不肯委屈将就。有时感觉单身挺好的,又常常羡慕别人成双入对。这才是很多人单身的日常吧
8.单身无罪,愿你嫁给爱情,好好享受孤独和一个人的日子。因为这样的日子不多了,珍惜一个人。等你遇到了那个值得托付终身的人,单身的日子你会非常怀念。愿所有单身朋友无所畏惧,不要因为流言有压力,也不要畏惧年龄草草的嫁了自己。也不要羡慕别人的爱情,其实你不知道有多少人羡慕你呢
9.凭本事单身 怕什么甜言蜜语
10.22岁的我惧怕着孤独,却在孤独里安安稳稳的驻扎自己的生活。或许人生来孤独,好在,从来不会输给孤独。
11.一个人最好的状态,就是依然相信爱情,期待爱情的美好,同时也能在孤独的日子里爱自己,善待身边的人。先学会做一个温暖的人,然后才会有人来温暖你。我们都会越来越好的。
12.从前喜欢一个人,现在喜欢 一个人。从不适应到佛系OK,舒服最重要
13.我虽然不是单身,但我有一个单身的朋友,说得很对,既然选择了孤单就要在孤单中找到快乐jxh
14.不如意的婚姻不如单身过得幸福
15.谈恋爱总是不由自主去想另外一伴在干什么,他有没有好好的……,分散了自己的注意力,做事效果也在慢慢的下降,在一个安静的环境中看书,思绪也不由自主跑偏了,当没有恋爱时,心无牵挂可以好好的,让单身的自己好好的拼一拼吧, 致单身的我们
16.单身让你有更多的时间和精力用来提升自己
17.非常的赞同,单身真好!
18.不找对象 是觉得自己还没到适合给他人安稳生活的状态,我要待我事业有成 想做什么随时能去做时,再考虑
19.不论你现在身边有没有那个人,你都要朝气蓬勃的,满怀希望的,热气腾腾的,努力去生活,去过好余生。 耐心等待吧,所有的都在来的路上
20.我可以一个人坐公交一直到终点。
21.偶尔羡慕情侣,偶尔庆幸单身,还是单身最好
22.当我还没有找男朋友的想法时,家人亲戚朋友都已经开始幻想我什么时候结婚,其实我不着急,虽然羡慕别人的甜甜的恋爱,羡慕别人的两人三餐日常,但是还是呢句话不想将就,等呢个足够优秀足够对我好,足够温柔的人叭!没事慢慢来
23.单身并不意味着自己有多糟糕,也不是没有能力在谈下一段感情,而是你真的无法再去接受你所希望的往往让你绝望,在你一个人的生活里你很努力生活很精致,你渐渐的迷恋这种生活,这就是所谓的一个人!!
24.顺其自然 好好爱自己 好好生活 晚安呀
25.做一枚热爱生活的单身狗,自己一个人去健身房健身,自己一个人去学习,自己一个人去旅游。过好自己的每一天~
26.照顾好自己,无论是一日三餐,还是白天黑夜。
27.一时单身一时爽,一直单身一直爽。 花太多时间去和另一个人在一起,有时候还会有烦恼,麻烦,还有生气。还不如一个人,整理整理生活,做点好吃的,听首美妙的歌曲,不香吗?非得找个人给自己添堵
28.有对象的幸福,单身的你也想象不到
29.我有点理解一句话:许多已婚人士劝你结婚不是因为她有多幸福,是看你单身爽她却不能而想把你也拖进火坑
30.我上班已经累一天了,下了班一个人安安静静舒舒服服做些自己想做的事,没人打扰没人支配,真的是太享受独处的时光了啊!!!
31.可是我看见那些情侣也是会羡慕的啊
32.单身挺好,要有另一半可能会更好而已
33.一个人挣钱一个人花, 别人买俩我买仨, 单身快乐
34.单身渴望恋爱的感觉,你想象不到
35.祝单身快乐
36.单身一时爽,一直单身一直爽我这个周末睡到下午才醒的人,曾经为了约会不得不早起,现在休息日睡到自然醒好爽
37.单身时尽情享受那份自由和快乐,爱情可遇不可求,来时坦然接受,没有,继续美好生活,毕竟,一个人的世界好过一地鸡毛和心碎的撕扯……
38.剑客,最重要的是没有感情,女人只会影响我拔剑的速度
39.单身不用操心恋爱的烦恼,多好
40.单身虽然快乐,但是人生总要有家,有人陪伴才温暖,没有家就没有单身的我们,还是要找个人结婚,组个家吧
41.所以不明白为什么到了年纪就要结婚生子,你结婚你快乐,但是也不能把你认为的快乐强行让别人也同意吧!三五好友,游山玩水,偶尔小聚哪一个不快乐,都享受着自己的快乐就好。
42.年过三十,单身的快乐享受着,却也在内心无比期待婚姻。更受不了家人的逼婚和日渐年迈的父母。
43.单身时会酸身边有陪伴的甜蜜,周末自己在房间游戏玩腻,无剧可刷时;节日看电影想找个人分享感受时;下班回家看见冷清的房子,吃饭的时候看见一个人的碗筷时。总想找个人来感受一下甜蜜
44.不单身不知道外卖凑单的苦。
45.刚24岁的女博士表示一个人过实在太有趣了曾经一个人玩遍香港迪士尼和海洋公园,现在觉得唯一需要人陪的就是需要麻醉的手术或者检查(比如无痛胃镜),搬家可以叫搬家公司,在家不舒服了可以美团送药,感谢这个时代赐予独居女性如此多的便利,让我们能更好地做自己

Statistical word cloud effect:

Insert picture description here

We divide the construction of the word cloud into two steps:

1. Processing text data

When generating a word cloud, wordcloud will use spaces or punctuation as delimiters to segment the target text by default. For Chinese text, word segmentation processing needs to be completed by the user. The general step is to first segment the text, then splice it with spaces, and then call the wordcloud library function

2. Generate word cloud pictures

The core of the wordcloud library is the WordColoud class, and all functions are encapsulated in the WordCloud class. When using, you need to instantiate an object of the Wo rd C oloud class, and call its generate(text) method to convert the text text into a word cloud

So we use functional programming to write code.

1. Processing text data

jieba supports three word segmentation modes:

  • The precise mode lcut() tries to cut the sentence most accurately, which is suitable for text analysis, and the words have no redundancy;
  • The full mode lcut(s, cut_all=True) scans out all the words in the sentence that can be formed into words. The speed is very fast, but it cannot resolve ambiguities and is redundant;
  • The search engine mode cut_for_search(s), on the basis of the precise mode, splits long words again to improve the recall rate, and is suitable for search engine word segmentation.
from wordcloud import WordCloud
from imageio import imread
import matplotlib.pyplot as plt
import jieba
 
def read_deal_text():
    with open(r"C:\Users\David\Desktop\all.txt","r",encoding='utf-8') as f: 
        #读取我们的待处理本文
        txt=f.read()
    re_move=[",","。"," ",'\n','\xa0'] #无效数据
       #去除无效数据
    for i in re_move:
        txt=txt.replace(i," ") 
    word=jieba.lcut(txt)  #使用精确分词模式进行分词后保存为word列表   
 
    with open(r"C:\Users\David\Desktop\txt_save.txt",'w') as file:
        for i in word:    
            file.write(str(i)+' ')
    print("文本处理完成")

Then save the word list with a space connection to the txt_save.txt file for the next step to generate word cloud pictures

Second, generate word cloud pictures

The commonly used method of the WordCloud class
generate(text) Generate a word cloud from the text text
to_file(filename) Save the word cloud image as a file named filename

def img_grearte():
    mask=imread(r"C:\Users\David\Desktop\boy.png")
    with open(r"C:\Users\David\Desktop\txt_save.txt","r") as file:
        txt=file.read()
    word=WordCloud(background_color="white",\
                    width=800,\
                   height=800,
                   font_path='simhei.ttf',
                   mask=mask,
                   ).generate(txt)
    word.to_file(r"C:\Users\David\Desktop\test.png")
    print("词云图片已保存")
    
    plt.imshow(word)    #使用plt库显示图片
    plt.axis("off")
    plt.show()

mask=imread("boy.png") #Used for the picture background, you can download the transparent png background picture by yourself. The imread library function belongs to the scipy library, and pip will automatically install the dependent library when installing the wordcloud library.

font_path='simhei.ttf' #Here we use Chinese font simhei.ttf, wordcloud default font does not support Chinese

Note: Here boy.png and simhei.ttf are placed in the same directory as the program.

We create a wordcloud instance object word, and set its attributes, including background color, picture size, font, picture background, and finally generate a word cloud picture and save it, which can be viewed frequently in the program directory.

Use plt library to display pictures later

3. Complete as follows:

from wordcloud import WordCloud
from imageio import imread
import matplotlib.pyplot as plt
import jieba
 
def read_deal_text():
    with open(r"C:\Users\David\Desktop\all.txt","r",encoding='utf-8') as f:
        txt=f.read()
    re_move=[",","。"," ",'\n','\xa0']
       #去除无效数据
    for i in re_move:
        txt=txt.replace(i," ") 
    word=jieba.lcut(txt)  #使用精确分词模式
 
    
    with open(r"C:\Users\David\Desktop\txt_save.txt",'w') as file:
        for i in word:    
            file.write(str(i)+' ')
    print("文本处理完成")
 
def img_grearte():
    mask=imread(r"C:\Users\David\Desktop\boy.png")
    with open(r"C:\Users\David\Desktop\txt_save.txt","r") as file:
        txt=file.read()
    word=WordCloud(background_color="white",\
                    width=800,\
                   height=800,
                   font_path='simhei.ttf',
                   mask=mask,
                   ).generate(txt)
    word.to_file(r"C:\Users\David\Desktop\test.png")
    print("词云图片已保存")
    
    plt.imshow(word)    #使用plt库显示图片
    plt.axis("off")
    plt.show()
 
 
read_deal_text()
img_grearte()

Guess you like

Origin blog.csdn.net/weixin_44322234/article/details/111651541