进阶词云

python实现词云

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
41
42
43
44
45
46
47
48
49
登陆微信获取好友个性签名生成词云
import itchat
import jieba
import imageio
import wordcloud
import matplotlib.pyplot as plt

# 登陆微信
itchat.login()
tList=[]

# 获取好友列表
friends=itchat.get_friends(update=True)

#构建所有好友个性签名组成的大列表tList
for i in friends:
signature=i["Signature"]
if 'emoji' in signature:
pass
else:
tList.append(signature)
text=" ".join(tList)

# 对个性签名进行中文分词
wordlist_jieba=jieba.lcut(text,cut_all=True)
wl_space_split=" ".join(wordlist_jieba)


# 读取个性图片
mk=imageio.imread("/Users/SJCHEN/Downloads/zihaowordcloud-master/code/chinamap.png")

# 构建词云对象
my_wordcloud=wordcloud.WordCloud(background_color='white',
width=1000,
height=700,
font_path='Songti.ttc',
max_words=2000,
mask=mk,
scale=20)
my_wordcloud.generate(wl_space_split)

nickname=friends[0]['NickName']
filename="example12-{}的微信好友个性签名词云图.png".format(nickname)

# 显示词云图
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
print('程序结束')