下面是我调用方法classify()的代码:
def sentiment(text):
feats = find_features(text)
return voted_classifier.classify(feats),voted_classifier.confidence(feats)
find_features()方法的定义:
def find_features(document):
words = word_tokenize(document)
features = {}
for w in word_features:
features[w] = (w in words)
return features
我得到错误:
TypeError: classify() missing 1 required positional argument: 'featureset'
其中featuresets是:
featuresets_f = open("pickled_algos/featuresets.pickle", "rb")
featuresets = pickle.load(featuresets_f)
featuresets_f.close()
random.shuffle(featuresets)
print(len(featuresets))
testing_set = featuresets[8000:]
training_set = featuresets[:8000]
(注:我正在Ubuntu 14.04上使用Python 3.4和nltk进行twitter情感分析)
最佳答案
我怀疑你没有训练你的分类器。请注意以下错误:
>>> from nltk import NaiveBayesClassifier # for example
>>> NaiveBayesClassifier.classify(feats)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: classify() missing 1 required positional argument: 'featureset'
你需要先训练它:
>>> classifier = NaiveBayesClassifier.train(training_set)
然后可以对特征进行分类:
>>> classifier.classify(feats) # feats == a dict of features