文本情感评分返回<function sentiment_value =“” at =“” ... =“”>而不是整数得分

时间:2019-05-07 02:16:07

标签: python sentiment-analysis tweets

我正尝试在我的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

然后我尝试将新的sentiment_value函数应用于我要得分的列

# 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>

我对此很陌生,感谢任何人都能提供的帮助!

1 个答案:

答案 0 :(得分:0)

那是因为您没有调用该函数。您应该这样做:

Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly
Set-Cookie: foo=Hello; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly
Set-Cookie: bar=World; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly

您还可以使用for row in tweets_df['tidy_tweet']: label.append(sentiment_value(row)) # note that row is now passed to sentiment_value tweets_df['label'] = label 理解:

list

甚至tweets_df['label'] = [sentiment_value(tweet) for tweet in tweets_df['tidy_tweet']

transform