Невозможно добавить значения из функции - PullRequest
0 голосов
/ 11 июня 2019

Я пишу функцию для оценки настроений в столбце 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 голосов
/ 11 июня 2019

Учитывая возвращаемое значение

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
0 голосов
/ 11 июня 2019

Попробуйте создать и вернуть вместо этого список:

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

Операторы печати не будут добавлены в список

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...