程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python | Word Cloud] Draw a super beautiful word cloud from chat records (Happy Qixi Festival, classmate Zeng)

編輯:Python

七夕快樂 !

 —— "I found that all beings are equal in my heart,Only you are overweight"

解釋器:python.3.9
編譯器:jupyter notebook
時間:8/4 10:20

文章目錄

  • 七夕快樂 !
  • 一、打開聊天記錄文件
  • 二、 Process chat history
    • 2.1 去除無用信息
    • 2.2 中文分詞
  • 三、繪制詞雲
    • 3.1 colormap Modify the main color
    • 3.2 Import picture outlines
    • 3.3 Picture style one
    • 3.4 Picture style two
    • 3.5 Picture style three
    • 3.6 Picture style four

一、打開聊天記錄文件

This chat record is collected between me and my classmate ZengQQChat 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

二、 Process chat history

2.1 去除無用信息

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

輸出:

2.2 中文分詞

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()

3.1 colormap Modify the main color

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()

輸出:

3.2 Import picture outlines

Import the prepared image, 使用psTool 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'))

原圖:



3.3 Picture style one

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()

3.4 Picture style two

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()

3.5 Picture style three

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()

3.6 Picture style four

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》

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved