From 8a377cabe6b3d177aac50cdb7ffb5a4328d27fd0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 17 Dec 2021 17:02:13 -0500 Subject: [PATCH] matchType/deinflect distinction (#2040) * Pass a "deinflect" option to findTerms * Update Translator to use deinflect option * Fix test input options --- ext/js/background/backend.js | 6 ++++-- ext/js/display/display.js | 2 ++ ext/js/language/translator.js | 28 ++++++++++----------------- test/data/translator-test-inputs.json | 3 ++- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index ce830361..994a6184 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1080,7 +1080,7 @@ class Backend { const jp = this._japaneseUtil; const mode = 'simple'; const options = this._getProfileOptions(optionsContext); - const details = {matchType: 'exact'}; + const details = {matchType: 'exact', deinflect: true}; const findTermsOptions = this._getTranslatorFindTermsOptions(mode, details, options); const results = []; let previousUngroupedSegment = null; @@ -1959,8 +1959,9 @@ class Backend { } _getTranslatorFindTermsOptions(mode, details, options) { - let {matchType} = details; + let {matchType, deinflect} = details; if (typeof matchType !== 'string') { matchType = 'exact'; } + if (typeof deinflect !== 'boolean') { deinflect = true; } const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options); const { general: {mainDictionary, sortFrequencyDictionary, sortFrequencyDictionaryOrder}, @@ -1988,6 +1989,7 @@ class Backend { } return { matchType, + deinflect, mainDictionary, sortFrequencyDictionary, sortFrequencyDictionaryOrder, diff --git a/ext/js/display/display.js b/ext/js/display/display.js index 50df3b9d..cd60488d 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -851,8 +851,10 @@ class Display extends EventDispatcher { if (match !== null) { if (match[1]) { findDetails.matchType = 'suffix'; + findDetails.deinflect = false; } else if (match[3]) { findDetails.matchType = 'prefix'; + findDetails.deinflect = false; } source = match[2]; } diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index dd7d54a0..31a3b7ed 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -234,12 +234,7 @@ class Translator { return {dictionaryEntries: [], originalTextLength: 0}; } - const {matchType} = options; - const deinflections = await ( - matchType && matchType !== 'exact' ? - this._findTermsWildcard(text, enabledDictionaryMap, matchType) : - this._findTermDeinflections(text, enabledDictionaryMap, options) - ); + const deinflections = await this._findTermsInternal2(text, enabledDictionaryMap, options); let originalTextLength = 0; const dictionaryEntries = []; @@ -259,17 +254,13 @@ class Translator { return {dictionaryEntries, originalTextLength}; } - async _findTermsWildcard(text, enabledDictionaryMap, matchType) { - const databaseEntries = await this._database.findTermsBulk([text], enabledDictionaryMap, matchType); - return databaseEntries.length > 0 ? [this._createDeinflection(text, text, text, 0, [], databaseEntries)] : []; - } - - async _findTermDeinflections(text, enabledDictionaryMap, options) { - const deinflections = this._getAllDeinflections(text, options); - - if (deinflections.length === 0) { - return []; - } + async _findTermsInternal2(text, enabledDictionaryMap, options) { + const deinflections = ( + options.deinflect ? + this._getAllDeinflections(text, options) : + [this._createDeinflection(text, text, text, 0, [], [])] + ); + if (deinflections.length === 0) { return []; } const uniqueDeinflectionTerms = []; const uniqueDeinflectionArrays = []; @@ -286,7 +277,8 @@ class Translator { deinflectionArray.push(deinflection); } - const databaseEntries = await this._database.findTermsBulk(uniqueDeinflectionTerms, enabledDictionaryMap, null); + const {matchType} = options; + const databaseEntries = await this._database.findTermsBulk(uniqueDeinflectionTerms, enabledDictionaryMap, matchType); for (const databaseEntry of databaseEntries) { const definitionRules = Deinflector.rulesToRuleFlags(databaseEntry.rules); diff --git a/test/data/translator-test-inputs.json b/test/data/translator-test-inputs.json index 396c476c..daee53a4 100644 --- a/test/data/translator-test-inputs.json +++ b/test/data/translator-test-inputs.json @@ -12,7 +12,8 @@ ] }, "default": { - "wildcard": null, + "matchType": "exact", + "deinflect": true, "mainDictionary": "${title}", "sortFrequencyDictionary": null, "sortFrequencyDictionaryOrder": "descending",