第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python文本情感分析代码_6行代码的超简单语言情感分析:由Python的Vader情感库实现

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

时间:2023-08-13 12:46:22

相关推荐

python文本情感分析代码_6行代码的超简单语言情感分析:由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()

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。