From f9774b4ce985b9920cee8afec0f756e5e1bfc9fa Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 18 Apr 2021 18:08:02 -0400 Subject: [PATCH] Improve dictionary sequence info (#1617) * Ensure negative sequence is always -1 * Expose sequence on definition objects * Update how sequence is exposed for definitions * Update test data * Update TS docs --- docs/interfaces/dictionary-entry.ts | 4 + ext/js/data/anki-note-data-creator.js | 3 +- ext/js/language/translator.js | 22 ++-- .../translator-test-results-note-data1.json | 20 ++-- test/data/translator-test-results.json | 105 ++++++++++++++++++ 5 files changed, 132 insertions(+), 22 deletions(-) diff --git a/docs/interfaces/dictionary-entry.ts b/docs/interfaces/dictionary-entry.ts index 230bada0..724a0b5a 100644 --- a/docs/interfaces/dictionary-entry.ts +++ b/docs/interfaces/dictionary-entry.ts @@ -296,6 +296,10 @@ namespace Translation { * The name of the dictionary that the definition information originated from. */ dictionary: string; + /** + * Database sequence number for the term. The value will be `-1` if there is no sequence. + */ + sequence: number; /** * Tags for the definition. */ diff --git a/ext/js/data/anki-note-data-creator.js b/ext/js/data/anki-note-data-creator.js index e3ff2ca0..1cd5aaf4 100644 --- a/ext/js/data/anki-note-data-creator.js +++ b/ext/js/data/anki-note-data-creator.js @@ -354,7 +354,7 @@ class AnkiNoteDataCreator { const definitions = []; const definitionTags = []; - for (const {tags, headwordIndices, entries, dictionary} of dictionaryEntry.definitions) { + for (const {tags, headwordIndices, entries, dictionary, sequence} of dictionaryEntry.definitions) { const definitionTags2 = []; for (const tag of tags) { definitionTags.push(this._convertTag(tag)); @@ -362,7 +362,6 @@ class AnkiNoteDataCreator { } if (!hasDefinitions) { continue; } const only = merged ? DictionaryDataUtil.getDisambiguations(dictionaryEntry.headwords, headwordIndices, allTermsSet, allReadingsSet) : void 0; - const {sequence} = dictionaryEntry; definitions.push({ sequence, dictionary, diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index 28634f9c..1e335ac3 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -927,8 +927,8 @@ class Translator { return {index, term, reading, sources, tags, wordClasses}; } - _createTermDefinition(index, headwordIndices, dictionary, tags, entries) { - return {index, headwordIndices, dictionary, tags, entries}; + _createTermDefinition(index, headwordIndices, dictionary, sequence, tags, entries) { + return {index, headwordIndices, dictionary, sequence, tags, entries}; } _createTermPronunciation(index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches) { @@ -960,12 +960,14 @@ class Translator { } _createTermDictionaryEntryFromDatabaseEntry(databaseEntry, originalText, transformedText, deinflectedText, reasons, isPrimary, enabledDictionaryMap) { - const {term, reading: rawReading, definitionTags, termTags, definitions, score, dictionary, id, sequence, rules} = databaseEntry; + const {term, reading: rawReading, definitionTags, termTags, definitions, score, dictionary, id, sequence: rawSequence, rules} = databaseEntry; const reading = (rawReading.length > 0 ? rawReading : term); const {index: dictionaryIndex, priority: dictionaryPriority} = this._getDictionaryOrder(dictionary, enabledDictionaryMap); const sourceTermExactMatchCount = (isPrimary && deinflectedText === term ? 1 : 0); const source = this._createSource(originalText, transformedText, deinflectedText, isPrimary); const maxTransformedTextLength = transformedText.length; + const hasSequence = (rawSequence >= 0); + const sequence = hasSequence ? rawSequence : -1; const headwordTagGroups = []; const definitionTagGroups = []; @@ -976,7 +978,7 @@ class Translator { id, isPrimary, sequence, - sequence >= 0 ? dictionary : null, + hasSequence ? dictionary : null, reasons, score, dictionaryIndex, @@ -984,7 +986,7 @@ class Translator { sourceTermExactMatchCount, maxTransformedTextLength, [this._createTermHeadword(0, term, reading, [source], headwordTagGroups, rules)], - [this._createTermDefinition(0, [0], dictionary, definitionTagGroups, definitions)] + [this._createTermDefinition(0, [0], dictionary, sequence, definitionTagGroups, definitions)] ); } @@ -1148,21 +1150,21 @@ class Translator { } _addTermDefinitions(definitions, newDefinitions, headwordIndexMap) { - for (const {headwordIndices, dictionary, tags, entries} of newDefinitions) { + for (const {headwordIndices, dictionary, sequence, tags, entries} of newDefinitions) { const headwordIndicesNew = []; for (const headwordIndex of headwordIndices) { headwordIndicesNew.push(headwordIndexMap[headwordIndex]); } - definitions.push(this._createTermDefinition(definitions.length, headwordIndicesNew, dictionary, tags, entries)); + definitions.push(this._createTermDefinition(definitions.length, headwordIndicesNew, dictionary, sequence, tags, entries)); } } _addTermDefinitions2(definitions, definitionsMap, newDefinitions, headwordIndexMap) { - for (const {headwordIndices, dictionary, tags, entries} of newDefinitions) { - const key = this._createMapKey([dictionary, ...entries]); + for (const {headwordIndices, dictionary, sequence, 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, [], [...entries]); + definition = this._createTermDefinition(definitions.length, [], dictionary, sequence, [], [...entries]); definitions.push(definition); definitionsMap.set(key, definition); } diff --git a/test/data/translator-test-results-note-data1.json b/test/data/translator-test-results-note-data1.json index 2709fcb5..fb20962d 100644 --- a/test/data/translator-test-results-note-data1.json +++ b/test/data/translator-test-results-note-data1.json @@ -8826,7 +8826,7 @@ ], "definitions": [ { - "sequence": -1, + "sequence": 4, "dictionary": "Test Dictionary 2", "glossary": [ "definition13", @@ -8854,7 +8854,7 @@ ] }, { - "sequence": -1, + "sequence": 4, "dictionary": "Test Dictionary 2", "glossary": [ "definition15", @@ -9154,7 +9154,7 @@ ], "definitions": [ { - "sequence": -1, + "sequence": 4, "dictionary": "Test Dictionary 2", "glossary": [ "definition17", @@ -9182,7 +9182,7 @@ ] }, { - "sequence": -1, + "sequence": 4, "dictionary": "Test Dictionary 2", "glossary": [ "definition19", @@ -9454,7 +9454,7 @@ ], "definitions": [ { - "sequence": -1, + "sequence": 3, "dictionary": "Test Dictionary 2", "glossary": [ "definition5", @@ -9482,7 +9482,7 @@ ] }, { - "sequence": -1, + "sequence": 3, "dictionary": "Test Dictionary 2", "glossary": [ "definition7", @@ -9706,7 +9706,7 @@ ], "definitions": [ { - "sequence": -1, + "sequence": 3, "dictionary": "Test Dictionary 2", "glossary": [ "definition9", @@ -9734,7 +9734,7 @@ ] }, { - "sequence": -1, + "sequence": 3, "dictionary": "Test Dictionary 2", "glossary": [ "definition11", @@ -9950,7 +9950,7 @@ ], "definitions": [ { - "sequence": -1, + "sequence": 1, "dictionary": "Test Dictionary 2", "glossary": [ "definition1", @@ -10166,7 +10166,7 @@ ], "definitions": [ { - "sequence": -1, + "sequence": 2, "dictionary": "Test Dictionary 2", "glossary": [ "definition3", diff --git a/test/data/translator-test-results.json b/test/data/translator-test-results.json index afaf73aa..fa4df674 100644 --- a/test/data/translator-test-results.json +++ b/test/data/translator-test-results.json @@ -324,6 +324,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -456,6 +457,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -596,6 +598,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -730,6 +733,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -864,6 +868,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -998,6 +1003,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -1130,6 +1136,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -1262,6 +1269,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -1402,6 +1410,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -1554,6 +1563,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -1702,6 +1712,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -1854,6 +1865,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -2008,6 +2020,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -2144,6 +2157,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -2280,6 +2294,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -2416,6 +2431,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -2548,6 +2564,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -2680,6 +2697,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -2818,6 +2836,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 5, "tags": [ { "name": "tag1", @@ -2946,6 +2965,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -3084,6 +3104,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -3224,6 +3245,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -3358,6 +3380,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -3498,6 +3521,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -3632,6 +3656,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -3772,6 +3797,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -3920,6 +3946,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -4074,6 +4101,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -4210,6 +4238,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -4350,6 +4379,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -4502,6 +4532,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -4656,6 +4687,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -4792,6 +4824,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -4930,6 +4963,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 5, "tags": [ { "name": "tag1", @@ -5030,6 +5064,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [], "entries": [ "definition13", @@ -5078,6 +5113,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [], "entries": [ "definition17", @@ -5126,6 +5162,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [], "entries": [ "definition15", @@ -5174,6 +5211,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [], "entries": [ "definition19", @@ -5224,6 +5262,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [], "entries": [ "definition5", @@ -5274,6 +5313,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [], "entries": [ "definition9", @@ -5324,6 +5364,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [], "entries": [ "definition7", @@ -5374,6 +5415,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [], "entries": [ "definition11", @@ -5420,6 +5462,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [], "entries": [ "definition1", @@ -5466,6 +5509,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [], "entries": [ "definition3", @@ -5582,6 +5626,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -5621,6 +5666,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -5773,6 +5819,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -5812,6 +5859,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -5966,6 +6014,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -6005,6 +6054,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -6141,6 +6191,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -6180,6 +6231,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -6312,6 +6364,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -6444,6 +6497,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -6663,6 +6717,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -6702,6 +6757,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -6741,6 +6797,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -6780,6 +6837,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -7026,6 +7084,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -7065,6 +7124,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -7104,6 +7164,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -7143,6 +7204,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -7293,6 +7355,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -7425,6 +7488,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -7569,6 +7633,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -7725,6 +7790,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -7877,6 +7943,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -8033,6 +8100,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -8187,6 +8255,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -8323,6 +8392,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -8459,6 +8529,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -8595,6 +8666,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -8727,6 +8799,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -8859,6 +8932,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -8999,6 +9073,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -9151,6 +9226,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -9299,6 +9375,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -9451,6 +9528,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -9605,6 +9683,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -9741,6 +9820,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -9877,6 +9957,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -10013,6 +10094,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -10145,6 +10227,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -10277,6 +10360,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -10417,6 +10501,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -10569,6 +10654,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -10717,6 +10803,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -10869,6 +10956,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -11023,6 +11111,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -11159,6 +11248,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -11295,6 +11385,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -11431,6 +11522,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -11563,6 +11655,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 1, "tags": [ { "name": "tag1", @@ -11695,6 +11788,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 2, "tags": [ { "name": "tag1", @@ -11811,6 +11905,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 6, "tags": [ { "name": "popular", @@ -11878,6 +11973,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 7, "tags": [ { "name": "popular", @@ -11961,6 +12057,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 6, "tags": [ { "name": "popular", @@ -12147,6 +12244,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -12186,6 +12284,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -12225,6 +12324,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -12264,6 +12364,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 4, "tags": [ { "name": "tag1", @@ -12510,6 +12611,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -12549,6 +12651,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -12588,6 +12691,7 @@ 0 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1", @@ -12627,6 +12731,7 @@ 1 ], "dictionary": "Test Dictionary 2", + "sequence": 3, "tags": [ { "name": "tag1",