用 Python分析朋友圈好友的签名
程序员文章站
2022-10-05 09:19:07
需要用到的第三方库: numpy:本例结合wordcloud使用 jieba:对中文惊进行分词 PIL: 对图像进行处理(本例与wordcloud结合使用) snowlp:对文本信息进行情感判断 wordcloud:生成词云matplotlib:绘制2D图形 效果图 ......
需要用到的第三方库:
numpy:本例结合wordcloud使用
jieba:
pil: 对图像进行处理(本例与wordcloud结合使用)
snowlp:
wordcloud:
matplotlib:绘制2d图形
# -*- coding: utf-8 -*- """ 朋友圈朋友签名的词云生成以及 签名情感分析 想要学习python?python学习交流群:984632579满足你的需求,资料都已经上传群文件,可以自行下载! """ import re,jieba,itchat import jieba.analyse import numpy as np from pil import image from snownlp import snownlp from wordcloud import wordcloud import matplotlib.pyplot as plt itchat.auto_login(hotreload=true) friends = itchat.get_friends(update=true) def analysesignature(friends): signatures = '' emotions = [] 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) # 朋友圈朋友签名的词云相关属性设置 back_coloring = np.array(image.open('alice_color.png')) wordcloud = wordcloud( font_path='simfang.ttf', background_color="white", max_words=1200, mask=back_coloring, max_font_size=75, random_state=45, width=1250, height=1000, 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'情感判断')#x轴 plt.ylabel(u'频数')#y轴 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() analysesignature(friends)
效果图
上一篇: 数据库更新 新增 修改 删除
下一篇: 行为型模式:状态模式