From 268e00435f081b81ac0426bd3e316acb1bb33915 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 16 Jan 2017 20:54:01 -0800 Subject: support for dictionary priority sorting --- ext/bg/js/translator.js | 17 +++++++++++------ ext/bg/js/util.js | 17 ++++++++++++++--- ext/bg/js/yomichan.js | 21 ++++++++++++--------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 4edf35ac..e7c2aac2 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -42,11 +42,13 @@ class Translator { } findTerms(text, dictionaries, softKatakana) { + const titles = Object.keys(dictionaries); const cache = {}; - return this.findTermsDeinflected(text, dictionaries, cache).then(deinfLiteral => { + + return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => { const textHiragana = wanakana._katakanaToHiragana(text); if (text !== textHiragana && softKatakana) { - return this.findTermsDeinflected(textHiragana, dictionaries, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana)); + return this.findTermsDeinflected(textHiragana, titles, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana)); } else { return deinfLiteral; } @@ -71,7 +73,7 @@ class Translator { } definitions = undupeTermDefs(definitions); - definitions = sortTermDefs(definitions); + definitions = sortTermDefs(definitions, dictionaries); let length = 0; for (const definition of definitions) { @@ -84,15 +86,18 @@ class Translator { findTermsGrouped(text, dictionaries, softKatakana) { return this.findTerms(text, dictionaries, softKatakana).then(({length, definitions}) => { - return {length, definitions: groupTermDefs(definitions)}; + return {length, definitions: groupTermDefs(definitions, dictionaries)}; }); } findKanji(text, dictionaries) { - const processed = {}, promises = []; + const titles = Object.keys(dictionaries); + const processed = {}; + const promises = []; + for (const c of text) { if (!processed[c]) { - promises.push(this.database.findKanji(c, dictionaries)); + promises.push(this.database.findKanji(c, titles)); processed[c] = true; } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 900a7a2c..bba4d797 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -50,7 +50,7 @@ function promiseCallback(promise, callback) { }); } -function sortTermDefs(definitions) { +function sortTermDefs(definitions, dictionaries=null) { return definitions.sort((v1, v2) => { const sl1 = v1.source.length; const sl2 = v2.source.length; @@ -60,6 +60,16 @@ function sortTermDefs(definitions) { return 1; } + if (dictionaries !== null) { + const p1 = (dictionaries[v1.dictionary] || {}).priority || 0; + const p2 = (dictionaries[v2.dictionary] || {}).priority || 0; + if (p1 > p2) { + return -1; + } else if (p1 < p2) { + return 1; + } + } + const s1 = v1.score; const s2 = v2.score; if (s1 > s2) { @@ -97,7 +107,7 @@ function undupeTermDefs(definitions) { return definitionsUnique; } -function groupTermDefs(definitions) { +function groupTermDefs(definitions, dictionaries) { const groups = {}; for (const definition of definitions) { const key = [definition.source, definition.expression].concat(definition.reasons); @@ -117,12 +127,13 @@ function groupTermDefs(definitions) { for (const key in groups) { const groupDefs = groups[key]; const firstDef = groupDefs[0]; + sortTermDefs(groupDefs, dictionaries); results.push({ definitions: groupDefs, expression: firstDef.expression, reading: firstDef.reading, reasons: firstDef.reasons, - score: groupDefs.reduce((x, y) => x > y ? x : y, Number.MIN_SAFE_INTEGER), + score: groupDefs.reduce((x, y) => x.score > y.score ? x.score : y.score, Number.MIN_SAFE_INTEGER), source: firstDef.source }); } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 27077f57..494b9cda 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -157,10 +157,11 @@ class Yomichan { } api_findKanji({text, callback}) { - const dictionaries = []; + const dictionaries = {}; for (const title in this.options.dictionaries) { - if (this.options.dictionaries[title].enableKanji) { - dictionaries.push(title); + const dictionary = this.options.dictionaries[title]; + if (dictionary.enableKanji) { + dictionaries[title] = dictionary; } } @@ -171,10 +172,11 @@ class Yomichan { } api_findTerms({text, callback}) { - const dictionaries = []; + const dictionaries = {}; for (const title in this.options.dictionaries) { - if (this.options.dictionaries[title].enableTerms) { - dictionaries.push(title); + const dictionary = this.options.dictionaries[title]; + if (dictionary.enableTerms) { + dictionaries[title] = dictionary; } } @@ -189,10 +191,11 @@ class Yomichan { } api_findTermsGrouped({text, callback}) { - const dictionaries = []; + const dictionaries = {}; for (const title in this.options.dictionaries) { - if (this.options.dictionaries[title].enableTerms) { - dictionaries.push(title); + const dictionary = this.options.dictionaries[title]; + if (dictionary.enableTerms) { + dictionaries[title] = dictionary; } } -- cgit v1.2.3