python分析微信好友签名分析

上一篇中我们已经得到了所有微信好友,并且分析了微信好友的地域分布。 我们同时也能得到微信好友的所有签名,对于签名我们能够通过分词,分析出使用频率比较高的词,为了直观的展现,也能够生成对应的词云; 甚至我们能够分析一下所有微信好友签名的情绪分布。

当前其实有不少工具能够实现我们的目的, 在例子中, 我采用了jieba来进行分词, 用wordcloud来生成对应的词云,用snownlp来分析对应的情绪。

关于这三种工作,不做详细的介绍了,简单的列一下:

jieba

“结巴”中文分词:做最好的 Python 中文分词组件

“Jieba” (Chinese for “to stutter”) Chinese text segmentation: built to be the best Python Chinese word segmentation module.

GitHub: https://github.com/fxsjy/jieba


wordcloud

  1. github:https://github.com/amueller/word_cloud   
  2. 官方地址:https://amueller.github.io/word_cloud/   

snownlp

SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了[TextBlob](https://github.com/sloria/TextBlob)的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和TextBlob不同的是,这里没有用NLTK,所有的算法都是自己实现的,并且自带了一些训练好的字典。注意本程序都是处理的unicode编码,所以使用时请自行decode成unicode。

具体实现:

for friend in my_friends:
    signature = str(friend.signature).strip().replace("span", "").replace("class", "").replace("emoji", "")
    signature_file.writelines(signature)
    rep = re.compile("1f\d+\w*|[<>/=]")
    signature = rep.sub("", signature)
    siglist.append(signature)

这样就将所有的签名都保存在了一个名为siglist的列表中, 然后保存成一份文件

text = "".join(siglist)
with open('text.txt', 'w') as f:
    f.write(text)
    f.close()

之后我们就能通过调用wordcloud来生成词云:

# 绘制词云
def draw_wordcloud():
    # 读入一个txt文件
    comment_text = open('text.txt', 'r').read()
    # 结巴分词,生成字符串
    cut_text = " ".join(jieba.cut(comment_text))
    color_mask = imread("background.png")  # 读取背景图片
    cloud = WordCloud(
        # 设置字体,不指定就会出现乱码
        font_path="HYQiHei-50S.ttf",
        # 设置背景色
        background_color='white',
        # 词云形状
        mask=color_mask,
        # 允许最大词汇
        max_words=2000,
        # 最大号字体
        max_font_size=40
    )
    word_cloud = cloud.generate(cut_text)  # 产生词云
    # word_cloud.to_file("wordcloud.jpg")  #保存图片
    #  显示词云图片
    plt.imshow(word_cloud)
    plt.axis('off')
    plt.show()

这边需要自己先下载一个背景图片,用来作为词云的模版,此外如果运行的时候找不到对应字体的话,下载字体到对应文件夹就行。最终的效果如下:


接下去进行情绪分析,在获取签名的时候,能够同时进行情绪的分析:

signature_emotion = re.sub(r'1f(\d.+)', '', signature)
    if (len(signature_emotion) > 0):
        nlp = SnowNLP(signature)
        emotions.append(nlp.sentiments)

然后根据情绪的得分,绘制一个柱状图:

def analyseEmotion(emotions):
    # 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'微信好友签名信息情感分析')
    plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值