【问题标题】:How to print valence score for each lexicon in vader?如何打印 vader 中每个词典的价分数?
【发布时间】:2021-01-23 23:23:15
【问题描述】:

我正在尝试使用 vader 打印句子中每个词典(单词)的价分数,但我在此过程中感到困惑。我可以使用 vader 将句子中的单词分类为正面、负面和中性。我也想打印价分数。如何解决这个问题?

sid = SentimentIntensityAnalyzer()
pos_word_list=[]
neu_word_list=[]
neg_word_list=[]

for word in tokenized_sentence:
    if (sid.polarity_scores(word)['compound']) >= 0.1:
        pos_word_list.append(word)
        sid.score_valence(word)
    elif (sid.polarity_scores(word)['compound']) <= -0.1:
        neg_word_list.append(word)
    else:
      neu_word_list.append(word)                

print('Positive:',pos_word_list)        
print('Neutral:',neu_word_list)    
print('Negative:',neg_word_list) 
score = sid.polarity_scores(sentence)
print('\nScores:', score)

这是我看到的代码here。我希望它打印为

Positive: ['happy', 1.3]
Neutral: ['paper', 0, 'too', 0, 'much', 0]
Negative: ['missed', -1.2, 'stupid', -1.9]

Scores: {'neg': 0.491, 'neu': 0.334, 'pos': 0.175, 'compound': -0.5848}

因此在句子中显示“快乐”这个词的效价得分为 1.3。

【问题讨论】:

    标签: python nlp nltk sentiment-analysis vader


    【解决方案1】:

    如果您能提供您在代码中使用的句子,那就太好了。但是,我提供了一个句子,您可以用您的句子替换它。

    看看我的源代码:

    import nltk
    from nltk.tokenize import word_tokenize, RegexpTokenizer
    from nltk.sentiment.vader import SentimentIntensityAnalyzer
     
    Analyzer = SentimentIntensityAnalyzer()
     
    sentence = 'Make sure you stay happy and less doubtful'
     
    tokenized_sentence = nltk.word_tokenize(sentence)
    pos_word_list=[]
    neu_word_list=[]
    neg_word_list=[]
     
    for word in tokenized_sentence:
        if (Analyzer.polarity_scores(word)['compound']) >= 0.1:
            pos_word_list.append(word)
            pos_word_list.append(Analyzer.polarity_scores(word)['compound'])
        elif (Analyzer.polarity_scores(word)['compound']) <= -0.1:
            neg_word_list.append(word)
            neg_word_list.append(Analyzer.polarity_scores(word)['compound'])
        else:
            neu_word_list.append(word)
            neu_word_list.append(Analyzer.polarity_scores(word)['compound'])
    
    print('Positive:',pos_word_list)
    print('Neutral:',neu_word_list)
    print('Negative:',neg_word_list) 
    score = Analyzer.polarity_scores(sentence)
    print('\nScores:', score)
    

    根据我从您的问题中了解到的情况,我猜您可能正在寻找这样的输出。如果不是,请告诉我。

    输出:

    Positive: ['sure', 0.3182, 'happy', 0.5719]
    Neutral: ['Make', 0.0, 'you', 0.0, 'stay', 0.0, 'and', 0.0, 'less', 0.0]
    Negative: ['doubtful', -0.34]
    
    Scores: {'neg': 0.161, 'neu': 0.381, 'pos': 0.458, 'compound': 0.5984}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2018-12-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      相关资源
      最近更新 更多