From 9bc4b6daaaa49d473fce1f27c62a71abc222a944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Thu, 6 Dec 2018 16:06:44 +0100 Subject: [PATCH] added an option to use a uniform prior rather than a learnt one --- lib/classificator.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/classificator.js b/lib/classificator.js index 8e15947..a517a57 100644 --- a/lib/classificator.js +++ b/lib/classificator.js @@ -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. @@ -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; @@ -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);