Translator sorting updates (#1755)

* Update when definition sorting takes place

* Update test data

* Change order of sorting
This commit is contained in:
toasted-nutbread 2021-06-26 12:37:38 -04:00 committed by GitHub
parent 6e0a367afc
commit 845070b817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 43 deletions

View File

@ -108,16 +108,21 @@ class Translator {
this._removeExcludedDefinitions(dictionaryEntries, excludeDictionaryDefinitions); this._removeExcludedDefinitions(dictionaryEntries, excludeDictionaryDefinitions);
} }
if (dictionaryEntries.length > 1) {
this._sortTermDictionaryEntries(dictionaryEntries);
}
if (mode === 'simple') { if (mode === 'simple') {
this._clearTermTags(dictionaryEntries); this._clearTermTags(dictionaryEntries);
} else { } else {
await this._addTermMeta(dictionaryEntries, enabledDictionaryMap); await this._addTermMeta(dictionaryEntries, enabledDictionaryMap);
await this._expandTermTags(dictionaryEntries); await this._expandTermTags(dictionaryEntries);
this._sortTermDictionaryEntryData(dictionaryEntries); }
if (dictionaryEntries.length > 1) {
this._sortTermDictionaryEntries(dictionaryEntries);
}
for (const {definitions, frequencies, pronunciations} of dictionaryEntries) {
this._flagRedundantDefinitionTags(definitions);
if (definitions.length > 1) { this._sortTermDictionaryEntryDefinitions(definitions); }
if (frequencies.length > 1) { this._sortTermDictionaryEntrySimpleData(frequencies); }
if (pronunciations.length > 1) { this._sortTermDictionaryEntrySimpleData(pronunciations); }
} }
return {dictionaryEntries, originalTextLength}; return {dictionaryEntries, originalTextLength};
@ -1090,9 +1095,7 @@ class Translator {
} }
// Sort // Sort
if (definitionEntries.length > 1) { if (definitionEntries.length <= 1) {
this._sortTermDefinitionEntries(definitionEntries);
} else {
checkDuplicateDefinitions = false; checkDuplicateDefinitions = false;
} }
@ -1330,11 +1333,8 @@ class Translator {
dictionaryEntries.sort(compareFunction); dictionaryEntries.sort(compareFunction);
} }
_sortTermDefinitionEntries(definitionEntries) { _sortTermDictionaryEntryDefinitions(definitions) {
const compareFunction = (e1, e2) => { const compareFunction = (v1, v2) => {
const v1 = e1.dictionaryEntry;
const v2 = e2.dictionaryEntry;
// Sort by dictionary priority // Sort by dictionary priority
let i = v2.dictionaryPriority - v1.dictionaryPriority; let i = v2.dictionaryPriority - v1.dictionaryPriority;
if (i !== 0) { return i; } if (i !== 0) { return i; }
@ -1344,20 +1344,14 @@ class Translator {
if (i !== 0) { return i; } if (i !== 0) { return i; }
// Sort by definition headword index // Sort by definition headword index
const definitions1 = v1.definitions; const headwordIndices1 = v1.headwordIndices;
const definitions2 = v2.definitions; const headwordIndices2 = v2.headwordIndices;
const headwordIndexMap1 = e1.headwordIndexMap; const jj = headwordIndices1.length;
const headwordIndexMap2 = e2.headwordIndexMap; i = headwordIndices2.length - jj;
for (let j = 0, jj = Math.min(definitions1.length, definitions2.length); j < jj; ++j) { if (i !== 0) { return i; }
const headwordIndices1 = definitions1[j].headwordIndices; for (let j = 0; j < jj; ++j) {
const headwordIndices2 = definitions2[j].headwordIndices; i = headwordIndices1[j] - headwordIndices2[j];
const kk = headwordIndices1.length;
i = headwordIndices2.length - kk;
if (i !== 0) { return i; } if (i !== 0) { return i; }
for (let k = 0; k < kk; ++k) {
i = headwordIndexMap1[headwordIndices1[k]] - headwordIndexMap2[headwordIndices2[k]];
if (i !== 0) { return i; }
}
} }
// Sort by dictionary order // Sort by dictionary order
@ -1365,10 +1359,10 @@ class Translator {
if (i !== 0) { return i; } if (i !== 0) { return i; }
// Sort by original order // Sort by original order
i = e1.index - e2.index; i = v1.index - v2.index;
return i; return i;
}; };
definitionEntries.sort(compareFunction); definitions.sort(compareFunction);
} }
_sortTermDictionaryEntriesById(dictionaryEntries) { _sortTermDictionaryEntriesById(dictionaryEntries) {
@ -1376,7 +1370,7 @@ class Translator {
dictionaryEntries.sort((a, b) => a.definitions[0].id - b.definitions[0].id); dictionaryEntries.sort((a, b) => a.definitions[0].id - b.definitions[0].id);
} }
_sortTermDictionaryEntryData(dictionaryEntries) { _sortTermDictionaryEntrySimpleData(dataList) {
const compare = (v1, v2) => { const compare = (v1, v2) => {
// Sort by dictionary priority // Sort by dictionary priority
let i = v2.dictionaryPriority - v1.dictionaryPriority; let i = v2.dictionaryPriority - v1.dictionaryPriority;
@ -1394,12 +1388,7 @@ class Translator {
i = v1.index - v2.index; i = v1.index - v2.index;
return i; return i;
}; };
dataList.sort(compare);
for (const {definitions, frequencies, pronunciations} of dictionaryEntries) {
this._flagRedundantDefinitionTags(definitions);
frequencies.sort(compare);
pronunciations.sort(compare);
}
} }
_sortKanjiDictionaryEntryData(dictionaryEntries) { _sortKanjiDictionaryEntryData(dictionaryEntries) {

View File

@ -5854,7 +5854,7 @@
] ]
}, },
{ {
"index": 1, "index": 2,
"headwordIndices": [ "headwordIndices": [
1 1
], ],
@ -5888,7 +5888,7 @@
] ]
}, },
{ {
"index": 2, "index": 1,
"headwordIndices": [ "headwordIndices": [
0 0
], ],
@ -6194,7 +6194,7 @@
] ]
}, },
{ {
"index": 1, "index": 2,
"headwordIndices": [ "headwordIndices": [
1 1
], ],
@ -6228,7 +6228,7 @@
] ]
}, },
{ {
"index": 2, "index": 1,
"headwordIndices": [ "headwordIndices": [
0 0
], ],
@ -10642,7 +10642,7 @@
] ]
}, },
{ {
"index": 1, "index": 2,
"headwordIndices": [ "headwordIndices": [
1 1
], ],
@ -10676,7 +10676,7 @@
] ]
}, },
{ {
"index": 2, "index": 1,
"headwordIndices": [ "headwordIndices": [
0 0
], ],
@ -10982,7 +10982,7 @@
] ]
}, },
{ {
"index": 1, "index": 2,
"headwordIndices": [ "headwordIndices": [
1 1
], ],
@ -11016,7 +11016,7 @@
] ]
}, },
{ {
"index": 2, "index": 1,
"headwordIndices": [ "headwordIndices": [
0 0
], ],