前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于词典的社交媒体内容的情感分析(Python实现)

基于词典的社交媒体内容的情感分析(Python实现)

作者头像
里克贝斯
发布2021-05-21 16:38:15
1K0
发布2021-05-21 16:38:15
举报
文章被收录于专栏:图灵技术域图灵技术域

之前写了一篇基于NLTK情感预测的文章https://www.omegaxyz.com/2017/12/15/nltk_emotion/?hilite=%27NLTK%27b

情感词典是从微博、新闻、论坛等数据来源的上百万篇情感标注数据当中自动构建的情感极性词典。因为标注包括微博数据,该词典囊括了很多网络用语及非正式简称,对非规范文本也有较高的覆盖率。该情感词典可以用于构建社交媒体情感分析引擎,负面内容发现等应用。

这是一个基于机器学习的已生成的情感词典(txt文档),注意只能预测社交媒体等非规范性文本(文章情感预测精度有误差)

词典下载:https://bosonnlp.com/resources/BosonNLP_sentiment_score.zip

python实现是利用jieba分词预测

Python

代码语言:txt
复制
import time
import jieba
 
emotion_dic = {}
filename = 'BosonNLP_sentiment_score.txt'  # txt文件和当前脚本在同一目录下,所以不用写具体路径
with open(filename, 'rb') as file:
    while True:
        try:
            senList = file.readline().decode('utf-8')
            # print(senList)
            senList = senList[:-1]
            senList = senList.split(' ')
            emotion_dic[senList[0]] = senList[1]
        except IndexError:
            break
 
def get_emotion(score):
    emotion_archive = ['绝望,十分愤怒,对生活不在抱有希望', '难过,失望,抑郁', '有点小难过或者小愤怒', '轻微的难受或者不屑,想得太多啦,洗洗睡觉吧', '生活也就这样吧', '有点小开心或者小激动',
                       '蛮开心的,生活多美好', '喜笑颜开,每天的太阳都是新的,生活充满了希望']
    if score <= -3.9:
        return emotion_archive[0]
    elif -3.9 < score <= -2.5:
        return emotion_archive[1]
    elif -2.5 < score <= -1:
        return emotion_archive[2]
    elif -1 < score <= 0:
        return emotion_archive[3]
    elif 0 <score <= 1:
        return emotion_archive[4]
    elif 1 < score <= 2.5:
        return emotion_archive[5]
    elif 2.5 < score < 3.9:
        return emotion_archive[6]
    else:
        return emotion_archive[7]
 
test = "才拒绝做爱情代罪的羔羊"
 
seg_list = jieba.cut(test, cut_all=True)
string = "/ ".join(seg_list)
string_list = string.split('/')
emotion_index = 0
time.sleep(1)
print("-5分为极端消极,5分为非常高兴")
 
for _ in range(len(string_list)):
    if string_list[_] in emotion_dic:
        emotion_index += float(emotion_dic[string_list[_]])
print(emotion_index)
print(get_emotion(emotion_index))

测试文本来自陈奕迅《爱情转移》中“才拒绝做爱情代罪的羔羊”

结果:

-0.730524151526 轻微的难受或者不屑,想得太多啦,洗洗睡觉吧

网站所有原创代码采用Apache 2.0授权  网站文章采用知识共享许可协议BY-NC-SA4.0授权

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

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

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

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

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