All files / lib classifier.js

100% Statements 17/17
100% Branches 0/0
100% Functions 3/3
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                2x   2x   3x   3x 2x 2x   2x 2x   4x 4x   4x 4x 4x       2x 2x     3x    
"use strict";
 
/**
 * Wrapper on bayes classifier.
 *
 * @module
 */
 
var bayes = require("bayes");
 
module.exports = () => {
 
    var classifier = bayes();
 
    classifier.classify = 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;
};