From 03dd1dc6ff808390fed7e770822a961c4db3fb2a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 18 Apr 2021 18:11:09 -0400 Subject: Remove redundant dictionary entry sequence (#1618) * Remove sequence/sequenceDictionary from dictionary entry objects * Expose isPrimary on definitions * Update sequence * Update test data --- ext/js/data/anki-note-data-creator.js | 22 +++++++++++++++++++-- ext/js/language/translator.js | 36 ++++++++++++++++------------------- 2 files changed, 36 insertions(+), 22 deletions(-) (limited to 'ext/js') diff --git a/ext/js/data/anki-note-data-creator.js b/ext/js/data/anki-note-data-creator.js index 1cd5aaf4..f4d6ab49 100644 --- a/ext/js/data/anki-note-data-creator.js +++ b/ext/js/data/anki-note-data-creator.js @@ -265,7 +265,7 @@ class AnkiNoteDataCreator { case 'merge': type = 'termMerged'; break; } - const {id, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, sequence} = dictionaryEntry; + const {id, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount} = dictionaryEntry; const { screenshotFileName=null, @@ -288,6 +288,7 @@ class AnkiNoteDataCreator { const glossary = this.createCachedValue(this._getTermGlossaryArray.bind(this, dictionaryEntry, type)); const cloze = this.createCachedValue(this._getCloze.bind(this, dictionaryEntry, context)); const furiganaSegments = this.createCachedValue(this._getTermFuriganaSegments.bind(this, dictionaryEntry, type)); + const sequence = this.createCachedValue(this._getTermDictionaryEntrySequence.bind(this, dictionaryEntry)); return { type, @@ -298,7 +299,7 @@ class AnkiNoteDataCreator { reasons: inflections, score, isPrimary: (type === 'term' ? dictionaryEntry.isPrimary : void 0), - sequence, + get sequence() { return self.getCachedValue(sequence); }, get dictionary() { return self.getCachedValue(dictionaryNames)[0]; }, dictionaryOrder: { index: dictionaryIndex, @@ -601,4 +602,21 @@ class AnkiNoteDataCreator { } return result; } + + _getTermDictionaryEntrySequence(dictionaryEntry) { + let hasSequence = false; + let mainSequence = -1; + for (const {sequence, isPrimary} of dictionaryEntry.definitions) { + if (!isPrimary) { continue; } + if (!hasSequence) { + mainSequence = sequence; + hasSequence = true; + if (mainSequence === -1) { break; } + } else if (mainSequence !== sequence) { + mainSequence = -1; + break; + } + } + return mainSequence; + } } diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index 1e335ac3..04c78258 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -350,13 +350,11 @@ class Translator { const groupedDictionaryEntriesMap = new Map(); const ungroupedDictionaryEntriesMap = new Map(); for (const dictionaryEntry of dictionaryEntries) { - const {id, sequence, definitions: [{dictionary}]} = dictionaryEntry; + const {id, definitions: [{dictionary, sequence}]} = dictionaryEntry; if (mainDictionary === dictionary && sequence >= 0) { let group = groupedDictionaryEntriesMap.get(sequence); if (typeof group === 'undefined') { group = { - sequence, - sequenceDictionary: dictionary, ids: new Set(), dictionaryEntries: [] }; @@ -384,7 +382,7 @@ class Translator { const newDictionaryEntries = []; for (const group of groupedDictionaryEntries) { - newDictionaryEntries.push(this._createGroupedDictionaryEntry(group.dictionaryEntries, group.sequence, group.sequenceDictionary, true)); + newDictionaryEntries.push(this._createGroupedDictionaryEntry(group.dictionaryEntries, true)); } newDictionaryEntries.push(...this._groupDictionaryEntriesByHeadword(ungroupedDictionaryEntriesMap.values())); return newDictionaryEntries; @@ -486,7 +484,7 @@ class Translator { const results = []; for (const dictionaryEntries2 of groups.values()) { - const dictionaryEntry = this._createGroupedDictionaryEntry(dictionaryEntries2, -1, null, false); + const dictionaryEntry = this._createGroupedDictionaryEntry(dictionaryEntries2, false); results.push(dictionaryEntry); } return results; @@ -927,8 +925,8 @@ class Translator { return {index, term, reading, sources, tags, wordClasses}; } - _createTermDefinition(index, headwordIndices, dictionary, sequence, tags, entries) { - return {index, headwordIndices, dictionary, sequence, tags, entries}; + _createTermDefinition(index, headwordIndices, dictionary, sequence, isPrimary, tags, entries) { + return {index, headwordIndices, dictionary, sequence, isPrimary, tags, entries}; } _createTermPronunciation(index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches) { @@ -939,13 +937,11 @@ class Translator { return {index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency}; } - _createTermDictionaryEntry(id, isPrimary, sequence, sequenceDictionary, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, maxTransformedTextLength, headwords, definitions) { + _createTermDictionaryEntry(id, isPrimary, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, maxTransformedTextLength, headwords, definitions) { return { type: 'term', id, isPrimary, - sequence, - sequenceDictionary, inflections, score, dictionaryIndex, @@ -977,8 +973,6 @@ class Translator { return this._createTermDictionaryEntry( id, isPrimary, - sequence, - hasSequence ? dictionary : null, reasons, score, dictionaryIndex, @@ -986,11 +980,11 @@ class Translator { sourceTermExactMatchCount, maxTransformedTextLength, [this._createTermHeadword(0, term, reading, [source], headwordTagGroups, rules)], - [this._createTermDefinition(0, [0], dictionary, sequence, definitionTagGroups, definitions)] + [this._createTermDefinition(0, [0], dictionary, sequence, isPrimary, definitionTagGroups, definitions)] ); } - _createGroupedDictionaryEntry(dictionaryEntries, sequence, sequenceDictionary, checkDuplicateDefinitions) { + _createGroupedDictionaryEntry(dictionaryEntries, checkDuplicateDefinitions) { // Headwords are generated before sorting, so that the order of dictionaryEntries can be maintained const definitionEntries = []; const headwords = new Map(); @@ -1040,8 +1034,6 @@ class Translator { return this._createTermDictionaryEntry( -1, isPrimary, - sequence, - sequenceDictionary, inflections !== null ? inflections : [], score, dictionaryIndex, @@ -1150,23 +1142,27 @@ class Translator { } _addTermDefinitions(definitions, newDefinitions, headwordIndexMap) { - for (const {headwordIndices, dictionary, sequence, tags, entries} of newDefinitions) { + for (const {headwordIndices, dictionary, sequence, isPrimary, tags, entries} of newDefinitions) { const headwordIndicesNew = []; for (const headwordIndex of headwordIndices) { headwordIndicesNew.push(headwordIndexMap[headwordIndex]); } - definitions.push(this._createTermDefinition(definitions.length, headwordIndicesNew, dictionary, sequence, tags, entries)); + definitions.push(this._createTermDefinition(definitions.length, headwordIndicesNew, dictionary, sequence, isPrimary, tags, entries)); } } _addTermDefinitions2(definitions, definitionsMap, newDefinitions, headwordIndexMap) { - for (const {headwordIndices, dictionary, sequence, tags, entries} of newDefinitions) { + for (const {headwordIndices, dictionary, sequence, isPrimary, tags, entries} of newDefinitions) { const key = this._createMapKey([dictionary, sequence, ...entries]); let definition = definitionsMap.get(key); if (typeof definition === 'undefined') { - definition = this._createTermDefinition(definitions.length, [], dictionary, sequence, [], [...entries]); + definition = this._createTermDefinition(definitions.length, [], dictionary, sequence, isPrimary, [], [...entries]); definitions.push(definition); definitionsMap.set(key, definition); + } else { + if (isPrimary) { + definition.isPrimary = true; + } } const newHeadwordIndices = definition.headwordIndices; -- cgit v1.2.3