I am trying to print the valence score for each lexicon (word) in a sentence using vader, but I am getting confused in the process. I am able to sort the words in a sentence as positive, negative and neutral using vader. I want to print the valence score as well. How to approach this?
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)
This is the code I saw here. I want it to print as
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}
thus showing the word 'happy' having 1.3 valence score in the sentence.
It would be great if you could provide the sentence you had used for your code. However, I have provided a sentence which you can replace with your sentence.
Have a look at my source code:
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)
From what I perceive from your question, I guess you might be looking for output such as this. Let me know, if otherwise.
OUTPUT:
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}