无法从函数附加值

时间:2019-06-11 08:22:29

标签: python list

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

2 个答案:

答案 0 :(得分:0)

尝试构建并返回列表:

TrackableHit hit;
TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal;

if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
{
    Vector3 normal = hit.Pose.up;
}

打印语句不会被添加到列表中

答案 1 :(得分:0)

考虑返回值

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