NaturalNode/natural — pull request #122 by Joe8Bit (2014-01-28 15:56:16+00:00)
Title: Add event emitter to classification trainer

At the moment the `train` method in the `Classify` class is a bit of a blackhole, in that there is no way of determining how much progress has been made (if any). This commit adds instrumentation to that method, in that it now emits an event `trainedWithDocument` when an individual document is finished with.

I added this due to my need to provide a WebSockets 'status' API to a current project that could be used to inform users as to the progress of their classifiers training. As when they'd add 50,000 documents in a batch (for instance, loading in an existing classified set) it would just tick over and give no feedback.

The usage is very simple, and is highlighted in the tests added and in the edits to `README.md`, but for the sake of clarity:

var classifier = new natural.BayesClassifier();
classifier.addDocument('i fixed the box', 'computing');
classifier.addDocument('i write code', 'computing');
classifier.addDocument('write a book', 'literature');
classifier.addDocument('study the books', 'literature');

classifier.train();

classifier.events.on('trainedWithDocument', function (obj) {
console.log(obj.index + ' of ' + obj.total + ' complete);
});

I added the tests to a new file `spec/classifier_spec.js` as it seemed to not fit the other more implementation specific specs in the other files. Happy to change this however.

Would love to get your thoughts!