首页 > 解决方案 > NLTK 极性分数

问题描述

我想知道我是否可以让 Sentimentia.polarity_scores() 函数只打印负数、正数,并且我可以摆脱中性和复合结果

    i = 0
    while i < len(Replaced_Data):
        Sentimentia = SentimentIntensityAnalyzer()
        print(Sentimentia.polarity_scores(data['Clean_TweetText'][i]))
        i = i + 1

output >>> {'neg': 0.214, 'neu': 0.534, 'pos': 0.252, 'compound': 0.25}

desired output >>> {'neg': 0.214, 'pos': 0.188}

标签: pythonnltksentiment-analysis

解决方案


可以使用Dictionary Comprehensions从字典中过滤 'neg' 和 'pos'

ss = Sentimentia.polarity_scores(data['Clean_TweetText'][i])     
print({e:ss[e] for e in ss if e in ['neg','pos']})


推荐阅读