1
我有這段代碼。我有兩個功能。我如何一起訓練這兩個功能?培訓兩個功能而不是一個
from textblob import TextBlob, Word, Blobber
from textblob.classifiers import NaiveBayesClassifier
from textblob.taggers import NLTKTagger
import re
import nltk
def get_word_before_you_feature(mystring):
keyword = 'you'
before_keyword, keyword, after_keyword = mystring.partition(keyword)
before_keyword = before_keyword.rsplit(None, 1)[-1]
return {'word_after_you': before_keyword}
def get_word_after_you_feature(mystring):
keyword = 'you'
before_keyword, keyword, after_keyword = mystring.partition(keyword)
after_keyword = after_keyword.split(None, 1)[0]
return {'word_after_you': after_keyword}
classifier = nltk.NaiveBayesClassifier.train(train)
lang_detector = NaiveBayesClassifier(train, feature_extractor=get_word_after_you_feature)
lang_detector = NaiveBayesClassifier(train, feature_extractor=get_word_before_you_feature)
print(lang_detector.accuracy(test))
print(lang_detector.show_informative_features(5))
這是我得到的輸出。
word_before_you = '做' 參考,:generi = 2.2:1.0
word_before_you = '當' generi:參考,= 1.1:1.0
似乎只得到了最後一個特性。如何讓分類器訓練兩個特徵而不是一個特徵。
感謝您的意見。它現在有效! – user3078335 2014-10-04 20:55:27