Over a million developers have joined DZone.

Bayesian Classifier And Filtering

DZone's Guide to

Bayesian Classifier And Filtering

· ·
Free Resource
(Also copied and modified from seb's code here)
Reverend is a free Bayesian module for Python. Get it here
Here's how we train the classifier

from reverend.thomas import Bayes
g = Bayes()    # guesser
g.train('french','La souris est rentrée dans son trou.')
g.train('english','my tailor is rich.')
g.train('french','Je ne sais pas si je viendrai demain.')
g.train('english','I do not plan to update my website soon.')

Then use it to guess the language

>>> print g.guess('Jumping out of cliffs it not a good idea.')
[('english', 0.99990000000000001), ('french', 9.9999999999988987e-005)]
# 99.99% English

>>> print g.guess('Demain il fera très probablement chaud.')
[('french', 0.99990000000000001), ('english', 9.9999999999988987e-005)]
# 99.99% French

You can train it with more languages. You can also train it
to classify the kind of text.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}