This commit is contained in:
toasted-nutbread 2020-02-15 16:17:11 -05:00
parent 5e8b408a23
commit 89af73b61b
2 changed files with 15 additions and 15 deletions

View File

@ -175,10 +175,10 @@ function dictTermsMergeBySequence(definitions, mainDictionary) {
return [sequencedDefinitions, nonSequencedDefinitions]; return [sequencedDefinitions, nonSequencedDefinitions];
} }
function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices=null) {
const definitionsByGloss = appendTo || {}; const definitionsByGloss = appendTo !== null ? appendTo : new Map();
for (const [index, definition] of definitions.entries()) { for (const [index, definition] of definitions.entries()) {
if (appendTo) { if (appendTo !== null) {
let match = false; let match = false;
for (const expression of result.expressions.keys()) { for (const expression of result.expressions.keys()) {
if (definition.expression === expression) { if (definition.expression === expression) {
@ -196,14 +196,15 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) {
if (!match) { if (!match) {
continue; continue;
} else if (mergedIndices) { } else if (mergedIndices !== null) {
mergedIndices.add(index); mergedIndices.add(index);
} }
} }
const gloss = JSON.stringify(definition.glossary.concat(definition.dictionary)); const gloss = JSON.stringify(definition.glossary.concat(definition.dictionary));
if (!definitionsByGloss[gloss]) { let glossDefinition = definitionsByGloss.get(gloss);
definitionsByGloss[gloss] = { if (typeof glossDefinition === 'undefined') {
glossDefinition = {
expression: new Set(), expression: new Set(),
reading: new Set(), reading: new Set(),
definitionTags: [], definitionTags: [],
@ -214,21 +215,22 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) {
id: definition.id, id: definition.id,
dictionary: definition.dictionary dictionary: definition.dictionary
}; };
definitionsByGloss.set(gloss, glossDefinition);
} }
definitionsByGloss[gloss].expression.add(definition.expression); glossDefinition.expression.add(definition.expression);
definitionsByGloss[gloss].reading.add(definition.reading); glossDefinition.reading.add(definition.reading);
result.expression.add(definition.expression); result.expression.add(definition.expression);
result.reading.add(definition.reading); result.reading.add(definition.reading);
for (const tag of definition.definitionTags) { for (const tag of definition.definitionTags) {
if (!definitionsByGloss[gloss].definitionTags.find((existingTag) => existingTag.name === tag.name)) { if (!glossDefinition.definitionTags.find((existingTag) => existingTag.name === tag.name)) {
definitionsByGloss[gloss].definitionTags.push(tag); glossDefinition.definitionTags.push(tag);
} }
} }
if (!appendTo) { if (appendTo === null) {
// result->expressions[ Expression1[ Reading1[ Tag1, Tag2 ] ], Expression2, ... ] // result->expressions[ Expression1[ Reading1[ Tag1, Tag2 ] ], Expression2, ... ]
if (!result.expressions.has(definition.expression)) { if (!result.expressions.has(definition.expression)) {
result.expressions.set(definition.expression, new Map()); result.expressions.set(definition.expression, new Map());
@ -245,8 +247,7 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) {
} }
} }
for (const gloss in definitionsByGloss) { for (const definition of definitionsByGloss.values()) {
const definition = definitionsByGloss[gloss];
definition.only = []; definition.only = [];
if (!utilSetEqual(definition.expression, result.expression)) { if (!utilSetEqual(definition.expression, result.expression)) {
for (const expression of utilSetIntersection(definition.expression, result.expression)) { for (const expression of utilSetIntersection(definition.expression, result.expression)) {

View File

@ -118,8 +118,7 @@ class Translator {
dictTermsMergeByGloss(result, defaultDefinitions.concat(secondarySearchResults), definitionsByGloss, mergedByTermIndices); dictTermsMergeByGloss(result, defaultDefinitions.concat(secondarySearchResults), definitionsByGloss, mergedByTermIndices);
for (const gloss in definitionsByGloss) { for (const definition of definitionsByGloss.values()) {
const definition = definitionsByGloss[gloss];
dictTagsSort(definition.definitionTags); dictTagsSort(definition.definitionTags);
result.definitions.push(definition); result.definitions.push(definition);
} }