2016-01-23 106 views
4

這裏是我的代碼,其中i調用方法分類():find_features的類型錯誤:分類()缺少1所需位置參數: 'FEATURESET'

def sentiment(text): 
    feats = find_features(text) 
    return voted_classifier.classify(feats),voted_classifier.confidence(feats) 

定義()方法:

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的Twitter情感分析,NLTK)

回答

2

我懷疑你有沒有受過訓練的分類項。請注意以下錯誤:

>>> 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 
相關問題