diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-02-26 23:55:32 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-26 23:55:32 -0500 |
commit | 9e7a76a1f121ad3622f9efd2867684560a9113db (patch) | |
tree | 78373f0ed31da172e41b8ba7838c15fc1972edc0 /ext/js | |
parent | 0bf0620c3579a5fe94c529673db105a83d6c3755 (diff) |
Improve dictionary priority (#1447)
* Refactor _sortDefinitions
* Remove use of useDictionaryPriority
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/language/translator.js | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index fc71bf7f..61d76b36 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -149,7 +149,7 @@ class Translator { async _findTermsSimple(text, options) { const {enabledDictionaryMap} = options; const [definitions, length] = await this._findTermsInternal(text, enabledDictionaryMap, options); - this._sortDefinitions(definitions, false); + this._sortDefinitions(definitions); return [definitions, length]; } @@ -157,7 +157,7 @@ class Translator { const {enabledDictionaryMap} = options; const [definitions, length] = await this._findTermsInternal(text, enabledDictionaryMap, options); await this._buildTermMeta(definitions, enabledDictionaryMap); - this._sortDefinitions(definitions, true); + this._sortDefinitions(definitions); return [definitions, length]; } @@ -167,7 +167,7 @@ class Translator { const groupedDefinitions = this._groupTerms(definitions, enabledDictionaryMap); await this._buildTermMeta(groupedDefinitions, enabledDictionaryMap); - this._sortDefinitions(groupedDefinitions, false); + this._sortDefinitions(groupedDefinitions); for (const definition of groupedDefinitions) { this._flagRedundantDefinitionTags(definition.definitions); @@ -214,7 +214,7 @@ class Translator { } await this._buildTermMeta(definitionsMerged, enabledDictionaryMap); - this._sortDefinitions(definitionsMerged, false); + this._sortDefinitions(definitionsMerged); for (const definition of definitionsMerged) { this._flagRedundantDefinitionTags(definition.definitions); @@ -470,7 +470,7 @@ class Translator { glossaryDefinitions.push(glossaryDefinition); } - this._sortDefinitions(glossaryDefinitions, true); + this._sortDefinitions(glossaryDefinitions); const termDetailsList = this._createTermDetailsListFromTermInfoMap(termInfoMap); @@ -580,7 +580,7 @@ class Translator { const results = []; for (const groupDefinitions of groups.values()) { - this._sortDefinitions(groupDefinitions, true); + this._sortDefinitions(groupDefinitions); const definition = this._createGroupedTermDefinition(groupDefinitions); results.push(definition); } @@ -1282,11 +1282,14 @@ class Translator { }); } - _sortDefinitions(definitions, useDictionaryPriority) { + _sortDefinitions(definitions) { if (definitions.length <= 1) { return; } const stringComparer = this._stringComparer; - const compareFunction1 = (v1, v2) => { - let i = v2.source.length - v1.source.length; + const compareFunction = (v1, v2) => { + let i = v2.dictionaryPriority - v1.dictionaryPriority; + if (i !== 0) { return i; } + + i = v2.source.length - v1.source.length; if (i !== 0) { return i; } i = v1.reasons.length - v2.reasons.length; @@ -1307,11 +1310,7 @@ class Translator { return stringComparer.compare(expression1, expression2); }; - const compareFunction2 = (v1, v2) => { - const i = v2.dictionaryPriority - v1.dictionaryPriority; - return (i !== 0) ? i : compareFunction1(v1, v2); - }; - definitions.sort(useDictionaryPriority ? compareFunction2 : compareFunction1); + definitions.sort(compareFunction); } _sortDatabaseDefinitionsByIndex(definitions) { |