计算具有相同"键名"的两个不同字典的两个值的平均值?



最初我有一个列表列表,比如:

mult_sentences = [['Sounds like he was bound by some ridiculous systems that 
the company at large needs to address.',
'                But he didn’t do anything to go above and beyond.'],
['He did absolutely nothing to help me.',
'He submitted a report and my problem was never resolved.'],
["I really don't care now.", 'Very disappointed']]

我想分析文档中每一句话的情感,因此我使用了nltk的vader情感分析器,我做了这样的事情:

from nltk.sentiment.vader import SentimentIntensityAnalyzer
for i,sents in enumerate(mult_sentences):
sia = SentimentIntensityAnalyzer()
for sent in sents:
print(sent)
ss = sia.polarity_scores(sent) #SS IS A DICTIONARY,STORING ALL THE SCORES IN A DICTIONARY.
print(ss)
print('*'*50)

这是输出:

Sounds like he was bound by some ridiculous systems that the company at 
large needs to address.        
{'neg': 0.125, 'neu': 0.75, 'pos': 0.125, 'compound': 0.0}
But he didn’t do anything to go above and beyond.
{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
**************************************************
He did absolutely nothing to help me.
{'neg': 0.296, 'neu': 0.704, 'pos': 0.0, 'compound': -0.3657}
He submitted a report and my problem was never resolved.
{'neg': 0.376, 'neu': 0.624, 'pos': 0.0, 'compound': -0.497}
**************************************************
I really don't care now.
{'neg': 0.492, 'neu': 0.508, 'pos': 0.0, 'compound': -0.4416}
Very disappointed
{'neg': 0.772, 'neu': 0.228, 'pos': 0.0, 'compound': -0.5256}

输出存储在字典'ss'中。根据此输出,我只想计算每个文档的复合分数的average

因此,例如,我想计算最后一个文档平均分数,我必须将第一句和第二句的复合分数相加,然后除以文档长度,即-0.4416-0.5256/2=-0.4836

如何做到这一点?

from nltk.sentiment.vader import SentimentIntensityAnalyzer
for i,sents in enumerate(mult_sentences):
sia = SentimentIntensityAnalyzer()
compound_sum = 0
for sent in sents:
print(sent)
ss = sia.polarity_scores(sent) #SS IS A DICTIONARY,STORING ALL THE SCORES IN A DICTIONARY.
compound_sum = compound_sum + ss['compound']
print(ss)
average_score = compound_sum / len(sents)
print('average_score: ', average_score)
print('*'*50)

最新更新