2014-02-20 19 views
0

我想編譯一個檢測情感分析的程序。但是,我對Python真的很陌生,這是我第一次處理它。每當我編譯源 代碼,以下錯誤出現Python的情緒分析:IndexError - 列表索引超出範圍

Traceback (most recent call last): 
    File "sentiment.py", line 23, in <module> 
    tweets.append([row[3], row[4]]); 
IndexError: list index out of range 

線23

for row in reader: 
    tweets.append([row[3], row[4]]); 

這是源代碼

""" 
@package sentiment 
Twitter sentiment analysis. 

This code performs sentiment analysis on Tweets. 

A custom feature extractor looks for key words and emoticons. These are fed in 
to a naive Bayes classifier to assign a label of 'positive', 'negative', or 
'neutral'. Optionally, a principle components transform (PCT) is used to lessen 
the influence of covariant features. 

""" 
import csv, random 
import nltk 
import tweet_features, tweet_pca 


# read all tweets and labels 
fp = open('sentiment.csv', 'rb') 
reader = csv.reader(fp, delimiter=',', quotechar='"', escapechar='\\') 
tweets = [] 
for row in reader: 
    tweets.append([row[3], row[4]]); 

# treat neutral and irrelevant the same 
for t in tweets: 
    if t[1] == 'irrelevant': 
     t[1] = 'neutral' 


# split in to training and test sets 
random.shuffle(tweets); 

fvecs = [(tweet_features.make_tweet_dict(t),s) for (t,s) in tweets] 
v_train = fvecs[:2500] 
v_test = fvecs[2500:] 


# dump tweets which our feature selector found nothing 
#for i in range(0,len(tweets)): 
# if tweet_features.is_zero_dict(fvecs[i][0]): 
#  print tweets[i][1] + ': ' + tweets[i][0] 


# apply PCA reduction 
#(v_train, v_test) = \ 
#  tweet_pca.tweet_pca_reduce(v_train, v_test, output_dim=1.0) 


# train classifier 
classifier = nltk.NaiveBayesClassifier.train(v_train); 
#classifier = nltk.classify.maxent.train_maxent_classifier_with_gis(v_train); 


# classify and dump results for interpretation 
print '\nAccuracy %f\n' % nltk.classify.accuracy(classifier, v_test) 
#print classifier.show_most_informative_features(200) 


# build confusion matrix over test set 
test_truth = [s for (t,s) in v_test] 
test_predict = [classifier.classify(t) for (t,s) in v_test] 

print 'Confusion Matrix' 
print nltk.ConfusionMatrix(test_truth, test_predict) 

任何幫助將非常感激。謝謝

+0

您可以發佈sentiment.csv的示例嗎? – user3

回答

0

在你的第23行,在列表

for row in reader: 
    tweets.append([row[3], row[4]]); 

append方法插入到列表中的元素。所以,該行的語法不正確。您可以一次追加一個元素,如下所示:

for row in reader: 
    tweets.append([row[3]) 
    tweets.append([row[4]) 
+1

您不要在Python中以分號結束行。 –

+0

@SudipKafle是啊...這是在代碼有問題的錯誤 –

相關問題