Skip to content

Commit

Permalink
Merge pull request NaturalNode#236 from mamaral/increment-features
Browse files Browse the repository at this point in the history
Increment features
  • Loading branch information
kkoch986 committed Sep 7, 2015
2 parents ee17323 + e313f30 commit d77ee23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/natural/classifiers/classifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
18 changes: 16 additions & 2 deletions spec/classifier_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down

0 comments on commit d77ee23

Please sign in to comment.