Skip to content

Commit

Permalink
added an option to use a uniform prior rather than a learnt one
Browse files Browse the repository at this point in the history
  • Loading branch information
François committed Dec 6, 2018
1 parent e58b41f commit 9bc4b6d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/classificator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const STATE_KEYS = (module.exports.STATE_KEYS = [
'options',
]);
const DEFAULT_ALPHA = 1
const DEFAULT_FIT_PRIOR = true

/**
* Initializes a NaiveBayes instance from a JSON state representation.
Expand Down Expand Up @@ -107,7 +108,7 @@ function Naivebayes(options) {

this.tokenizer = this.options.tokenizer || defaultTokenizer;
this.alpha = this.options.alpha || DEFAULT_ALPHA

this.fitPrior = this.options.fitPrior === undefined ? DEFAULT_FIT_PRIOR : this.options.fitPrior
//initialize our vocabulary and its size
this.vocabulary = {};
this.vocabularySize = 0;
Expand Down Expand Up @@ -295,7 +296,12 @@ Naivebayes.prototype.categorize = function(text){
//start by calculating the overall probability of this category
//=> out of all documents we've ever looked at, how many were
// mapped to this category
let categoryLikelihood = this.docCount[category] / this.totalDocuments;
let categoryLikelihood
if (this.fitPrior) {
categoryLikelihood = this.docCount[category] / this.totalDocuments;
} else {
categoryLikelihood = 1
}

//take the log to avoid underflow
// let logLikelihood = Math.log(categoryLikelihood);
Expand Down

0 comments on commit 9bc4b6d

Please sign in to comment.