diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-10-04 12:54:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-04 12:54:55 -0400 |
commit | 2bd82353e46ecc8c16f2b55f81c8daae6f73e12e (patch) | |
tree | 933efe3292653edfb7c91c496d0e4ac8513219e3 /ext/bg/js/backend.js | |
parent | 86c64ac4c27279fef6bd6f49aa807a10cd9d08bf (diff) |
Translator options refactor (#879)
* Refactor internal options for findTerms to not use the settings object
* Move findTerms/findKanji options creation
* Deconstruct used options values to variables before any await calls
* Rename findTermsOptions to just options
* Add documentation comments
* Add type information about definitions
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r-- | ext/bg/js/backend.js | 71 |
1 files changed, 60 insertions, 11 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index a268396d..ebdd2cda 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -376,16 +376,19 @@ class Backend { async _onApiKanjiFind({text, optionsContext}) { const options = this.getOptions(optionsContext); - const definitions = await this._translator.findKanji(text, options); - definitions.splice(options.general.maxResults); + const {general: {maxResults}} = options; + const findKanjiOptions = this._getTranslatorFindKanjiOptions(options); + const definitions = await this._translator.findKanji(text, findKanjiOptions); + definitions.splice(maxResults); return definitions; } async _onApiTermsFind({text, details, optionsContext}) { const options = this.getOptions(optionsContext); - const mode = options.general.resultOutputMode; - const [definitions, length] = await this._translator.findTerms(mode, text, details, options); - definitions.splice(options.general.maxResults); + const {general: {resultOutputMode: mode, maxResults}} = options; + const findTermsOptions = this._getTranslatorFindTermsOptions(details, options); + const [definitions, length] = await this._translator.findTerms(mode, text, findTermsOptions); + definitions.splice(maxResults); return {length, definitions}; } @@ -948,25 +951,26 @@ class Backend { } async _textParseScanning(text, options) { + const {scanning: {length: scanningLength}, parsing: {readingMode}} = options; + const findTermsOptions = this._getTranslatorFindTermsOptions({wildcard: null}, options); const results = []; while (text.length > 0) { const term = []; const [definitions, sourceLength] = await this._translator.findTerms( 'simple', - text.substring(0, options.scanning.length), - {}, - options + text.substring(0, scanningLength), + findTermsOptions ); if (definitions.length > 0 && sourceLength > 0) { const {expression, reading} = definitions[0]; const source = text.substring(0, sourceLength); for (const {text: text2, furigana} of jp.distributeFuriganaInflected(expression, reading, source)) { - const reading2 = jp.convertReading(text2, furigana, options.parsing.readingMode); + const reading2 = jp.convertReading(text2, furigana, readingMode); term.push({text: text2, reading: reading2}); } text = text.substring(source.length); } else { - const reading = jp.convertReading(text[0], '', options.parsing.readingMode); + const reading = jp.convertReading(text[0], '', readingMode); term.push({text: text[0], reading}); text = text.substring(1); } @@ -976,6 +980,7 @@ class Backend { } async _textParseMecab(text, options) { + const {parsing: {readingMode}} = options; const results = []; const rawResults = await this._mecab.parseText(text); for (const [mecabName, parsedLines] of Object.entries(rawResults)) { @@ -988,7 +993,7 @@ class Backend { jp.convertKatakanaToHiragana(reading), source )) { - const reading2 = jp.convertReading(text2, furigana, options.parsing.readingMode); + const reading2 = jp.convertReading(text2, furigana, readingMode); term.push({text: text2, reading: reading2}); } result.push(term); @@ -1660,4 +1665,48 @@ class Backend { await this._optionsUtil.save(options); this._applyOptions(source); } + + _getTranslatorFindTermsOptions(details, options) { + const {wildcard} = details; + const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options); + const { + general: {compactTags, mainDictionary}, + scanning: {alphanumeric}, + translation: { + convertHalfWidthCharacters, + convertNumericCharacters, + convertAlphabeticCharacters, + convertHiraganaToKatakana, + convertKatakanaToHiragana, + collapseEmphaticSequences + } + } = options; + return { + wildcard, + compactTags, + mainDictionary, + alphanumeric, + convertHalfWidthCharacters, + convertNumericCharacters, + convertAlphabeticCharacters, + convertHiraganaToKatakana, + convertKatakanaToHiragana, + collapseEmphaticSequences, + enabledDictionaryMap + }; + } + + _getTranslatorFindKanjiOptions(options) { + const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options); + return {enabledDictionaryMap}; + } + + _getTranslatorEnabledDictionaryMap(options) { + const enabledDictionaryMap = new Map(); + for (const [title, {enabled, priority, allowSecondarySearches}] of Object.entries(options.dictionaries)) { + if (!enabled) { continue; } + enabledDictionaryMap.set(title, {priority, allowSecondarySearches}); + } + return enabledDictionaryMap; + } } |