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

View File

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