0

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

  • 3
    What do you want to achieve when you write `ss['neg']`? – Patol75 Apr 22 '20 at 11:32
  • 1
    The error is telling you pretty clearly what's wrong here: you tried to access a string like a dict, `ss['neg']` ... i mean, what do you expect to happen if you try something like`'strGoesHere'['neg']`? – garglblarg Apr 22 '20 at 11:38
  • Ah thank you, I forgot I added that str() conversion to try to fix *AttributeError: 'float' object has no attribute 'encode'.* I guess with that removed, what would be the best way to correct this? – Schleepy Apr 22 '20 at 16:15
  • My desired output for the spreadsheet would have a header containing sentences, sent_neg, sent_neu, sent_pos, and sent_compound followed by rows with the tweet along with its respective sentiment score. – Schleepy Apr 22 '20 at 17:10

0 Answers0