nbayes is a lightweight Naive Bayes Classifier written in vanilla JavaScript. It classifies a document (arbitrary piece of text) among the classes (arbitrarily named categories) it has been trained with before. This is all basic on simple mathematics. As an example, you could use nbayes to answer the following questions.
- Is an email spam, or not spam ?
- Is a news article about technology, politics, or sports ?
- Does a piece of text express positive emotions, or negative emotions?
const nbayes = require('nbayes')
let classifier = nbayes()
classifier.learn('happy', nbayes.stringToDoc('amazing, awesome movie!! Yeah!! Oh boy.'))
classifier.learn('happy', nbayes.stringToDoc('Sweet, this is incredibly amazing, perfect, great!!'))
classifier.learn('angry', nbayes.stringToDoc('terrible, shitty thing. Damn. This Sucks!!'))
classifier.learn('neutral', nbayes.stringToDoc('I dont really know what to make of this.'))
classifier.classify(nbayes.stringToDoc('awesome, cool, amazing!! Yay.'))
// -> 'happy'
nbayes offers a simple and straightforward API, keeping it below 3kb (minified). It is an ISC-licensed rewrite of ttezel/bayes and thoroughly tested.
npm install nbayes
Creates a representation of a document, which can be used to track words and their frequencies.
let d = nbayes.createDoc()
d.set('foo', 2)
d.add('bar')
d.increase('bar', 2)
d.has('FOO') // -> false
d.get('foo') // -> 2
d.get('bar') // -> 3
d.sum() // -> 5
d.words() // -> 2
has(word)
: Ifword
has beenadd
ed before.get(word)
: Returns the count ofword
.set(word, count)
: Sets the count ofword
.add(word)
: Shorthand forincrease(word, 1)
.increase(word, d = 1)
: Addsd
to the count ofword
.sum
: Returns the sum of all word counts.words
: Returns the number of distinct word.
Returns a document from the string. Special characters will be ignored. Everything will be lowercase.
Note: It is probably a better idea to use a proper tokenizer/stemmer and to remove stopwords to support non-Latin languages and to get more accurate results.
nbayes.stringToDoc('awesome, amazing!! Yay.').words()
// -> ['awesome', 'amazing', 'yay']
Creates a classifier, which can learn
and then classify
documents into classes.
let c = nbayes()
c.learn('happy', nbayes.stringToDoc('amazing, awesome movie!! Yeah!! Oh boy.'))
c.learn('happy', nbayes.stringToDoc('Sweet, this is incredibly amazing, perfect, great!!'))
c.learn('angry', nbayes.stringToDoc('terrible, shitty thing. Damn. This Sucks!!'))
c.learn('neutral', nbayes.stringToDoc('I dont really know what to make of this.'))
c.classify(c.stringToDoc('awesome, cool, amazing!! Yay.'))
// -> 'happy'
c.probabilities(c.stringToDoc('awesome, cool, amazing!! Yay.'))
// -> { happy: 0.000001…,
// angry: 2.384…e-7,
// neutral: 1.665…e-7 }
learn(class, doc)
: Tags words ofdoc
as being ofclass
.probabilities(doc)
: For each stored class, returns the probability ofdoc
, given the class.classify(doc)
: Fordoc
, returns the class with the highest probability.prior(class)
: Computes the probability ofclass
out of all classes.likelihood(class, doc)
: Computes the probability ofdoc
, givenclass
.
If you have a question, found a bug or want to propose a feature, have a look at the issues page.