NLTK klasyfikuje interfejs za pomocą wyszkolonego klasyfikatora
Mam ten mały fragment kodu, który znalazłemtutaj:
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()
Ale jak mogę sklasyfikować przypadkowe słowo, które może znajdować się w korpusie.
classifier.classify('magnificent')
Nie działa Czy potrzebuje jakiegoś obiektu?
Dziękuję Ci bardzo.
EDIT: Dzięki opinii @ unutbu i kopaniututaj i czytanie komentarzy do oryginalnego postu daje następujące kody „pos” lub „neg” dla tego kodu (ten jest „pos”)
print(classifier.classify(word_feats(['magnificent'])))
a to daje ocenę słowa „pos” lub „neg”
print(classifier.prob_classify(word_feats(['magnificent'])).prob('neg'))