微信签名生成词云

首先我们需要安装一下模块

pip install wxpy
pip install matplotlib
pip install wordcloud
pip install Pillow
pip install numpy
pip install jieba

上边的模块安装成功后,我们就开始制图了

from wxpy import *
import re
import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS
from PIL import Image
import numpy as np
import jieba

bot=Bot(cache_path=True)
friends=bot.friends()

# 统计签名
with open('signatures.txt','w',encoding='utf-8') as f:
    for friend in friends:
        pattern=re.compile(r'[一-龥]+')
        filterdata=re.findall(pattern,friend.signature)
        f.write(''.join(filterdata))

abel_mask = np.array(Image.open("chw2.png"))
text_from_file_with_apath = open('signatures.txt',encoding='utf-8').read()
wordlist_after_jieba = jieba.cut(text_from_file_with_apath, cut_all=True)
wl_space_split = " ".join(wordlist_after_jieba)
stopwords = set(STOPWORDS)
wc = WordCloud(background_color="white", margin=5,max_words=2000, mask=abel_mask,
               stopwords=stopwords).generate(wl_space_split)

wc.to_file("alice.png")
plt.imshow(wc)
plt.axis("off")
plt.show()

 

 
 
 
posted @ 2018-12-07 10:39  贾迪123  阅读(191)  评论(0编辑  收藏  举报