当前位置:   article > 正文

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

vadersentiment

废话不多说,上码!

pip install vaderSentiment
  1. from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
  2. 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',]
  3. analyzer = SentimentIntensityAnalyzer()
  4. for sentence in sentences:
  5. vs = analyzer.polarity_scores(sentence)
  6. print("{:-<65} {}".format(sentence, str(vs)))

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

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

 

详细的测试代码:

  1. # -*- coding: utf-8 -*-
  2. def run():
  3. from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
  4. #note: depending on how you installed (e.g., using source code download versus pip install), you may need to import like this:
  5. #from vaderSentiment import SentimentIntensityAnalyzer
  6. # --- examples -------
  7. sentences = [
  8. 'I like you just so so',
  9. 'I like you a little',
  10. 'I like you',
  11. 'I like you very much',
  12. 'I hate you just so so',
  13. 'I hate you a little',
  14. 'I hate you',
  15. 'I hate you very much',
  16. ]
  17. analyzer = SentimentIntensityAnalyzer()
  18. for sentence in sentences:
  19. vs = analyzer.polarity_scores(sentence)
  20. print("{:-<65} {}".format(sentence, str(vs)))
  21. # 输出结果(# 消极negative、中性neutral、积极positive、复合情绪compound):
  22. # I like you just so so-------------------------------------------- {'neg': 0.0, 'neu': 0.667, 'pos': 0.333, 'compound': 0.3612}
  23. # I like you a little---------------------------------------------- {'neg': 0.0, 'neu': 0.615, 'pos': 0.385, 'compound': 0.3612}
  24. # I like you------------------------------------------------------- {'neg': 0.0, 'neu': 0.444, 'pos': 0.556, 'compound': 0.3612}
  25. # I like you very much--------------------------------------------- {'neg': 0.0, 'neu': 0.615, 'pos': 0.385, 'compound': 0.3612}
  26. # I hate you just so so-------------------------------------------- {'neg': 0.425, 'neu': 0.575, 'pos': 0.0, 'compound': -0.5719}
  27. # I hate you a little---------------------------------------------- {'neg': 0.481, 'neu': 0.519, 'pos': 0.0, 'compound': -0.5719}
  28. # I hate you------------------------------------------------------- {'neg': 0.649, 'neu': 0.351, 'pos': 0.0, 'compound': -0.5719}
  29. # I hate you very much--------------------------------------------- {'neg': 0.481, 'neu': 0.519, 'pos': 0.0, 'compound': -0.5719}
  30. if __name__ == "__main__":
  31. run()

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号