def word_feats(words):
return dict([(word, True) for word in words])
for tweet in negTweets:
words = re.findall(r"[\w']+|[.,!?;]", tweet) #splits the tweet into words
negwords = [(word_feats(words), 'neg')] #tag the words with feature
negfeats.append(negwords) #add the words to the feature list
for tweet in posTweets:
words = re.findall(r"[\w']+|[.,!?;]", tweet)
poswords = [(word_feats(words), 'pos')]
posfeats.append(poswords)
negcutoff = len(negfeats)*3/4 #take 3/4ths of the words
poscutoff = len(posfeats)*3/4
trainfeats = negfeats[:negcutoff] + posfeats[:poscutoff] #assemble the train set
testfeats = negfeats[negcutoff:] + posfeats[poscutoff:]
classifier = NaiveBayesClassifier.train(trainfeats)
print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)
classifier.show_most_informative_features()
运行此代码时出现以下错误...
File "C:\Python27\lib\nltk\classify\naivebayes.py", line 191, in train
for featureset, label in labeled_featuresets:
ValueError: need more than 1 value to unpack
错误来自分类器 = NaiveBayesClassifier.train(trainfeats) 行,我不知道为什么。我以前做过类似的事情,我的 trainfeats 接缝的格式与当时的格式相同……下面列出了格式中的一个示例……
[[({'me': True, 'af': True, 'this': True, 'joy': True, 'high': True, 'hookah': True, 'got': True}, 'pos' )]]
我的 trainfeats 还需要什么其他值来创建分类器?强调文本