aboutsummaryrefslogtreecommitdiff
path: root/ext/js/language
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-03-08 21:01:55 -0500
committerGitHub <noreply@github.com>2021-03-08 21:01:55 -0500
commitc6f4144fda0f07ddfc84ebee1960264c968d156b (patch)
tree23e1a7de5c3d6ff0ce1743726322d1d5005434dc /ext/js/language
parent643dbfb12a66b98b2fabe82416322f0218474567 (diff)
Clean up translator (#1505)
* Remove unused: _removeUsedDefinitions * Remove unused: _scoreToTermFrequency * Remove unused: _getTermTagsScoreSum * Add RegexUtil * Update Translator to use RegexUtil * Update sw.js * Update tests
Diffstat (limited to 'ext/js/language')
-rw-r--r--ext/js/language/translator.js90
1 files changed, 2 insertions, 88 deletions
diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js
index e8aba5b1..c885cd4d 100644
--- a/ext/js/language/translator.js
+++ b/ext/js/language/translator.js
@@ -17,6 +17,7 @@
/* global
* Deinflector
+ * RegexUtil
* TextSourceMap
*/
@@ -534,24 +535,6 @@ class Translator {
);
}
- _removeUsedDefinitions(definitions, termInfoMap, usedDefinitions) {
- for (let i = 0, ii = definitions.length; i < ii; ++i) {
- const definition = definitions[i];
- const {expression, reading} = definition;
- const expressionMap = termInfoMap.get(expression);
- if (
- typeof expressionMap !== 'undefined' &&
- typeof expressionMap.get(reading) !== 'undefined'
- ) {
- usedDefinitions.add(definition);
- } else {
- definitions.splice(i, 1);
- --i;
- --ii;
- }
- }
- }
-
_getUniqueDefinitionTags(definitions) {
const definitionTagsMap = new Map();
for (const {definitionTags} of definitions) {
@@ -794,16 +777,6 @@ class Translator {
// Simple helpers
- _scoreToTermFrequency(score) {
- if (score > 0) {
- return 'popular';
- } else if (score < 0) {
- return 'rare';
- } else {
- return 'normal';
- }
- }
-
_getNameBase(name) {
const pos = name.indexOf(':');
return (pos >= 0 ? name.substring(0, pos) : name);
@@ -974,14 +947,6 @@ class Translator {
// Reduction functions
- _getTermTagsScoreSum(termTags) {
- let result = 0;
- for (const {score} of termTags) {
- result += score;
- }
- return result;
- }
-
_getSourceTermMatchCountSum(definitions) {
let result = 0;
for (const {sourceTermExactMatchCount} of definitions) {
@@ -1408,59 +1373,8 @@ class Translator {
_applyTextReplacements(text, sourceMap, replacements) {
for (const {pattern, replacement} of replacements) {
- text = this._applyTextReplacement(text, sourceMap, pattern, replacement);
+ text = RegexUtil.applyTextReplacement(text, sourceMap, pattern, replacement);
}
return text;
}
-
- _applyTextReplacement(text, sourceMap, pattern, replacement) {
- const isGlobal = pattern.global;
- if (isGlobal) { pattern.lastIndex = 0; }
- for (let loop = true; loop; loop = isGlobal) {
- const match = pattern.exec(text);
- if (match === null) { break; }
-
- const matchText = match[0];
- const index = match.index;
- const actualReplacement = this._applyMatchReplacement(replacement, match);
- const actualReplacementLength = actualReplacement.length;
- const delta = actualReplacementLength - (matchText.length > 0 ? matchText.length : -1);
-
- text = `${text.substring(0, index)}${actualReplacement}${text.substring(index + matchText.length)}`;
- pattern.lastIndex += delta;
-
- if (actualReplacementLength > 0) {
- sourceMap.insert(index, ...(new Array(actualReplacementLength).fill(0)));
- sourceMap.combine(index - 1 + actualReplacementLength, matchText.length);
- } else {
- sourceMap.combine(index, matchText.length);
- }
- }
- return text;
- }
-
- _applyMatchReplacement(replacement, match) {
- const pattern = /\$(?:\$|&|`|'|(\d\d?)|<([^>]*)>)/g;
- return replacement.replace(pattern, (g0, g1, g2) => {
- if (typeof g1 !== 'undefined') {
- const matchIndex = Number.parseInt(g1, 10);
- if (matchIndex >= 1 && matchIndex <= match.length) {
- return match[matchIndex];
- }
- } else if (typeof g2 !== 'undefined') {
- const {groups} = match;
- if (typeof groups === 'object' && groups !== null && Object.prototype.hasOwnProperty.call(groups, g2)) {
- return groups[g2];
- }
- } else {
- switch (g0) {
- case '$': return '$';
- case '&': return match[0];
- case '`': return replacement.substring(0, match.index);
- case '\'': return replacement.substring(match.index + g0.length);
- }
- }
- return g0;
- });
- }
}