現在、nltk を使用して naivebayes 分類子を学習しています。
ドキュメント( http://www.nltk.org/book/ch06.html ) 1.3 ドキュメント分類に、特徴量の例があります。
featuresets = [(document_features(d), c) for (d,c) in documents]
train_set, test_set = featuresets[100:], featuresets[:100]
classifier = nltk.NaiveBayesClassifier.train(train_set)
all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
word_features = list(all_words)[:2000] [1]
def document_features(document): [2]
document_words = set(document) [3]
features = {}
for word in word_features:
features['contains({})'.format(word)] = (word in document_words)
return features
したがって、機能セットの形式の例は {('contains(waste)': False, 'contains(lot)': False, ...},'neg')...} です。
しかし、単語辞書の形式を'contains(waste)': Falseから'contains(waste)': 2に変更したいと思います。その form('contains(waste)': 2) は、世界の頻度を計算できるので、ドキュメントをよく説明していると思います。したがって、機能セットは{('contains(waste)': 2, 'contains(lot)': 5, ...},'neg')...} になります。
しかし、'contains(waste)': 2と'contains(waste)': 1は naivebayesclassifier とはまったく別の言葉ではないか心配です。それでは、 'contains(waste)': 2と'contains(waste)': 1の類似性を説明できません。
{'contains(lot)': 1 and 'contains(waste)': 1} and {'contains(waste)': 2 and 'contains(waste)': 1}は、同じプログラムにできます。
nltk.naivebayesclassifier は単語の頻度を理解できますか?
これは私が使用したコードです
def split_and_count_word(data):
#belongs_to : Main
#Role : make featuresets from korean words using konlpy.
#Parameter : dictionary data(dict of contents ex.{'politic':{'parliament': [content,content]}..})
#Return : list featuresets([{'word':True',...},'politic'] == featureset + category)
featuresets = []
twitter = konlpy.tag.Twitter()#Korean word splitter
for big_cat in data:
for small_cat in data[big_cat]:
#save category name needed in featuresets
category = str(big_cat[0:3])+'/'+str(small_cat)
count = 0; print(small_cat)
for one_news in data[big_cat][small_cat]:
count+=1; if count%100==0: print(count,end=' ')
#one_news is list in list so open it!
doc = one_news
#split word as using konlpy
list_of_splited_word = twitter.morphs(doc[:-63])#delete useless sentences.
#get word length is higher than two and get list of splited words
list_of_up_two_word = [word for word in list_of_splited_word if len(word)>1]
dict_of_featuresets = make_featuresets(list_of_up_two_word)
#save
featuresets.append((dict_of_featuresets,category))
return featuresets
def make_featuresets(data):
#belongs_to : split_and_count_word
#Role : make featuresets
#Parameter : list list_of_up_two_word(ex.['비누','떨어','지다']
#Return : dictionary {word : True for word in data}
#PROBLEM :(
#cannot consider the freqency of word
return {word : True for word in data}
def naive_train(featuresets):
#belongs_to : Main
#Role : Learning by naive bayes rule
#Parameter : list featuresets([{'word':True',...},'pol/pal'])
#Return : object classifier(nltk naivebayesclassifier object),
# list test_set(the featuresets that are randomly selected)
random.shuffle(featuresets)
train_set, test_set = featuresets[1000:], featuresets[:1000]
classifier = naivebayes.NaiveBayesClassifier.train(train_set)
return classifier,test_set
featuresets = split_and_count_word(data)
classifier,test_set = naive_train(featuresets)