Skip to content

Commit

Permalink
Add Deinflector.rulesMatch helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread committed Jan 15, 2024
1 parent 48f1d01 commit 3fcf663
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 12 additions & 1 deletion ext/js/language/deinflector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down Expand Up @@ -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;
}
}
5 changes: 4 additions & 1 deletion ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ export class Translator {
);
if (deinflections.length === 0) { return []; }

/** @type {string[]} */
const uniqueDeinflectionTerms = [];
/** @type {import('translation-internal').DatabaseDeinflection[][]} */
const uniqueDeinflectionArrays = [];
/** @type {Map<string, import('translation-internal').DatabaseDeinflection[]>} */
const uniqueDeinflectionsMap = new Map();
for (const deinflection of deinflections) {
const term = deinflection.deinflectedText;
Expand All @@ -275,7 +278,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);
}
}
Expand Down

0 comments on commit 3fcf663

Please sign in to comment.