diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bg/js/dictionary.js | 20 | ||||
-rw-r--r-- | ext/bg/js/translator.js | 16 |
2 files changed, 20 insertions, 16 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 67128725..7d6bf7ba 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -159,11 +159,14 @@ function dictTermsGroup(definitions, dictionaries) { } function dictTermsMergeBySequence(definitions, mainDictionary) { - const definitionsBySequence = {'-1': []}; + const sequencedDefinitions = new Map(); + const nonSequencedDefinitions = []; for (const definition of definitions) { - if (mainDictionary === definition.dictionary && definition.sequence >= 0) { - if (!definitionsBySequence[definition.sequence]) { - definitionsBySequence[definition.sequence] = { + const sequence = definition.sequence; + if (mainDictionary === definition.dictionary && sequence >= 0) { + sequencedDefinition = sequencedDefinitions.get(sequence); + if (typeof sequencedDefinition === 'undefined') { + sequencedDefinition = { reasons: definition.reasons, score: Number.MIN_SAFE_INTEGER, expression: new Set(), @@ -173,15 +176,16 @@ function dictTermsMergeBySequence(definitions, mainDictionary) { dictionary: definition.dictionary, definitions: [] }; + sequencedDefinitions.set(sequence, sequencedDefinition); + } else { + sequencedDefinition.score = Math.max(sequencedDefinition.score, definition.score); } - const score = Math.max(definitionsBySequence[definition.sequence].score, definition.score); - definitionsBySequence[definition.sequence].score = score; } else { - definitionsBySequence['-1'].push(definition); + nonSequencedDefinitions.push(definition); } } - return definitionsBySequence; + return [sequencedDefinitions, nonSequencedDefinitions]; } function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index dfec54ac..d32f8c67 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -48,14 +48,14 @@ class Translator { } async getSequencedDefinitions(definitions, mainDictionary) { - const definitionsBySequence = dictTermsMergeBySequence(definitions, mainDictionary); - const defaultDefinitions = definitionsBySequence['-1']; - - const sequenceList = Object.keys(definitionsBySequence).map((v) => Number(v)).filter((v) => v >= 0); - const sequencedDefinitions = sequenceList.map((key) => ({ - definitions: definitionsBySequence[key], - rawDefinitions: [] - })); + const [definitionsBySequence, defaultDefinitions] = dictTermsMergeBySequence(definitions, mainDictionary); + + const sequenceList = []; + const sequencedDefinitions = []; + for (const [key, value] of definitionsBySequence.entries()) { + sequenceList.push(key); + sequencedDefinitions.push({definitions: value, rawDefinitions: []}); + } for (const definition of await this.database.findTermsBySequenceBulk(sequenceList, mainDictionary)) { sequencedDefinitions[definition.index].rawDefinitions.push(definition); |