NLTK-Klassifizierungsschnittstelle unter Verwendung eines ausgebildeten Klassifikators
Ich habe dieses kleine Stück Code, das ich gefunden habeHier:
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()
Aber wie kann ich ein zufälliges Wort klassifizieren, das sich möglicherweise im Korpus befindet?
classifier.classify('magnificent')
Funktioniert nicht Braucht es ein Objekt?
Vielen Dank.
BEARBEITEN: Dank @ unutbu's Feedback und einigem GrabenHier und beim Lesen der Kommentare zum Originalbeitrag ergibt sich für diesen Code 'pos' oder 'neg' (dies ist eine 'pos')
print(classifier.classify(word_feats(['magnificent'])))
und dies ergibt die Auswertung des Wortes für "pos" oder "neg"
print(classifier.prob_classify(word_feats(['magnificent'])).prob('neg'))