NLTK accuracy: "ValueError: too many values to unpack"

NLTK accuracy: "ValueError: too many values to unpack"

我正在尝试使用 NLTK 工具包对 Twitter 上的一部新电影进行情感分析。我遵循了 NLTK 'movie_reviews' 示例,并且构建了自己的 CategorizedPlaintextCorpusReader 对象。当我调用 nltk.classify.util.accuracy(classifier, testfeats) 时出现问题。这是代码:

import os
import glob
import nltk.classify.util
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]

trainfeats = negfeats + posfeats

# Building a custom Corpus Reader
tweets = nltk.corpus.reader.CategorizedPlaintextCorpusReader('./tweets', r'.*\.txt', cat_pattern=r'(.*)\.txt')
tweetsids = tweets.fileids()
testfeats = [(word_feats(tweets.words(fileids=[f]))) for f in tweetsids]

print 'Training the classifier'
classifier = NaiveBayesClassifier.train(trainfeats)

for tweet in tweetsids:
        print tweet + ' : ' + classifier.classify(word_feats(tweets.words(tweetsids)))

classifier.show_most_informative_features()

print 'accuracy:', nltk.classify.util.accuracy(classifier, testfeats)

在到达最后一行之前,一切似乎都运行良好。那是我收到错误的时候:

>>> nltk.classify.util.accuracy(classifier, testfeats)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/nltk/classify/util.py", line 87, in accuracy
    results = classifier.classify_many([fs for (fs,l) in gold])
ValueError: too many values to unpack

有人看到代码有什么问题吗?

谢谢。

错误信息

File "/usr/lib/python2.7/dist-packages/nltk/classify/util.py", line 87, in accuracy
  results = classifier.classify_many([fs for (fs,l) in gold])
ValueError: too many values to unpack

出现是因为gold中的项目不能解包成一个二元组,(fs,l):

[fs for (fs,l) in gold]  # <-- The ValueError is raised here

如果 gold 等于 [(1,2,3)],你会得到同样的错误,因为 3 元组 (1,2,3) 不能解包成 2 元组 (fs,l) :

In [74]: [fs for (fs,l) in [(1,2)]]
Out[74]: [1]
In [73]: [fs for (fs,l) in [(1,2,3)]]
ValueError: too many values to unpack

gold 可能隐藏在 nltk.classify.util.accuracy 的实现中,但这暗示您的输入 classifiertestfeats 是错误的 "shape" .

分类器没有问题,因为调用了accuracy(classifier, trainfeats) 作品:

In [61]: print 'accuracy:', nltk.classify.util.accuracy(classifier, trainfeats)
accuracy: 0.9675

问题一定在testfeats.


比较 trainfeatstestfeatstrainfeats[0] 是一个包含字典和分类的二元组:

In [63]: trainfeats[0]
Out[63]: 
({u'!': True,
  u'"': True,
  u'&': True,
  ...
  u'years': True,
  u'you': True,
  u'your': True},
 'neg')           # <---  Notice the classification, 'neg'

但是testfeats[0]只是一个字典,word_feats(tweets.words(fileids=[f])):

testfeats = [(word_feats(tweets.words(fileids=[f]))) for f in tweetsids]

因此,要解决此问题,您需要定义 testfeats 使其看起来更像 trainfeats -- word_feats 返回的每个字典都必须与分类配对。