用聊天记录生成云图

可以撩妹可以撩汉

直接贴代码

1
2
3
4
5
6
7
8
9
10
11
12
13
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
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 = []
# 打开当前目录下的聊天记录文件data.txt
for word in open('./data.txt', 'r', encoding='utf-8'):
tmp = word[0:4]
# print(tmp)
if (tmp == "2019" or tmp == "===="): # 过滤掉聊天记录的时间和qq名称
continue
tmp = word[0:2]
# print(tmp)
if (tmp[0] == '[' or tmp[0] == '/'): # 过滤掉图片和表情,例如[图片],/可爱
continue
newtext.append(word)

# 将过滤掉图片和表情和时间信息和qq名称剩下的文字重新写入当前目录下的a2.txt文件中去
with open('./a2.txt', 'w', encoding='utf-8') as f:
for i in newtext:
f.write(i)
# 打开新生成的聊天记录文件
text = open('./a2.txt', 'r', encoding='utf-8').read()
word_jieba = jieba.cut(text, cut_all=True)
word_split = " ".join(word_jieba)
# 找一张logo图来生成配色方案,logo图 timg1.jpg路径在当前目录下
alice_coloring = np.array(Image.open(os.path.join('./', 'timg1.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()
坚持原创技术分享,您的支持将鼓励我继续创作!