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 () {