I am new to python and I have a dataset that looks like this
I am extracting the reviews from the dataset and trying to apply the VADER tool to check the sentiment weights associated with each review. I am able to successfully retrieve the reviews but unable to apply VADER to each review. This is the code
import nltk
import requirements_elicitation
from nltk.sentiment.vader import SentimentIntensityAnalyzer
c = requirements_elicitation.read_reviews("D:\\Python\\testml\\my-tracks-reviews.csv")
class SentiFind:
def init__(self,review):
self.review = review
for review in c:
review = review.comment
print(review)
sid = SentimentIntensityAnalyzer()
for i in review:
print(i)
ss = sid.polarity_scores(i)
for k in sorted(ss):
print('{0}: {1}, '.format(k, ss[k]), end='')
print()
Sample output:
g
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
r
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
e
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
a
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
t
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
a
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
p
compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
p
I need to customize the labels for each review as well to something like this
"Total weight: {0}, Negative: {1}, Neutral: {2}, Positive: {3}".
The review
that you've defined is a string
, so when you iterate through it, you get each letter:
for i in review:
print(i)
g
r
e
a...
Thus, you'll want the analyzer to go for each review:
sid = SentimentIntensityAnalyzer()
for review in c:
review = review.comment
ss = sid.polarity_scores(review)
total_weight = ss.compound
positive = ss.pos
negative = ss.neg
neutral = ss.neu
print("Total weight: {0}, Negative: {1}, Neutral: {2}, Positive: {3}".format(total_weight, positive, negative, neutral))