0

Initially i have list of lists like:

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']]

I wanted to analyse sentiments for each sentence within a document individually, so for this i used vader sentiment analyser from nltk, and i did something like this:

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)

Here is the output:

 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}

The output is stored in the dictionary 'ss'.From this output i want to calculate the average of only the compound scores for each document.

so for example i want to calculate the average score for the last document, i will have to add compound score of 1st and 2nd sentence and then divide by length of document i.e -0.4416-0.5256/2 = -0.4836

How can this be done ?

Ben Blank
  • 54,908
  • 28
  • 127
  • 156
explorer_x
  • 149
  • 3
  • 11
  • You gather the scores for the sentences in each document, sum that list, and divide by its length. It appears that you haven't yet coded any step of this; where are you stuck? – Prune Jul 19 '18 at 18:13

1 Answers1

0
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)
JISHAD A.V
  • 361
  • 1
  • 9