我正在尝试收集一些已解析为电子表格的推文的情绪数据。我不是最好的编码员,也有一段时间没有编码了,所以我超级生疏。我正在使用几年前在一个项目中使用的旧脚本做同样的事情,并且无法理解它以找出我的错误。任何帮助,将不胜感激!:)
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"
回溯(最近一次通话最后):
第 40 行,sentiment_data = get_sentiment(rating_data)
第 25 行,在 get_sentiment 中打印 ss['neg']
TypeError:字符串索引必须是整数,而不是 unicode