本文整理汇总了Python中nltk.NaiveBayesClassifier.classify方法的典型用法代码示例。如果您正苦于以下问题:Python NaiveBayesClassifier.classify方法的具体用法?Python NaiveBayesClassifier.classify怎么用?Python NaiveBayesClassifier.classify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.NaiveBayesClassifier
的用法示例。
在下文中一共展示了NaiveBayesClassifier.classify方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: defaultdict
# 需要导入模块: from nltk import NaiveBayesClassifier [as 别名]
# 或者: from nltk.NaiveBayesClassifier import classify [as 别名]
feature_freqdist = defaultdict(FreqDist)
feature_values = defaultdict(set)
num_samples = len(train_samples) / 2
for token, counts in labeled_features.items():
for label in ['neg','pos']:
feature_freqdist[label, token].inc(True, count=counts[label])
feature_freqdist[label, token].inc(None, num_samples - counts[label])
feature_values[token].add(None)
feature_values[token].add(True)
for item in feature_freqdist.items():
print item[0],item[1]
feature_probdist = {}
for ((label, fname), freqdist) in feature_freqdist.items():
probdist = ELEProbDist(freqdist, bins=len(feature_values[fname]))
feature_probdist[label,fname] = probdist
return feature_probdist
labeled_features = get_labeled_features(train_samples)
label_probdist = get_label_probdist(labeled_features)
feature_probdist = get_feature_probdist(labeled_features)
classifier = NaiveBayesClassifier(label_probdist, feature_probdist)
for sample in test_samples:
print "%s | %s" % (sample, classifier.classify(gen_bow(sample)))
classifier.show_most_informative_features()