From be267e1994f8fda2ef68562c5c3a5edc9442a8a8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 19 Jan 2024 22:42:43 -0500 Subject: Add Deinflector.rulesMatch helper function (#530) --- ext/js/language/deinflector.js | 13 ++++++++++++- ext/js/language/translator.js | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'ext') diff --git a/ext/js/language/deinflector.js b/ext/js/language/deinflector.js index 02a215a1..7d75576d 100644 --- a/ext/js/language/deinflector.js +++ b/ext/js/language/deinflector.js @@ -59,7 +59,7 @@ export class Deinflector { for (const [reason, variants] of this.reasons) { for (const [kanaIn, kanaOut, rulesIn, rulesOut] of variants) { if ( - (rules !== 0 && (rules & rulesIn) === 0) || + !Deinflector.rulesMatch(rules, rulesIn) || !term.endsWith(kanaIn) || (term.length - kanaIn.length + kanaOut.length) <= 0 ) { @@ -124,4 +124,15 @@ export class Deinflector { } return value; } + + /** + * If `currentRules` is `0`, then `nextRules` is ignored and `true` is returned. + * Otherwise, there must be at least one shared rule between `currentRules` and `nextRules`. + * @param {number} currentRules + * @param {number} nextRules + * @returns {boolean} + */ + static rulesMatch(currentRules, nextRules) { + return currentRules === 0 || (currentRules & nextRules) !== 0; + } } diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index a2313263..cedc7d3d 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -410,7 +410,7 @@ export class Translator { const definitionRules = Deinflector.rulesToRuleFlags(databaseEntry.rules); for (const deinflection of uniqueDeinflectionArrays[databaseEntry.index]) { const deinflectionRules = deinflection.rules; - if (!partsOfSpeechFilter || deinflectionRules === 0 || (definitionRules & deinflectionRules) !== 0) { + if (!partsOfSpeechFilter || Deinflector.rulesMatch(deinflectionRules, definitionRules)) { deinflection.databaseEntries.push(databaseEntry); } } -- cgit v1.2.3