classifier.js
Version:
:robot: Natural language processing with Javascript
66 lines (65 loc) • 2.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Category = void 0;
const tokensList_1 = require("./tokensList");
const { isArray } = Array;
const lib_1 = require("./lib");
class Category {
constructor(name, tokens = []) {
this.relatedCategories = new Set();
this.sentences = [];
this.isUpdated = true;
this.name = name;
this.inferedTokens = new tokensList_1.TokensList(name);
for (const [name, value] of tokens)
this.inferedTokens.tokens.set(name, value);
}
addSentence(sentence, relatedCategories) {
var _a;
this.isUpdated = false;
const normalizedSentence = this.normalizeData(sentence);
(_a = this.sentences) === null || _a === void 0 ? void 0 : _a.push(normalizedSentence);
relatedCategories.forEach(category => this.relatedCategories.add(category));
return this;
}
normalizeData(sentence) {
return (0, lib_1.toNormalizedString)(sentence.toLowerCase());
}
getWords(input) {
return isArray(input) ? input.join(' ').split(' ') : input.split(' ');
}
isARelatedCategory(category) {
return this.relatedCategories.has(category);
}
analize(categories) {
if (this.isUpdated)
return this;
this.getWords(this.sentences).forEach((word) => {
this.inferedTokens.increaseRelevancy(word);
});
categories
.filter((category) => category.name !== this.name && !this.isARelatedCategory(category.name))
.forEach((category) => category
.getWords(category.sentences)
.forEach((word) => this.inferedTokens.decreaseRelevancy(word)));
this.isUpdated = true;
return this;
}
resolveScore(score) {
const positiveValues = score.filter((item) => item >= 0);
const negativeValues = score.filter((item) => item < 0);
const sumOfPositiveValues = positiveValues.reduce(lib_1.sumFunc, 0);
const resolvedNegativeScore = (0, lib_1.getAbsoluteValue)(negativeValues.reduce(lib_1.sumFunc, 0));
return sumOfPositiveValues / (resolvedNegativeScore + sumOfPositiveValues);
}
classify(sentence, categories) {
this.analize(categories);
const classifiedWords = this.getWords(this.normalizeData(sentence)).map((word) => this.inferedTokens.test(word));
const score = this.resolveScore(classifiedWords);
return score > 0 ? score : 0;
}
getTokens() {
return [...this.inferedTokens.tokens.entries()];
}
}
exports.Category = Category;