support for dictionary priority sorting

This commit is contained in:
Alex Yatskov 2017-01-16 20:54:01 -08:00
parent e8840465f0
commit 268e00435f
3 changed files with 37 additions and 18 deletions

View File

@ -42,11 +42,13 @@ class Translator {
} }
findTerms(text, dictionaries, softKatakana) { findTerms(text, dictionaries, softKatakana) {
const titles = Object.keys(dictionaries);
const cache = {}; const cache = {};
return this.findTermsDeinflected(text, dictionaries, cache).then(deinfLiteral => {
return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => {
const textHiragana = wanakana._katakanaToHiragana(text); const textHiragana = wanakana._katakanaToHiragana(text);
if (text !== textHiragana && softKatakana) { 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 { } else {
return deinfLiteral; return deinfLiteral;
} }
@ -71,7 +73,7 @@ class Translator {
} }
definitions = undupeTermDefs(definitions); definitions = undupeTermDefs(definitions);
definitions = sortTermDefs(definitions); definitions = sortTermDefs(definitions, dictionaries);
let length = 0; let length = 0;
for (const definition of definitions) { for (const definition of definitions) {
@ -84,15 +86,18 @@ class Translator {
findTermsGrouped(text, dictionaries, softKatakana) { findTermsGrouped(text, dictionaries, softKatakana) {
return this.findTerms(text, dictionaries, softKatakana).then(({length, definitions}) => { return this.findTerms(text, dictionaries, softKatakana).then(({length, definitions}) => {
return {length, definitions: groupTermDefs(definitions)}; return {length, definitions: groupTermDefs(definitions, dictionaries)};
}); });
} }
findKanji(text, dictionaries) { findKanji(text, dictionaries) {
const processed = {}, promises = []; const titles = Object.keys(dictionaries);
const processed = {};
const promises = [];
for (const c of text) { for (const c of text) {
if (!processed[c]) { if (!processed[c]) {
promises.push(this.database.findKanji(c, dictionaries)); promises.push(this.database.findKanji(c, titles));
processed[c] = true; processed[c] = true;
} }
} }

View File

@ -50,7 +50,7 @@ function promiseCallback(promise, callback) {
}); });
} }
function sortTermDefs(definitions) { function sortTermDefs(definitions, dictionaries=null) {
return definitions.sort((v1, v2) => { return definitions.sort((v1, v2) => {
const sl1 = v1.source.length; const sl1 = v1.source.length;
const sl2 = v2.source.length; const sl2 = v2.source.length;
@ -60,6 +60,16 @@ function sortTermDefs(definitions) {
return 1; 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 s1 = v1.score;
const s2 = v2.score; const s2 = v2.score;
if (s1 > s2) { if (s1 > s2) {
@ -97,7 +107,7 @@ function undupeTermDefs(definitions) {
return definitionsUnique; return definitionsUnique;
} }
function groupTermDefs(definitions) { function groupTermDefs(definitions, dictionaries) {
const groups = {}; const groups = {};
for (const definition of definitions) { for (const definition of definitions) {
const key = [definition.source, definition.expression].concat(definition.reasons); const key = [definition.source, definition.expression].concat(definition.reasons);
@ -117,12 +127,13 @@ function groupTermDefs(definitions) {
for (const key in groups) { for (const key in groups) {
const groupDefs = groups[key]; const groupDefs = groups[key];
const firstDef = groupDefs[0]; const firstDef = groupDefs[0];
sortTermDefs(groupDefs, dictionaries);
results.push({ results.push({
definitions: groupDefs, definitions: groupDefs,
expression: firstDef.expression, expression: firstDef.expression,
reading: firstDef.reading, reading: firstDef.reading,
reasons: firstDef.reasons, 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 source: firstDef.source
}); });
} }

View File

@ -157,10 +157,11 @@ class Yomichan {
} }
api_findKanji({text, callback}) { api_findKanji({text, callback}) {
const dictionaries = []; const dictionaries = {};
for (const title in this.options.dictionaries) { for (const title in this.options.dictionaries) {
if (this.options.dictionaries[title].enableKanji) { const dictionary = this.options.dictionaries[title];
dictionaries.push(title); if (dictionary.enableKanji) {
dictionaries[title] = dictionary;
} }
} }
@ -171,10 +172,11 @@ class Yomichan {
} }
api_findTerms({text, callback}) { api_findTerms({text, callback}) {
const dictionaries = []; const dictionaries = {};
for (const title in this.options.dictionaries) { for (const title in this.options.dictionaries) {
if (this.options.dictionaries[title].enableTerms) { const dictionary = this.options.dictionaries[title];
dictionaries.push(title); if (dictionary.enableTerms) {
dictionaries[title] = dictionary;
} }
} }
@ -189,10 +191,11 @@ class Yomichan {
} }
api_findTermsGrouped({text, callback}) { api_findTermsGrouped({text, callback}) {
const dictionaries = []; const dictionaries = {};
for (const title in this.options.dictionaries) { for (const title in this.options.dictionaries) {
if (this.options.dictionaries[title].enableTerms) { const dictionary = this.options.dictionaries[title];
dictionaries.push(title); if (dictionary.enableTerms) {
dictionaries[title] = dictionary;
} }
} }