Skip to content

Instantly share code, notes, and snippets.

@venkat9214
Created September 22, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save venkat9214/28f969c5047a2eaa9e92149f29b2b916 to your computer and use it in GitHub Desktop.
Save venkat9214/28f969c5047a2eaa9e92149f29b2b916 to your computer and use it in GitHub Desktop.
import csv
import numpy as np
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from nltk import tokenize
name = []
review = []
sid = SentimentIntensityAnalyzer()
target = open("output_1.csv", 'w')
target.write("Name")
target.write(",")
target.write("Review")
target.write(",")
target.write("compound")
target.write(",")
target.write("positive")
target.write(",")
target.write("neutral")
target.write(",")
target.write("negative")
target.write("\n")
with open('daa.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
name = row['key']
lines_list = tokenize.sent_tokenize(row['value'])
pos_rate = []
neu_rate = []
neg_rate = []
compound_rate = []
for sentence in lines_list:
ss = sid.polarity_scores(sentence)
compound_rate = ss['compound']
pos_rate = ss['pos']
neu_rate = ss['neu']
neg_rate = ss['neg']
compound = np.mean(compound_rate)
pos = np.mean(pos_rate)
neg = np.mean(neg_rate)
neu = np.mean(neu_rate)
target.write(row['key'])
target.write(",")
target.write(row['value'])
target.write(",")
target.write(str(compound))
target.write(",")
target.write(str(pos))
target.write(",")
target.write(str(neu))
target.write(",")
target.write(str(neg))
target.write("\n")
print(compound,pos,neg,neu)
target.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment