summaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/api.js16
-rw-r--r--ext/bg/js/translator.js38
2 files changed, 28 insertions, 26 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index 9fed99a1..9751d4a1 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -74,21 +74,7 @@ async function apiOptionsSave(source) {
async function apiTermsFind(text, optionsContext) {
const options = await apiOptionsGet(optionsContext);
- const translator = utilBackend().translator;
-
- const searcher = {
- 'merge': translator.findTermsMerged,
- 'split': translator.findTermsSplit,
- 'group': translator.findTermsGrouped
- }[options.general.resultOutputMode].bind(translator);
-
- const {definitions, length} = await searcher(
- text,
- dictEnabledSet(options),
- options.scanning.alphanumeric,
- options
- );
-
+ const [definitions, length] = await utilBackend().translator.findTerms(text, options);
return {
length,
definitions: definitions.slice(0, options.general.maxResults)
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 305619a5..d33a2e27 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -141,9 +141,23 @@ class Translator {
return result;
}
- async findTermsGrouped(text, dictionaries, alphanumeric, options) {
+ async findTerms(text, options) {
+ switch (options.general.resultOutputMode) {
+ case 'group':
+ return await this.findTermsGrouped(text, options);
+ case 'merge':
+ return await this.findTermsMerged(text, options);
+ case 'split':
+ return await this.findTermsSplit(text, options);
+ default:
+ return [[], 0];
+ }
+ }
+
+ async findTermsGrouped(text, options) {
+ const dictionaries = dictEnabledSet(options);
const titles = Object.keys(dictionaries);
- const {length, definitions} = await this.findTermsInternal(text, dictionaries, alphanumeric);
+ const [definitions, length] = await this.findTermsInternal(text, dictionaries, options.scanning.alphanumeric);
const definitionsGrouped = dictTermsGroup(definitions, dictionaries);
await this.buildTermFrequencies(definitionsGrouped, titles);
@@ -154,13 +168,14 @@ class Translator {
}
}
- return {length, definitions: definitionsGrouped};
+ return [definitionsGrouped, length];
}
- async findTermsMerged(text, dictionaries, alphanumeric, options) {
+ async findTermsMerged(text, options) {
+ const dictionaries = dictEnabledSet(options);
const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches);
const titles = Object.keys(dictionaries);
- const {length, definitions} = await this.findTermsInternal(text, dictionaries, alphanumeric);
+ const [definitions, length] = await this.findTermsInternal(text, dictionaries, options.scanning.alphanumeric);
const {sequencedDefinitions, defaultDefinitions} = await this.getSequencedDefinitions(definitions, options.general.mainDictionary);
const definitionsMerged = [];
const mergedByTermIndices = new Set();
@@ -191,23 +206,24 @@ class Translator {
}
}
- return {length, definitions: dictTermsSort(definitionsMerged)};
+ return [dictTermsSort(definitionsMerged), length];
}
- async findTermsSplit(text, dictionaries, alphanumeric) {
+ async findTermsSplit(text, options) {
+ const dictionaries = dictEnabledSet(options);
const titles = Object.keys(dictionaries);
- const {length, definitions} = await this.findTermsInternal(text, dictionaries, alphanumeric);
+ const [definitions, length] = await this.findTermsInternal(text, dictionaries, options.scanning.alphanumeric);
await this.buildTermFrequencies(definitions, titles);
- return {length, definitions};
+ return [definitions, length];
}
async findTermsInternal(text, dictionaries, alphanumeric) {
if (!alphanumeric && text.length > 0) {
const c = text[0];
if (!jpIsKana(c) && !jpIsKanji(c)) {
- return {length: 0, definitions: []};
+ return [[], 0];
}
}
@@ -246,7 +262,7 @@ class Translator {
length = Math.max(length, definition.source.length);
}
- return {length, definitions};
+ return [definitions, length];
}
async findTermDeinflections(text, text2, titles) {