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() 

を解凍する必要以上1つの値このコードを実行しているとき、私は、エラーが来ている...PythonのNLTK Classifier.train(trainfeats)...とValueError:

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)の行から、私はなぜそうはわかりません。私は前にこのようなことをしていましたが、私の訓練の継ぎ目はそれと同じ形式になっています...フォーマットのサンプルは以下にリストされています...

[[({'真:'、 'true'、 'true'、 'pos')]]]

私の他の値は何ですか? trainfeatsはクラシファイアを作成する必要がありますか? は@Pruneによってコメントが正しいとテキスト

+1

カッコには大カッコが2つあります。唯一の要素がタプルだけのリストであるリストです。そのタプルには辞書と文字列が含まれています。おそらくあなたはラッパーが多すぎるので、アンパックは1つのアイテムだけを表示します。 – Prune

答えて

1

を強調した:機能辞書や各データポイントのカテゴリ:あなたのlabeled_featuresetsはペア(2要素のリストまたはタプル)の配列でなければなりません。代わりに、trainfeatsの各要素は、1つの要素を含むリストです。これら2つの要素のタプルです。両方の機能構築ループで角括弧を紛失すると、この部分が正しく機能するはずです。例えば、

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

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

+0

良い付加価値;ありがとう! – Prune

関連する問題