1. 程式人生 > >python 將QQ聊天記錄生成詞雲圖

python 將QQ聊天記錄生成詞雲圖

首先,在QQ的訊息管理器中將某一個人或者全部的聊天記錄匯出成txt格式,放入F盤下;

執行下面的python程式碼需要安裝jieba和wordcloud兩個庫。

import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
from wordcloud import WordCloud, ImageColorGenerator
import os
import numpy as np
import PIL.Image as Image

newtext = []
#開啟F盤下的聊天記錄檔案e.txt
for word in open('F:\\e.txt', 'r',encoding='utf-8'):
    tmp = word[0:4]
    #print(tmp)
    if (tmp == "2017" or tmp == "===="):#過濾掉聊天記錄的時間和qq名稱
        continue
    tmp = word[0:2]
    #print(tmp)
    if (tmp[0] == '[' or tmp[0] == '/'):#過濾掉圖片和表情,例如[圖片],/可愛
        continue
    newtext.append(word)

#將過濾掉圖片和表情和時間資訊和qq名稱剩下的文字重新寫入F盤下的ab.txt檔案中去
with open('F:\\a2.txt', 'w', encoding='utf-8') as f:
    for i in newtext:
        f.write(i)
#開啟新生成的聊天記錄檔案
text = open('F:\\a2.txt', 'r',encoding = 'utf-8').read()
word_jieba = jieba.cut(text, cut_all=True)
word_split = " ".join(word_jieba)
#找一張小黃人logo圖來生成配色方案,小黃人logo圖小黃人.jpg路徑在F:\\盤下
alice_coloring = np.array(Image.open(os.path.join('F:\\','小黃人.jpg')))
my_wordcloud = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
                         max_font_size=40, random_state=42,
                         font_path='C:/Windows/Fonts/simhei.ttf')\
    .generate(word_split)
image_colors = ImageColorGenerator(alice_coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
生成的詞雲圖如下:


過濾器寫的不好,結果中聊天記錄中的QQ表情沒有過濾掉,還有待完善。