import nltk.classify.util
import csv
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
def word_feats(words):
return dict([(word, True) for word in words])
negids = movie_reviews.fileids('neg')
posids = movie_reviews.fileids('pos')
negfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'neg') for f in negids]
posfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'pos') for f in posids]
negcutoff = len(negfeats)*3/4
poscutoff = len(posfeats)*3/4
trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff]
testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]
print 'train on %d instances, test on %d instances' % (len(trainfeats), len(testfeats))
classifier = NaiveBayesClassifier.train(trainfeats)
print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)
classifier.show_most_informative_features()
data = csv.DictReader(open('twitter_data_Trump.csv', 'r'))
print data.fieldnames
for each in data:
row = {}
otuput.append(row)
print output
我是Python新手,我在尝试弄清楚如何使用电影评论分类器对我导入的.csv文件进行分类时遇到了麻烦。我一直收到一条错误消息:“没有这样的文件或目录。”关于如何解决这个问题的任何想法?我正在尝试读取.csv文件,并基本上为csv文件中包含的推文分配正面和负面。我不确定我的代码是否有效并完成了手头的任务。