0
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

エラーは classifier = NaiveBayesClassifier.train(trainfeats) 行から発生していますが、その理由はわかりません。私は以前にこのようなことをしたことがあり、私のtrainfeatsの縫い目は当時と同じ形式になります...形式のサンプルを以下に示します...

[[({'me': True, 'af': True, 'this': True, 'joy': True, 'high': True, 'hookah': True, 'got': True}, 'pos' )]]

私のtrainfeatsが分類器を作成するために必要な他の値は何ですか? 強調されたテキスト

4

1 に答える 1

1

@Prune のコメントは正しいです:labeled_featuresets一連のペア (2 要素のリストまたはタプル) である必要があります: 各データ ポイントの機能ディクテーションとカテゴリ。代わりに、 your の各要素はtrainfeats1 つの要素を含むリストです: これら 2 つの要素のタプルです。両方の機能構築ループで角かっこをなくすと、この部分が正しく機能するはずです。例えば、

negwords = (word_feats(words), 'neg')
negfeats.append(negwords)

nltk.word_tokenize()さらに 2 つのこと:独自のトークン化を行う代わりに使用することを検討してください。また、トレーニング データの順序をランダム化する必要がありますrandom.scramble(trainfeats)

于 2016-11-10T20:24:40.493 に答える