From e313f3086814c3970de327d9beaf8a17156a82c1 Mon Sep 17 00:00:00 2001 From: Mike Amaral Date: Thu, 30 Apr 2015 15:04:03 -0400 Subject: [PATCH] When adding a document to a classifier, the features for the given document's tokens need to be incremented, rather than hard-coded to 1 every time. --- lib/natural/classifiers/classifier.js | 5 +++-- spec/classifier_spec.js | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/natural/classifiers/classifier.js b/lib/natural/classifiers/classifier.js index f39da0c17..e65bd1701 100644 --- a/lib/natural/classifiers/classifier.js +++ b/lib/natural/classifiers/classifier.js @@ -47,8 +47,9 @@ function addDocument(text, classification) { text: text }); - for(var i = 0; i < text.length; i++) { - this.features[text[i]] = 1; + for (var i = 0; i < text.length; i++) { + var token = text[i]; + this.features[token] = (this.features[token] || 0) + 1; } } diff --git a/spec/classifier_spec.js b/spec/classifier_spec.js index 416614361..5e307049b 100644 --- a/spec/classifier_spec.js +++ b/spec/classifier_spec.js @@ -20,8 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -var natural = require('lib/natural'); -var baseClassifier = require('lib/natural/classifiers/classifier.js'); +var natural = require('../lib/natural'); +var baseClassifier = require('../lib/natural/classifiers/classifier.js'); var fs = require('fs'); describe('classifier', function () { @@ -33,6 +33,20 @@ describe('classifier', function () { classifier.addDocument('', 'philosophy'); expect(classifier.docs.length).toBe(0); }); + + it('should increment features', function () { + var classifier = new natural.BayesClassifier(); + classifier.addDocument('foo', ''); + classifier.addDocument('foo', ''); + classifier.addDocument('bar', ''); + classifier.addDocument('bar', ''); + classifier.addDocument('bar', ''); + classifier.addDocument('baz', ''); + expect(classifier.docs.length).toBe(6); + expect(classifier.features['foo']).toBe(2); + expect(classifier.features['bar']).toBe(3); + expect(classifier.features['baz']).toBe(1); + }); }); describe('events emitters', function () {