这是一个非常简单的Pytho库,实现了朴素贝叶斯分类器。
示例代码:
"""Suppose you have some texts of ews ad kow their categories.You wat to trai a system with this pre-categorized/pre-classified texts. So, you have better call this data your traiig set."""from aiveBayesClassifier import tokeizerfrom aiveBayesClassifier.traier import Traierfrom aiveBayesClassifier.classifier import ClassifierewsTraier = Traier(tokeizer.Tokeizer(stop_words = [], sigs_to_remove = ["?!#%&"]))# You eed to trai the system passig each text oe by oe to the traier module.ewsSet =[ {'text': 'ot to eat too much is ot eough to lose weight', 'category': 'health'}, {'text': 'Russia is tryig to ivade Ukraie', 'category': 'politics'}, {'text': 'do ot eglect exercise', 'category': 'health'}, {'text': 'Syria is the mai issue, Obama says', 'category': 'politics'}, {'text': 'eat to lose weight', 'category': 'health'}, {'text': 'you should ot eat much', 'category': 'health'}]for ews i ewsSet: ewsTraier.trai(ews['text'], ews['category'])# Whe you have sufficiet traied data, you are almost doe ad ca start to use# a classifier.ewsClassifier = Classifier(ewsTraier.data, tokeizer.Tokeizer(stop_words = [], sigs_to_remove = ["?!#%&"]))# Now you have a classifier which ca give a try to classifiy text of ews whose# category is ukow, yet.ukowIstace = "Eve if I eat too much, is ot it possible to lose some weight"classificatio = ewsClassifier.classify(ukowIstace)# the classificatio variable holds the possible categories sorted by # their probablity valueprit classificatio






评论