diff options
author | StefanVukovic99 <stefanvukovic44@gmail.com> | 2024-04-15 19:47:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-15 17:47:14 +0000 |
commit | afd6e172669cdd148f53c1776ec449f4b1926ace (patch) | |
tree | 41dfc9138540e15fb352f264dbf980be42f01f58 /ext/js/language | |
parent | f5a6b25e8d3331fce1cc5145bb7b21f2081743fe (diff) |
improve term sorting (#806)24.4.15.0
* improve term sorting
* edge case
Diffstat (limited to 'ext/js/language')
-rw-r--r-- | ext/js/language/translator.js | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index af275c07..6132ee82 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -1547,12 +1547,12 @@ export class Translator { * @param {number} dictionaryIndex * @param {number} dictionaryPriority * @param {number} sourceTermExactMatchCount - * @param {number} maxTransformedTextLength + * @param {number} maxOriginalTextLength * @param {import('dictionary').TermHeadword[]} headwords * @param {import('dictionary').TermDefinition[]} definitions * @returns {import('dictionary').TermDictionaryEntry} */ - _createTermDictionaryEntry(isPrimary, inflectionRuleChainCandidates, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, maxTransformedTextLength, headwords, definitions) { + _createTermDictionaryEntry(isPrimary, inflectionRuleChainCandidates, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, maxOriginalTextLength, headwords, definitions) { return { type: 'term', isPrimary, @@ -1562,7 +1562,7 @@ export class Translator { dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, - maxTransformedTextLength, + maxOriginalTextLength, headwords, definitions, pronunciations: [], @@ -1602,7 +1602,7 @@ export class Translator { const {index: dictionaryIndex, priority: dictionaryPriority} = this._getDictionaryOrder(dictionary, enabledDictionaryMap); const sourceTermExactMatchCount = (isPrimary && deinflectedText === term ? 1 : 0); const source = this._createSource(originalText, transformedText, deinflectedText, matchType, matchSource, isPrimary); - const maxTransformedTextLength = transformedText.length; + const maxOriginalTextLength = originalText.length; const hasSequence = (rawSequence >= 0); const sequence = hasSequence ? rawSequence : -1; @@ -1620,7 +1620,7 @@ export class Translator { dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, - maxTransformedTextLength, + maxOriginalTextLength, [this._createTermHeadword(0, term, reading, [source], headwordTagGroups, rules)], [this._createTermDefinition(0, [0], dictionary, dictionaryIndex, dictionaryPriority, id, score, [sequence], isPrimary, definitionTagGroups, contentDefinitions)] ); @@ -1651,7 +1651,7 @@ export class Translator { let score = Number.MIN_SAFE_INTEGER; let dictionaryIndex = Number.MAX_SAFE_INTEGER; let dictionaryPriority = Number.MIN_SAFE_INTEGER; - let maxTransformedTextLength = 0; + let maxOriginalTextLength = 0; let isPrimary = false; /** @type {import('dictionary').TermDefinition[]} */ const definitions = []; @@ -1665,7 +1665,7 @@ export class Translator { dictionaryPriority = Math.max(dictionaryPriority, dictionaryEntry.dictionaryPriority); if (dictionaryEntry.isPrimary) { isPrimary = true; - maxTransformedTextLength = Math.max(maxTransformedTextLength, dictionaryEntry.maxTransformedTextLength); + maxOriginalTextLength = Math.max(maxOriginalTextLength, dictionaryEntry.maxOriginalTextLength); const dictionaryEntryInflections = dictionaryEntry.inflectionRuleChainCandidates; if (inflections === null || dictionaryEntryInflections.length < inflections.length) { inflections = dictionaryEntryInflections; @@ -1697,7 +1697,7 @@ export class Translator { dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, - maxTransformedTextLength, + maxOriginalTextLength, headwordsArray, definitions ); @@ -1874,11 +1874,11 @@ export class Translator { */ const compareFunction = (v1, v2) => { // Sort by length of source term - let i = v2.maxTransformedTextLength - v1.maxTransformedTextLength; + let i = v2.maxOriginalTextLength - v1.maxOriginalTextLength; if (i !== 0) { return i; } // Sort by the number of inflection reasons - i = v1.inflectionRuleChainCandidates.length - v2.inflectionRuleChainCandidates.length; + i = this._getShortestInflectionChainLength(v1.inflectionRuleChainCandidates) - this._getShortestInflectionChainLength(v2.inflectionRuleChainCandidates); if (i !== 0) { return i; } // Sort by how many terms exactly match the source (e.g. for exact kana prioritization) @@ -2075,6 +2075,19 @@ export class Translator { } } + /** + * @param {import('dictionary').InflectionRuleChainCandidate[]} inflectionRuleChainCandidates + * @returns {number} + */ + _getShortestInflectionChainLength(inflectionRuleChainCandidates) { + if (inflectionRuleChainCandidates.length === 0) { return 0; } + let length = Number.MAX_SAFE_INTEGER; + for (const {inflectionRules} of inflectionRuleChainCandidates) { + length = Math.min(length, inflectionRules.length); + } + return length; + } + // Miscellaneous /** |