UNPKG

fin-ukus

Version:

UK US spelling variation detection for FIN NLP

52 lines (51 loc) 1.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const spelling_variations_1 = require("spelling-variations"); const Fin = require("finnlp"); Fin.Run.prototype.tokenScores = function () { const scores = []; this.sentences.forEach((sentence, si) => { scores[si] = []; sentence.tokens.forEach((token, ti) => { scores[si][ti] = 0; const analysis = new spelling_variations_1.default(token); if (analysis.scoreUK() > analysis.scoreUS()) scores[si][ti] += 1; if (analysis.scoreUK() < analysis.scoreUS()) scores[si][ti] -= 1; }); }); return scores; }; Fin.Run.prototype.sentencesScores = function () { return this.tokenScores().map(tokenBased => tokenBased.reduce((a, b) => a + b, 0)); }; Fin.Run.prototype.inputScore = function () { return this.sentencesScores().reduce((a, b) => a + b, 0); }; Fin.Run.prototype.toUS = function () { let input = this.raw; this.tokenScores().forEach((sentence, si) => { sentence.forEach((score, ti) => { if (score > 0) { const token = this.sentences[si].tokens[ti]; const analysis = new spelling_variations_1.default(token); input = input.split(token).join(analysis.USPreferred()); } }); }); return input; }; Fin.Run.prototype.toUK = function () { let input = this.raw; this.tokenScores().forEach((sentence, si) => { sentence.forEach((score, ti) => { if (score < 0) { const token = this.sentences[si].tokens[ti]; const analysis = new spelling_variations_1.default(token); input = input.split(token).join(analysis.UKPreferred()); } }); }); return input; };