—— "I found that all beings are equal in my heart,Only you are overweight"
解釋器:
python.3.9
編譯器:jupyter notebook
時間:8/4 10:20
This chat record is collected between me and my classmate ZengQQ
Chat history for a year,Let's take a look at what is the most spoken word between you couples!
import re
import wordcloud
import PIL
import numpy as np
import jieba
chat = open('./Princess(2023624240).txt',mode='r',encoding='utf-8')
text = chat.read()
text
Each piece of information has one thing in common:
日期 用戶名
信息
[換行]
使用
re
模塊 去除不需要的[圖片]
, 用戶名和 換行符\n
, As well as date and high frequency system messages
re_text = re.sub('[圖片]|The boss came for a bowl of chopped noodles|@waaaaaaaaaaaaaaa|Princess|prince|\n|202\d-\d{2}-\d{2} \d{1,}:\d{2}:\d{2}|撤回了一條消息,並壞笑了一下.|ss|[表情]|系統消息|','',text)
re_text
輸出:
A third-party lexicon is used here jieba
jieba_text = jieba.lcut(re_text)
jieba_text
輸出:
Connect each word with a space
result_text = ' '.join(jieba_text)
result_text
輸出:
wcd_z = wordcloud.WordCloud(font_path='./../font/Muyao.TTF/',mode='RGBA',colormap='Reds',background_color=None,repeat=True,max_words=200,width=800,height=600,max_font_size=100)
wcd_z.generate(result_text)
wcd_z.to_image()
Hue library: https://www.matplotlib.org.cn/gallery/color/colormap_reference.html
這裡使用 The current season is summer autumn
wcd_z = wordcloud.WordCloud(font_path='./../font/Muyao.TTF/',mode='RGBA',colormap='autumn',background_color=None,repeat=True,max_words=200,width=800,height=600,max_font_size=100)
wcd_z.generate(result_text)
wcd_z.to_image()
輸出:
Import the prepared image, 使用
ps
Tool to subtract unwanted background, Here for the best look,I used four different sets of backgrounds(The best thing to look at is the one I drew myself🤭)
Import four pictures
mask1 = np.array(PIL.Image.open('./qixi.png',mode='r'))
mask2 = np.array(PIL.Image.open('./qixi2.png',mode='r'))
mask3 = np.array(PIL.Image.open('./qixi3.png',mode='r'))
mask4 = np.array(PIL.Image.open('./qixi4.png',mode='r'))
原圖:
wcd_z = wordcloud.WordCloud(font_path='./../font/Muyao.TTF/',mode='RGBA',mask=mask1,colormap='autumn',background_color=None,repeat=True,max_words=500,min_font_size=1,width=800,height=600,max_font_size=100)
wcd_z.generate(result_text)
wcd_z.to_image()
wcd_z = wordcloud.WordCloud(font_path='./../font/Muyao.TTF/',mode='RGBA',mask=mask2,colormap='autumn',background_color=None,repeat=True,max_words=500,min_font_size=1,width=800,height=600,max_font_size=100)
wcd_z.generate(result_text)
wcd_z.to_image()
wcd_z = wordcloud.WordCloud(font_path='./../font/Muyao.TTF/',mode='RGBA',mask=mask3,colormap='tab20',background_color=None,repeat=True,max_words=600,min_font_size=1,width=800,height=600,max_font_size=100)
wcd_z.generate(result_text)
wcd_z.to_image()
wcd_z = wordcloud.WordCloud(font_path='./../font/Muyao.TTF/',mode='RGBA',mask=mask4,colormap='tab20',background_color=None,repeat=True,max_words=1400,min_font_size=1,width=800,height=600,max_font_size=100)
wcd_z.generate(result_text)
wcd_z.to_image()
你來了,A green moon,Fall into my young cabin.
——海子《Wedding at sea》