diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-10-04 18:54:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-04 18:54:03 -0400 |
commit | f904b3e11a6624918da23fd0b15bf8c1c7248478 (patch) | |
tree | 1e73f14b943e9ae16a1fcf9cf90da4e5b0d18e3a /ext/bg/js/translator.js | |
parent | 561e36e88dab984d6f071ea888cc2b92039a86f0 (diff) |
Add sourceTermExactMatchCount to prioritize exact expression matches (#882)
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r-- | ext/bg/js/translator.js | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 4b66dd9c..c5077977 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -295,6 +295,12 @@ class Translator { return result; } + _getSourceTermMatchCountSum(definitions) { + let result = 0; + for (const {sourceTermExactMatchCount} of definitions) { result += sourceTermExactMatchCount; } + return result; + } + async _findTermsGrouped(text, options) { const {compactTags, enabledDictionaryMap} = options; const [definitions, length] = await this._findTermsInternal(text, enabledDictionaryMap, options); @@ -979,6 +985,7 @@ class Translator { const furiganaSegments = jp.distributeFurigana(expression, reading); const termDetailsList = [this._createTermDetails(sourceTerm, expression, reading, furiganaSegments, termTags)]; + const sourceTermExactMatchCount = (sourceTerm === expression ? 1 : 0); return { type: 'term', @@ -1000,8 +1007,9 @@ class Translator { termTags: termTagsExpanded, // definitions frequencies: [], - pitches: [] + pitches: [], // only + sourceTermExactMatchCount }; } @@ -1010,6 +1018,7 @@ class Translator { const score = this._getMaxDefinitionScore(definitions); const dictionaryPriority = this._getMaxDictionaryPriority(definitions); const termDetailsList = [this._createTermDetails(sourceTerm, expression, reading, furiganaSegments, termTags)]; + const sourceTermExactMatchCount = (sourceTerm === expression ? 1 : 0); return { type: 'termGrouped', // id @@ -1030,13 +1039,15 @@ class Translator { termTags: this._cloneTags(termTags), definitions, // type: 'term' frequencies: [], - pitches: [] + pitches: [], // only + sourceTermExactMatchCount }; } _createMergedTermDefinition(source, rawSource, definitions, expressions, readings, termDetailsList, reasons, dictionary, score) { const dictionaryPriority = this._getMaxDictionaryPriority(definitions); + const sourceTermExactMatchCount = this._getSourceTermMatchCountSum(definitions); return { type: 'termMerged', // id @@ -1057,8 +1068,9 @@ class Translator { // termTags definitions, // type: 'termMergedByGlossary' frequencies: [], - pitches: [] + pitches: [], // only + sourceTermExactMatchCount }; } @@ -1071,6 +1083,8 @@ class Translator { only.push(...getSetIntersection(readings, allReadings)); } + const sourceTermExactMatchCount = this._getSourceTermMatchCountSum(definitions); + const termInfoMap = new Map(); this._addUniqueTermInfos(definitions, termInfoMap); const termDetailsList = this._createTermDetailsListFromTermInfoMap(termInfoMap); @@ -1102,7 +1116,8 @@ class Translator { definitions, // type: 'term'; contains duplicate data frequencies: [], pitches: [], - only + only, + sourceTermExactMatchCount }; } @@ -1153,6 +1168,9 @@ class Translator { i = v1.reasons.length - v2.reasons.length; if (i !== 0) { return i; } + i = v2.sourceTermExactMatchCount - v1.sourceTermExactMatchCount; + if (i !== 0) { return i; } + i = v2.score - v1.score; if (i !== 0) { return i; } |