Skip to content

Commit

Permalink
Improve term dictionary entry sequence (FooSoft#1591)
Browse files Browse the repository at this point in the history
* Improve sequence for merged entries and add sequenceDictionary

* Update docs

* Expose sequence in definitions

* Expose sequence in root definition

* Update test data
  • Loading branch information
toasted-nutbread committed Apr 15, 2021
1 parent 19aa68b commit b96f071
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 14 deletions.
4 changes: 4 additions & 0 deletions docs/interfaces/dictionary-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ namespace Translation {
* Database sequence number for the term, or `-1` if multiple entries have been merged.
*/
sequence: number;
/**
* The dictionary that the sequence number originated from, or `null` if there is no sequence.
*/
sequenceDictionary: string;
/**
* A list of inflections that was applied to get the term.
*/
Expand Down
6 changes: 4 additions & 2 deletions ext/js/data/anki-note-data-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class AnkiNoteDataCreator {
case 'merge': type = 'termMerged'; break;
}

const {id, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount} = dictionaryEntry;
const {id, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, sequence} = dictionaryEntry;

const {
screenshotFileName=null,
Expand Down Expand Up @@ -298,7 +298,7 @@ class AnkiNoteDataCreator {
reasons: inflections,
score,
isPrimary: (type === 'term' ? dictionaryEntry.isPrimary : void 0),
sequence: (type === 'term' ? dictionaryEntry.sequence : void 0),
sequence,
get dictionary() { return self.getCachedValue(dictionaryNames)[0]; },
dictionaryOrder: {
index: dictionaryIndex,
Expand Down Expand Up @@ -362,7 +362,9 @@ class AnkiNoteDataCreator {
}
if (!hasDefinitions) { continue; }
const only = merged ? DictionaryDataUtil.getDisambiguations(dictionaryEntry.headwords, headwordIndices, allTermsSet, allReadingsSet) : void 0;
const {sequence} = dictionaryEntry;
definitions.push({
sequence,
dictionary,
glossary: entries,
definitionTags: definitionTags2,
Expand Down
20 changes: 14 additions & 6 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,12 @@ class Translator {
if (mainDictionary === dictionary && sequence >= 0) {
let group = groupedDictionaryEntriesMap.get(sequence);
if (typeof group === 'undefined') {
group = {ids: new Set(), dictionaryEntries: []};
group = {
sequence,
sequenceDictionary: dictionary,
ids: new Set(),
dictionaryEntries: []
};
sequenceList.push({query: sequence, dictionary});
groupedDictionaryEntries.push(group);
groupedDictionaryEntriesMap.set(sequence, group);
Expand All @@ -378,7 +383,7 @@ class Translator {

const newDictionaryEntries = [];
for (const group of groupedDictionaryEntries) {
newDictionaryEntries.push(this._createGroupedDictionaryEntry(group.dictionaryEntries, true));
newDictionaryEntries.push(this._createGroupedDictionaryEntry(group.dictionaryEntries, group.sequence, group.sequenceDictionary, true));
}
newDictionaryEntries.push(...this._groupDictionaryEntriesByHeadword(ungroupedDictionaryEntriesMap.values()));
return newDictionaryEntries;
Expand Down Expand Up @@ -480,7 +485,7 @@ class Translator {

const results = [];
for (const dictionaryEntries2 of groups.values()) {
const dictionaryEntry = this._createGroupedDictionaryEntry(dictionaryEntries2, false);
const dictionaryEntry = this._createGroupedDictionaryEntry(dictionaryEntries2, -1, null, false);
results.push(dictionaryEntry);
}
return results;
Expand Down Expand Up @@ -933,12 +938,13 @@ class Translator {
return {index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency};
}

_createTermDictionaryEntry(id, isPrimary, sequence, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, maxTransformedTextLength, headwords, definitions) {
_createTermDictionaryEntry(id, isPrimary, sequence, sequenceDictionary, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, maxTransformedTextLength, headwords, definitions) {
return {
type: 'term',
id,
isPrimary,
sequence,
sequenceDictionary,
inflections,
score,
dictionaryIndex,
Expand Down Expand Up @@ -969,6 +975,7 @@ class Translator {
id,
isPrimary,
sequence,
sequence >= 0 ? dictionary : null,
reasons,
score,
dictionaryIndex,
Expand All @@ -980,7 +987,7 @@ class Translator {
);
}

_createGroupedDictionaryEntry(dictionaryEntries, checkDuplicateDefinitions) {
_createGroupedDictionaryEntry(dictionaryEntries, sequence, sequenceDictionary, checkDuplicateDefinitions) {
// Headwords are generated before sorting, so that the order of dictionaryEntries can be maintained
const definitionEntries = [];
const headwords = new Map();
Expand Down Expand Up @@ -1030,7 +1037,8 @@ class Translator {
return this._createTermDictionaryEntry(
-1,
isPrimary,
-1,
sequence,
sequenceDictionary,
inflections !== null ? inflections : [],
score,
dictionaryIndex,
Expand Down
Loading

0 comments on commit b96f071

Please sign in to comment.