6行代码 超级简单 语言情感分析 - 使用Python的vaderSentiment库实现

废话不多说,上码!

pip install vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
sentences = ['I like you just so so','I like you a little','I like you','I like you very much','I hate you just so so','I hate you a little','I hate you','I hate you very much',]
analyzer = SentimentIntensityAnalyzer()
for sentence in sentences:
    vs = analyzer.polarity_scores(sentence)
    print("{:-<65} {}".format(sentence, str(vs)))

输出结果的值为:消极negative、中性neutral、积极positive、复合情绪compound

注:以上代码保存为.py文件即可执行,vaderSentiment只支持英文的文本进行情感分析,不支持中文!不支持中文!不支持中文!

 

详细的测试代码:

# -*- coding: utf-8 -*-


def run():
    from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
    #note: depending on how you installed (e.g., using source code download versus pip install), you may need to import like this:
    #from vaderSentiment import SentimentIntensityAnalyzer
    # --- examples -------
    sentences = [
                'I like you just so so',
                'I like you a little',
                'I like you',
                'I like you very much',
                'I hate you just so so',
                'I hate you a little',
                'I hate you',
                'I hate you very much',
                ]

    analyzer = SentimentIntensityAnalyzer()
    for sentence in sentences:
        vs = analyzer.polarity_scores(sentence)
        print("{:-<65} {}".format(sentence, str(vs)))
    
    # 输出结果(# 消极negative、中性neutral、积极positive、复合情绪compound):
    # I like you just so so-------------------------------------------- {'neg': 0.0, 'neu': 0.667, 'pos': 0.333, 'compound': 0.3612}
    # I like you a little---------------------------------------------- {'neg': 0.0, 'neu': 0.615, 'pos': 0.385, 'compound': 0.3612}
    # I like you------------------------------------------------------- {'neg': 0.0, 'neu': 0.444, 'pos': 0.556, 'compound': 0.3612}
    # I like you very much--------------------------------------------- {'neg': 0.0, 'neu': 0.615, 'pos': 0.385, 'compound': 0.3612}
    # I hate you just so so-------------------------------------------- {'neg': 0.425, 'neu': 0.575, 'pos': 0.0, 'compound': -0.5719}
    # I hate you a little---------------------------------------------- {'neg': 0.481, 'neu': 0.519, 'pos': 0.0, 'compound': -0.5719}
    # I hate you------------------------------------------------------- {'neg': 0.649, 'neu': 0.351, 'pos': 0.0, 'compound': -0.5719}
    # I hate you very much--------------------------------------------- {'neg': 0.481, 'neu': 0.519, 'pos': 0.0, 'compound': -0.5719}


if __name__ == "__main__":
    run()

 

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值