Simplify getSequencedDefinitions and dictTermsMergeBySequence

This commit is contained in:
toasted-nutbread 2020-01-28 18:58:14 -05:00
parent 09a1214f49
commit 1d9332cb69
2 changed files with 19 additions and 15 deletions

View File

@ -159,11 +159,14 @@ function dictTermsGroup(definitions, dictionaries) {
} }
function dictTermsMergeBySequence(definitions, mainDictionary) { function dictTermsMergeBySequence(definitions, mainDictionary) {
const definitionsBySequence = {'-1': []}; const sequencedDefinitions = new Map();
const nonSequencedDefinitions = [];
for (const definition of definitions) { for (const definition of definitions) {
if (mainDictionary === definition.dictionary && definition.sequence >= 0) { const sequence = definition.sequence;
if (!definitionsBySequence[definition.sequence]) { if (mainDictionary === definition.dictionary && sequence >= 0) {
definitionsBySequence[definition.sequence] = { sequencedDefinition = sequencedDefinitions.get(sequence);
if (typeof sequencedDefinition === 'undefined') {
sequencedDefinition = {
reasons: definition.reasons, reasons: definition.reasons,
score: Number.MIN_SAFE_INTEGER, score: Number.MIN_SAFE_INTEGER,
expression: new Set(), expression: new Set(),
@ -173,15 +176,16 @@ function dictTermsMergeBySequence(definitions, mainDictionary) {
dictionary: definition.dictionary, dictionary: definition.dictionary,
definitions: [] 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 { } else {
definitionsBySequence['-1'].push(definition); nonSequencedDefinitions.push(definition);
} }
} }
return definitionsBySequence; return [sequencedDefinitions, nonSequencedDefinitions];
} }
function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) {

View File

@ -48,14 +48,14 @@ class Translator {
} }
async getSequencedDefinitions(definitions, mainDictionary) { async getSequencedDefinitions(definitions, mainDictionary) {
const definitionsBySequence = dictTermsMergeBySequence(definitions, mainDictionary); const [definitionsBySequence, defaultDefinitions] = dictTermsMergeBySequence(definitions, mainDictionary);
const defaultDefinitions = definitionsBySequence['-1'];
const sequenceList = Object.keys(definitionsBySequence).map((v) => Number(v)).filter((v) => v >= 0); const sequenceList = [];
const sequencedDefinitions = sequenceList.map((key) => ({ const sequencedDefinitions = [];
definitions: definitionsBySequence[key], for (const [key, value] of definitionsBySequence.entries()) {
rawDefinitions: [] sequenceList.push(key);
})); sequencedDefinitions.push({definitions: value, rawDefinitions: []});
}
for (const definition of await this.database.findTermsBySequenceBulk(sequenceList, mainDictionary)) { for (const definition of await this.database.findTermsBySequenceBulk(sequenceList, mainDictionary)) {
sequencedDefinitions[definition.index].rawDefinitions.push(definition); sequencedDefinitions[definition.index].rawDefinitions.push(definition);