赞
踩
废话不多说,上码!
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()

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。