无法从函数中追加值

2024-06-02 07:53:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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))

Tags: text列表dictsentencetweets情感tweetprint
2条回答

考虑返回一个值

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 : 
        value = "Positive" 

    elif sentiment_dict['compound'] <= - 0.05 : 
        value  = "Negative"

    else : 
        value = "Neutral"

    return value

尝试构建并返回列表:

def sentiment_scores(sentence): 

    local_tweet_sentiment_vader = []
    # 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") 
        local_tweet_sentiment_vader.append("Positive")

    elif sentiment_dict['compound'] <= - 0.05 : 
        print("Negative") 
        local_tweet_sentiment_vader.append("Negative")
    else : 
        print("Neutral") 
        local_tweet_sentiment_vader.append("Neutral")
    return local_tweet_sentiment_vader

打印语句不会出现在列表中

相关问题 更多 >