【发布时间】:2019-05-07 02:16:07
【问题描述】:
我正在尝试在我的 Twitter 数据框中添加一个带有情绪分数的列。
我尝试了下面的代码,但我不断得到一个不是 1、0 或 -1 int 的输出。正如我所期待的那样
label =[ ]
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
def sentiment_value(text):
analyser = SentimentIntensityAnalyzer()
result = analyser.polarity_scores(text)
score = result['compound']
if score >= 0.05:
return 1
elif (score > -0.05) and (score < 0.05):
return 0
else:
return -1
然后我做了一个小测试,看看这是否有效,它似乎是:
# Input:
sample = tweets_df['tidy_tweet'][10]
print(sample)
print('Sentiment: ')
print(sentiment_value(sample))
# Output:
that interest take seen cours approv through thi process wonder which one came core team shot down howev origin wonder what possibl could exist cours which there mani
Sentiment:
1
然后我尝试将新的情感值函数应用于我要评分的列
# I have named my clean tweets 'tidy_tweet'
# @handels,special characters, numbers, punctuations, short words have been removed
# Tweets have been tokenizen and stemmed
for row in tweets_df['tidy_tweet']:
label.append(sentiment_value)
tweets_df['label'] = label
当我调用新列时,我期望得到一个情绪分数,例如:
tweets_df['label'].head()
0 1
1 0
2 -1
3 0
4 -1
但我实际得到的是:
0 <function sentiment_value at 0x10284b1e0>
1 <function sentiment_value at 0x10284b1e0>
2 <function sentiment_value at 0x10284b1e0>
3 <function sentiment_value at 0x10284b1e0>
4 <function sentiment_value at 0x10284b1e0>
我对此很陌生,感谢任何人提供的任何帮助!
【问题讨论】:
标签: python sentiment-analysis tweets