我在dataframedata_tweets['text']
列中编写了一个函数来评估情感:句子中的消极、积极或中立态度,我尝试将输出附加到一个列表中,因为我想将情感添加到原始数据帧中
我的职能:
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# function to print sentiments
# of the sentence.
def sentiment_scores(sentence):
# Create a SentimentIntensityAnalyzer object.
sid_obj = SentimentIntensityAnalyzer()
# polarity_scores method of SentimentIntensityAnalyzer
# oject gives a sentiment dictionary.
# which contains pos, neg, neu, and compound scores.
sentiment_dict = sid_obj.polarity_scores(sentence)
print("Sentence Overall Rated As",end = " ")
# decide sentiment as positive, negative and neutral
if sentiment_dict['compound'] >= 0.05 :
print("Positive")
elif sentiment_dict['compound'] <= - 0.05 :
print("Negative")
else :
print("Neutral")
输出:
Neutral
Neutral
Positive
Neutral
Neutral
下面是我用来附加列表的内容,但是当我打印tweet_sentiment_vader
时,我只得到None
。有人能告诉我为什么不能成功地将值附加到空列表中吗?你知道吗
tweet_sentiment_vader = []
row_count=data_tweets.shape[0]
for i in range(row_count):
sent = data_tweets['text'][i]
tweet_sentiment_vader.append(sentiment_scores(sent))
考虑返回一个值
尝试构建并返回列表:
打印语句不会出现在列表中
相关问题 更多 >
编程相关推荐