【问题标题】:Text sentiment scoring returning <function sentiment_value at ...> instead of int score文本情感评分返回 <function mood_value at ...> 而不是 int score
【发布时间】: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


    【解决方案1】:

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

    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:

    tweets_df['label'] = tweets_df['tidy_tweet'].transform(sentiment_value)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多