Python - 微信好友个性签名情感分析( 基于Python开源库snownlp )

版权声明:转载请注明出处 https://blog.csdn.net/qq_42292831/article/details/88932731

配置与简介https://blog.csdn.net/qq_42292831/article/details/88932177

本文源码下载https://github.com/Hirehop/Python/tree/master/Wechat


函数源码(参数frinends为itchat.get_friends()的返回值

def analyseSignature(friends):
    moodsThreeParts = [0,0,0]    #存储情感分类

    test = re.compile("<span.*?</span>")    #非贪婪匹配
    sigs_raw = list(map(lambda x:test.sub('',x["Signature"]),friends[:]))
    sigs_deal = []
    for data in sigs_raw:
        if data!='':    #''和None不同!
            sigs_deal.append(data)
        else:
            sigs_deal.append('###')

    #对获得的signatures进行情感判定
    for data in sigs_deal:
        mood = snownlp.SnowNLP(data).sentiments
        if mood<0.5:
            moodsThreeParts[0]+=1
        elif mood==0.5:
            moodsThreeParts[1]+=1
        else:
            moodsThreeParts[2]+=1
    return moodsThreeParts

def dealSex(friends):
    print("Dealing with WeChat-Friends-Sexes: \n")
    analyseSex(friends)

def dealSignatures(friends):
    print("Dealing with WeChat-Friends-Sinatures: \n")
    plt.pie(
        analyseSignature(friends),
        [0,0,0.1],
        ["Negative","Indifferent","Positive"],
        ["red","yellowgreen","lightskyblue"],
        radius=0.8,
        autopct="%.2f%%",
        shadow = False
    )
    plt.title("%s的微信好友签名情感倾向" % friends[0]["NickName"])
    plt.legend(loc="upper right")
    judge = input("是否需要存储该图片?(y/n) :")
    if judge=='y' or 'Y':
        plt.savefig("f:Geclipse的微信好友性别比例.png",dpi=500)
        print("存储成功( F:/ )")
    else:
        print("存储取消! \n")
    plt.show()

注意事项

1> 正则表达式的匹配问题(重要!)

result = re.compile(REGEX):匹配REGEX

result.sub(A,B):将参数B的值替换为参数A的值

2> ''和None不同!

猜你喜欢

转载自blog.csdn.net/qq_42292831/article/details/88932731