Reputation: 1100
Is there a commonly used method for sorting multiple Vader Sentiment Analysis Results in Dictionary
I am trying to sort by 'compound' Vader Sentiment Analysis Results in the review Dictionary.
I just started learning nlp and Sentiment Analysis and got my first project to 95% so far been learning books, tutorials and here.
I was hoping to sort closest scores 5 overall sentiments scores to a particular one in a large set of multiple dictionary results.
this what i tried Might be also due to printing it being a string I assume i need to convert this to string or object, just not sure next steps.I was think I might need to loop through dictionary or convert the dictionary. Any pointers would be greatly appreciated.
newlist = sorted(review, key=lambda k: k['compound'])
I also tried
from operator import itemgetter
newlist = sorted(review, key=itemgetter('compound'))
The Vader results are printed from dictionary.
s = pprint.pformat(review)
print(s)
This is the format of the results which i think is standard output
'america-reviews': "Overall sentiment dictionary is : {'neg': 0.051, 'neu': "
"0.632, 'pos': 0.316, 'compound': 1.0}, sentence was rated "
'as 5.1% Negative, sentence was rated as 63.2% Neutral, '
'sentence was rated as 31.6% Positive, Sentence Overall '
'Rated As Positive',
'amygrant-reviews': "Overall sentiment dictionary is : {'neg': 0.022, 'neu': "
"0.734, 'pos': 0.244, 'compound': 0.9998}, sentence was "
'rated as 2.1999999999999997% Negative, sentence was '
'rated as 73.4% Neutral, sentence was rated as 24.4% '
'Positive, Sentence Overall Rated As Positive',
'andygarcia-reviews': "Overall sentiment dictionary is : {'neg': 0.0, 'neu': "
"0.955, 'pos': 0.045, 'compound': 0.8419}, sentence was "
'rated as 0.0% Negative, sentence was rated as 95.5% '
'Neutral, sentence was rated as 4.5% Positive, Sentence '
'Overall Rated As Positive',
'annemurray-reviews': "Overall sentiment dictionary is : {'neg': 0.02, 'neu': "
"0.769, 'pos': 0.211, 'compound': 0.9986}, sentence was "
'rated as 2.0% Negative, sentence was rated as 76.9% '
'Neutral, sentence was rated as 21.099999999999998% '
'Positive, Sentence Overall Rated As Positive',
'annielennox-reviews': "Overall sentiment dictionary is : {'neg': 0.03, "
"'neu': 0.717, 'pos': 0.254, 'compound': 0.9999}, "
'sentence was rated as 3.0% Negative, sentence was '
'rated as 71.7% Neutral, sentence was rated as 25.4% '
'Positive, Sentence Overall Rated As Positive',
'artgarfunkel-reviews': "Overall sentiment dictionary is : {'neg': 0.056, "
"'neu': 0.642, 'pos': 0.302, 'compound': 1.0}, "
'sentence was rated as 5.6000000000000005% Negative, '
'sentence was rated as 64.2% Neutral, sentence was '
'rated as 30.2% Positive, Sentence Overall Rated As '
'Positive',
'bangles-reviews': "Overall sentiment dictionary is : {'neg': 0.054, 'neu': "
"0.733, 'pos': 0.213, 'compound': 0.9998}, sentence was "
'rated as 5.4% Negative, sentence was rated as 73.3% '
'Neutral, sentence was rated as 21.3% Positive, Sentence '
'Overall Rated As Positive',
'barbrastriesand-reviews': "Overall sentiment dictionary is : {'neg': 0.014, "
"'neu': 0.815, 'pos': 0.171, 'compound': 0.9982}, "
'sentence was rated as 1.4000000000000001% '
'Negative, sentence was rated as 81.5% Neutral, '
'sentence was rated as 17.1% Positive, Sentence '
'Overall Rated As Positive',
'barrymanilow-reviews': "Overall sentiment dictionary is : {'neg': 0.041, "
"'neu': 0.647, 'pos': 0.313, 'compound': 1.0}, "
'sentence was rated as 4.1000000000000005% Negative, '
'sentence was rated as 64.7% Neutral, sentence was '
'rated as 31.3% Positive, Sentence Overall Rated As '
'Positive',
'beachboys-reviews': "Overall sentiment dictionary is : {'neg': 0.016, 'neu': "
"0.906, 'pos': 0.078, 'compound': 0.945}, sentence was "
'rated as 1.6% Negative, sentence was rated as '
'90.60000000000001% Neutral, sentence was rated as 7.8% '
'Positive, Sentence Overall Rated As Positive',
'belindacarlisle-reviews': "Overall sentiment dictionary is : {'neg': 0.046, "
"'neu': 0.756, 'pos': 0.197, 'compound': 0.9987}, "
'sentence was rated as 4.6% Negative, sentence was '
'rated as 75.6% Neutral, sentence was rated as '
'19.7% Positive, Sentence Overall Rated As '
'Positive',
'bernadettepeters-reviews': "Overall sentiment dictionary is : {'neg': 0.02, "
"'neu': 0.753, 'pos': 0.227, 'compound': 0.9992}, "
'sentence was rated as 2.0% Negative, sentence '
'was rated as 75.3% Neutral, sentence was rated '
'as 22.7% Positive, Sentence Overall Rated As '
'Positive',
'bethhart-reviews': "Overall sentiment dictionary is : {'neg': 0.041, 'neu': "
"0.592, 'pos': 0.366, 'compound': 1.0}, sentence was "
'rated as 4.1000000000000005% Negative, sentence was '
'rated as 59.199999999999996% Neutral, sentence was rated '
'as 36.6% Positive, Sentence Overall Rated As Positive',
'bettemidler-reviews': "Overall sentiment dictionary is : {'neg': 0.043, "
"'neu': 0.635, 'pos': 0.322, 'compound': 0.9999}, "
'sentence was rated as 4.3% Negative, sentence was '
'rated as 63.5% Neutral, sentence was rated as 32.2% '
'Positive, Sentence Overall Rated As Positive',
'bjork-reviews': "Overall sentiment dictionary is : {'neg': 0.042, 'neu': "
"0.696, 'pos': 0.262, 'compound': 1.0}, sentence was rated "
'as 4.2% Negative, sentence was rated as 69.6% Neutral, '
'sentence was rated as 26.200000000000003% Positive, '
'Sentence Overall Rated As Positive',
'bluemangroup-reviews': "Overall sentiment dictionary is : {'neg': 0.047, "
"'neu': 0.726, 'pos': 0.227, 'compound': 0.9999}, "
'sentence was rated as 4.7% Negative, sentence was '
'rated as 72.6% Neutral, sentence was rated as 22.7% '
'Positive, Sentence Overall Rated As Positive',
'bluetravelers-reviews': "Overall sentiment dictionary is : {'neg': 0.0, "
"'neu': 0.914, 'pos': 0.086, 'compound': 0.9455}, "
'sentence was rated as 0.0% Negative, sentence was '
'rated as 91.4% Neutral, sentence was rated as 8.6% '
'Positive, Sentence Overall Rated As Positive',
'bobbyvinton-reviews': "Overall sentiment dictionary is : {'neg': 0.0, 'neu': "
"0.928, 'pos': 0.072, 'compound': 0.9501}, sentence "
'was rated as 0.0% Negative, sentence was rated as '
'92.80000000000001% Neutral, sentence was rated as '
'7.199999999999999% Positive, Sentence Overall Rated '
'As Positive',
'bonnieRatt-reviews': "Overall sentiment dictionary is : {'neg': 0.034, "
"'neu': 0.612, 'pos': 0.354, 'compound': 0.9999}, "
'sentence was rated as 3.4000000000000004% Negative, '
'sentence was rated as 61.199999999999996% Neutral, '
'sentence was rated as 35.4% Positive, Sentence Overall '
'Rated As Positive',
'boygeorge-reviews': "Overall sentiment dictionary is : {'neg': 0.039, 'neu': "
"0.884, 'pos': 0.076, 'compound': 0.9217}, sentence was "
'rated as 3.9% Negative, sentence was rated as 88.4% '
'Neutral, sentence was rated as 7.6% Positive, Sentence '
'Overall Rated As Positive',
'brianlittrell-reviews': "Overall sentiment dictionary is : {'neg': 0.052, "
"'neu': 0.873, 'pos': 0.074, 'compound': 0.8203}, "
'sentence was rated as 5.2% Negative, sentence was '
'rated as 87.3% Neutral, sentence was rated as '
'7.3999999999999995% Positive, Sentence Overall '
'Rated As Positive',
'briansetzerorchestra-reviews': "Overall sentiment dictionary is : {'neg': "
"0.046, 'neu': 0.646, 'pos': 0.308, "
"'compound': 1.0}, sentence was rated as 4.6% "
'Negative, sentence was rated as '
'64.60000000000001% Neutral, sentence was '
'rated as 30.8% Positive, Sentence Overall '
'Rated As Positive',
'brianwilson-reviews': "Overall sentiment dictionary is : {'neg': 0.042, "
"'neu': 0.647, 'pos': 0.311, 'compound': 1.0}, "
'sentence was rated as 4.2% Negative, sentence was '
'rated as 64.7% Neutral, sentence was rated as 31.1% '
'Positive, Sentence Overall Rated As Positive',
'brucehornsby-reviews': "Overall sentiment dictionary is : {'neg': 0.0, "
"'neu': 0.928, 'pos': 0.072, 'compound': 0.9196}, "
'sentence was rated as 0.0% Negative, sentence was '
'rated as 92.80000000000001% Neutral, sentence was '
'rated as 7.199999999999999% Positive, Sentence '
'Overall Rated As Positive',
'bryanadams-reviews': "Overall sentiment dictionary is : {'neg': 0.008, "
"'neu': 0.933, 'pos': 0.059, 'compound': 0.9028}, "
'sentence was rated as 0.8% Negative, sentence was '
'rated as 93.30000000000001% Neutral, sentence was '
'rated as 5.8999999999999995% Positive, Sentence '
'Overall Rated As Positive',
Upvotes: 0
Views: 269
Reputation: 488
The direct sorting doesn't work because your dictionary values are strings, not dictionaries or lists. To sort by compound you need to extract it's value at first. Here is a simple example of how you can do it by using regex and lambda:
import re
def extract_compound(item):
# define regex to extract compound value
rgxp = r'(?<=\'compound\':\s)\d\.\d+'
compound = re.search(rgxp, item)
compound = compound.group(0)
return float(compound)
review_sorted = {key: value for key, value in sorted(
review.items(), key=lambda item: extract_compound(item[1]), reverse=True)
Upvotes: 1