前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用 Python分析朋友圈好友的签名

用 Python分析朋友圈好友的签名

作者头像
py3study
发布2020-01-17 13:23:33
3340
发布2020-01-17 13:23:33
举报
文章被收录于专栏:python3python3

需要用到的第三方库:

numpy:本例结合wordcloud使用

jieba对中文惊进行分词

PIL: 对图像进行处理(本例与wordcloud结合使用)

snowlp对文本信息进行情感判断

wordcloud生成词云 matplotlib:绘制2D图形

代码语言:javascript
复制
# -*- 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)

效果图

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档