classifier.js
Version:
:robot: Natural language processing with Javascript
39 lines (38 loc) • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokensList = void 0;
const lib_1 = require("./lib");
class TokensList {
constructor(category) {
this.tokens = new Map();
this.category = category;
}
increaseRelevancy(token) {
var _a;
const currentTokenProps = this.tokens.get(token);
this.tokens.set(token, { relevancy: ((_a = currentTokenProps === null || currentTokenProps === void 0 ? void 0 : currentTokenProps.relevancy) !== null && _a !== void 0 ? _a : 0) + 1 });
}
decreaseRelevancy(token) {
var _a;
const currentTokenProps = this.tokens.get(token);
this.tokens.set(token, { relevancy: ((_a = currentTokenProps === null || currentTokenProps === void 0 ? void 0 : currentTokenProps.relevancy) !== null && _a !== void 0 ? _a : 0) - 1 });
}
getSimilarity(value) {
const entries = [...this.tokens.entries()];
let similarity = 0, strength = 0;
entries.forEach((entry) => {
const [token, tokenProps] = entry;
const currentSimilarity = (0, lib_1.resolveSimilarity)(token, value);
if (currentSimilarity > similarity) {
similarity = currentSimilarity;
strength = tokenProps.relevancy;
}
});
return similarity * strength;
}
test(token) {
var _a, _b;
return (_b = (_a = this.tokens.get(token)) === null || _a === void 0 ? void 0 : _a.relevancy) !== null && _b !== void 0 ? _b : this.getSimilarity(token);
}
}
exports.TokensList = TokensList;