Skip to content

Commit

Permalink
Use pure js bayes classifier
Browse files Browse the repository at this point in the history
  • Loading branch information
schipiga committed Apr 27, 2018
1 parent 7311edd commit ac53357
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 615 deletions.
41 changes: 41 additions & 0 deletions lib/classifier.js
@@ -0,0 +1,41 @@
"use strict";

/**
* Wrapper on bayes classifier.
*
* @module
*/

var bayes = require("bayes");

module.exports = () => {

var classifier = bayes();
classifier.isTrained = false;

classifier.train = function () {
this.isTrained = true;
};

classifier.getClassifications = function (text) {
var tokens = this.tokenizer(text);
var frequencyTable = this.frequencyTable(tokens);

var result = [];
for (var category in this.categories) {

var item = { label: category, value: 0 };
result.push(item);

for (var [token, frequency] of Object.entries(frequencyTable)) {
var tokenProbability = this.tokenProbability(token, category);
item.value += frequency * tokenProbability;
};
};

result.sort((a, b) => b.value - a.value);
return result;
};

return classifier;
};
9 changes: 3 additions & 6 deletions lib/tools.js
Expand Up @@ -12,16 +12,14 @@ require("colors");
var _ = require("lodash");
var expect = require("chai").expect;
var highlight = require("cli-highlight").highlight;
var natural = require("natural");
var Testrail = require("testrail-api");
var U = require("glace-utils");

var classifier = require("./classifier")();

var d = U.switchColor();
var self = module.exports;

var classifier = new natural.BayesClassifier();
classifier.isTrained = false;

var all_steps = [];
all_steps.isFilled = false;

Expand Down Expand Up @@ -64,7 +62,7 @@ self.listSteps = (filter, namesOnly) => {
if (!all_steps.isFilled) all_steps.push(tmp);

if (doc && !classifier.isTrained) {
classifier.addDocument(doc, s);
classifier.learn(doc, s);
}

if (namesOnly) {
Expand All @@ -79,7 +77,6 @@ self.listSteps = (filter, namesOnly) => {

if (!classifier.isTrained) {
classifier.train();
classifier.isTrained = true;
}

if (filter && !namesOnly) {
Expand Down

0 comments on commit ac53357

Please sign in to comment.