NLP(IV):使用VADER进行情感分析
安装VADER
使用pip安装即可。
pip install vaderSentiment
计算文本情感积极与消极的比率
以下的代码返回积极的推文列表以及消极的推文列表,以及所有推文的平均指数。
import nltk
# need to download 'stopwords' before using it.
nltk.download('stopwords')
from nltk.corpus import stopwords
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from nltk.tokenize import word_tokenize
import string
def computeSentimentOfSentences(sentenceData):
# Input: a list of sentences from tweets
# Output: a list of sentences from positive tweets, average compound from all the input sentences
sid_obj = SentimentIntensityAnalyzer()
pos_sents = []
neg_sents = []
compound_sum = 0.0
for sentence in sentenceData:
sentiment_dict = sid_obj.polarity_scores(sentence)
compound_sum += sentiment_dict['compound']
if sentiment_dict['compound'] >= 0.05:
pos_sents.append(sentence)
if sentiment_dict