【Python3.6】生成微信好友个性签名词云

代码链接:https://gitee.com/AI-Echo/codes/4bk0tsqczpe6dfyhu7mlo12
思路:
1.通过itchat爬取微信好友信息,获取好友签名Signature
2.将Signature拼接成Str,并去掉预先设置的一些停用词(如的、是、有等,主要是为了留下名词和形容词)
3.导入jieba模块分词
4.导入wordcloud 模块生成词云,用matplotlib.pyplot画出图像并保存

# -*- coding: utf-8 -*-
"""
Created on Thu Sep 14 09:26:32 2017

@author: admin
"""

import itchat
import re
itchat.login()
friends = itchat.get_friends(update=True)[0:]
tList = []
for i in friends:
    #除去空格、表情(表情在爬取的时候会包含在span标签内)
    signature = i["Signature"].replace(" ", "").replace("span", "").replace("class", "").replace("emoji", "")
    rep = re.compile("1f\d.+")
    signature = rep.sub("", signature)
    tList.append(signature)
    # 拼接字符串
    text = "".join(tList)
# jieba分词
import jieba
#去除停用词
stopwords = {}.fromkeys(['的', '包括', '等', '是','不是','有人','人','有'])
#去掉停用词时用精确模式
segs = jieba.cut(text, cut_all=False)
final = ''
for seg in segs:
#==============================================================================
#     seg = seg.encode('utf-8')
#==============================================================================
    if seg not in stopwords:
        final += seg
#处理完后分词,采用全模式
wordlist_jieba = jieba.cut(final, cut_all=True)
wl_space_split = " ".join(wordlist_jieba)
# wordcloud词云
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import os
import numpy as np
import PIL.Image as Image

# 获取当前文件路径
d= os.path.dirname(os.path.abspath( __file__ ))

#需要在py文件所在目录下已经有模版图片 timg2.jpg
alice_coloring = np.array(Image.open(os.path.join(d, "timg2.jpg")))
my_wordcloud = WordCloud(background_color="white", max_words=2000,mask=alice_coloring,max_font_size=400, random_state=420,font_path='C:\Windows\Fonts\SIMYOU.TTF').generate(wl_space_split)
image_colors = ImageColorGenerator(alice_coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
#不显示坐标轴
plt.axis("off")
#保存图片,需在plt.show()前调用
#在plt.show() 后实际上已经创建了一个新的空白的图片(坐标轴),
#这时候你再plt.savefig() 就会保存这个新生成的空白图片。
plt.savefig("examples.jpg")  
plt.show()

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值