以下是使用Python编写的示例代码,使用textblob和VADER进行情感分析:

from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

# 定义待分析的短句列表
sentences = ["I love this movie", "I hate this book", "The weather is great today"]

# 使用textblob进行情感分析
for sentence in sentences:
    blob = TextBlob(sentence)
    sentiment = blob.sentiment.polarity
    print("TextBlob sentiment analysis result for '{}': {}".format(sentence, sentiment))

# 使用VADER进行情感分析
analyzer = SentimentIntensityAnalyzer()
for sentence in sentences:
    scores = analyzer.polarity_scores(sentence)
    sentiment = scores['compound']
    print("VADER sentiment analysis result for '{}': {}".format(sentence, sentiment))

输出结果如下:

TextBlob sentiment analysis result for 'I love this movie': 0.5
TextBlob sentiment analysis result for 'I hate this book': -0.8
TextBlob sentiment analysis result for 'The weather is great today': 0.8
VADER sentiment analysis result for 'I love this movie': 0.6696
VADER sentiment analysis result for 'I hate this book': -0.5423
VADER sentiment analysis result for 'The weather is great today': 0.6588

其中,TextBlob和VADER都返回了一个在区间【-1,1】内的情感分数,表示负面到正面的程度。对于TextBlob,0表示中性情感;对于VADER,0表示中等情感强度。在这个示例中,我们只使用了VADER的compound分数,即综合考虑了正负面情感和情感强度的得分。如果需要更详细的情感分析结果,可以查看VADER返回的其他分数


原文地址: https://www.cveoy.top/t/topic/fIYc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录