nlp-toolkit
Version:
This module covers some basic nlp principles and implementations. Every implementation in this module is written as stream to only hold that data in memory that is currently processed at any step.
42 lines (34 loc) • 939 B
JavaScript
/**
* Bayes classifier.
*/
;
/**
* MODULES.
*/
var bayes = require('bayes_fixed');
/**
* FUNCTIONS.
*/
function bayesClassifier(trainingSet) {
var classifier = bayes();
return {
learn: function learn(sentence, feature) {
var _sentence = (typeof sentence === 'object' && Object.prototype.toString.call(sentence) !== '[object Array]') ? sentence.text : sentence;
if (typeof _sentence !== 'string') {
_sentence = _sentence.join(' ');
}
classifier.learn(_sentence, feature);
},
classify: function classify(sentence) {
var _sentence = (typeof sentence === 'object' && Object.prototype.toString.call(sentence) !== '[object Array]') ? sentence.text : sentence;
if (typeof _sentence !== 'string') {
_sentence = _sentence.join(' ');
}
return classifier.categorize(_sentence);
}
};
}
/**
* EXPORTS.
*/
module.exports = bayesClassifier;