2
分類界面我的代碼,這個小塊,我發現here:NLTK使用訓練分類
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import movie_reviews
from nltk.corpus import stopwords
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()
但我怎麼能分類隨機單詞,可能是在語料庫。
classifier.classify('magnificent')
不起作用。它需要某種對象嗎?
非常感謝。
編輯:多虧@ unutbu的反饋和一些挖here並在原帖如下產量的POS「或「NEG」這個代碼(這一個是一個「正」)閱讀註釋
print(classifier.classify(word_feats(['magnificent'])))
和這產生單詞的評價爲 'POS' 或 '負'
print(classifier.prob_classify(word_feats(['magnificent'])).prob('neg'))