当前位置:   article > 正文

python文本情感分析代码_6行代码的超简单语言情感分析:由Python的Vader情感库实现,超级,使用,vaderSentiment...

python vader

废话不多说,上码!

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()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/893888
推荐阅读
相关标签
  

闽ICP备14008679号