分析微信好友

import itchat
import jieba.analyse
import matplotlib.pyplot as plt
from snownlp import SnowNLP
from PIL import Image
import numpy as np
from wordcloud import WordCloud
import re

from collections import Counter
itchat.auto_login(hotReload= True)
friends = itchat.get_friends(update= True)

def analyseSex(firends):
sexs = list(map(lambda x:x[‘Sex’],friends[1:]))
counts = list(map(lambda x:x[1],Counter(sexs).items()))
labels = [‘unknow’,‘male’,‘female’]
colors = [‘red’,‘yellowgreen’,‘blue’]
plt.figure(figsize=(8,5), dpi=80)
plt.axes(aspect=1)
plt.pie(counts, #性别统计结果
labels=labels, #性别展示标签
colors=colors, #饼图区域配色
labeldistance = 1.1, #标签距离圆点距离
autopct = ‘%3.1f%%’, #饼图区域文本格式
shadow = False, #饼图是否显示阴影
startangle = 0, #饼图起始角度
pctdistance = 0.6 #饼图区域文本距离圆点距离
)
plt.legend(loc=‘upper right’,)
plt.title(u’%s的微信好友性别组成’ % friends[0][‘NickName’])
plt.show()

def analyseSignature(friends):
signatures = ‘’
emotions = []
pattern = re.compile(“1f\d.+”)
for friend in friends:
signature = friend[‘Signature’]
if(signature != None):
signature = signature.strip().replace(‘span’, ‘’).replace(‘class’, ‘’).replace(‘emoji’, ‘’)
signature = re.sub(r’1f(\d.+)’,’’,signature)
if(len(signature)>0):
nlp = SnowNLP(signature)
emotions.append(nlp.sentiments)
signatures += ’ '.join(jieba.analyse.extract_tags(signature,5))
with open(‘signatures.txt’,‘wt’,encoding=‘utf-8’) as file:
file.write(signatures)

# Sinature WordCloud
back_coloring = np.array(Image.open('m.jpg'))
wordcloud = WordCloud(
    font_path='simfang.ttf',
    background_color="white",
    max_words=1200,
    mask=back_coloring,
    max_font_size=75,
    random_state=45,
    width=960,
    height=720,
    margin=15
)

wordcloud.generate(signatures)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
wordcloud.to_file('signatures.jpg')

# Signature Emotional Judgment
count_good = len(list(filter(lambda x:x>0.66,emotions)))
count_normal = len(list(filter(lambda x:x>=0.33 and x<=0.66,emotions)))
count_bad = len(list(filter(lambda x:x<0.33,emotions)))
labels = [u'负面消极',u'中性',u'正面积极']
values = (count_bad,count_normal,count_good)
plt.rcParams['font.sans-serif'] = ['simHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel(u'情感判断')
plt.ylabel(u'频数')
plt.xticks(range(3),labels)
plt.legend(loc='upper right',)
plt.bar(range(3), values, color = 'rgb')
plt.title(u'%s的微信好友签名信息情感分析' % friends[0]['NickName'])
plt.show()

if name == ‘main’:
analyseSex(friends)
analyseSignature(friends)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值