I'm trying to gather some sentiment data on some tweets that I've parsed into a spreadsheet. I'm not the best coder and haven't coded in a good while so I'm super rusty. I am using an old script that I had for a project a few years back doing the same thing and am having trouble understanding it in order to figure out my error. Any help would be appreciated! :)
import pandas as pd
import numpy as np
import nltk
nltk.download("vader_lexicon")
def get_sentiment(rating_data):
"""
https: // github.com / cjhutto / vaderSentiment
:return:
"""
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
rating_data['sent_neg'] = -10
rating_data['sent_neu'] = -10
rating_data['sent_pos'] = -10
rating_data['sent_compound'] = -10
for i in range(len(rating_data)):
sentence = rating_data['Sentences'][i]
ss = str(sid.polarity_scores(sentence.encode('ascii', 'ignore')))
print ss['neg']
rating_data.iloc[i, 1] = float(ss['neg'])
print rating_data.iloc[i, 1]
rating_data.iloc[i, 2] = ss['neu']
rating_data.iloc[i, 3] = ss['pos']
rating_data.iloc[i, 4] = ss['compound']
return rating_data
rating_data = pd.read_csv("for-sent.csv", 'r', encoding='ascii', error_bad_lines=False)
rating_data = rating_data.rename(columns={ rating_data.columns[0]: "Sentences" })
sentiment_data = get_sentiment(rating_data)
sentiment_data.to_excel("sentiment.xlsx", index = False)
print " Written to sentiment.xlsx"
Traceback (most recent call last):
line 40, in sentiment_data = get_sentiment(rating_data)
line 25, in get_sentiment print ss['neg']
TypeError: string indices must be integers, not unicode