Use findKanjiBulk and findKanjiMetaBulk in Translator.findKanji

This commit is contained in:
toasted-nutbread 2019-10-19 10:21:10 -04:00
parent d212d0aba1
commit a25167df73

View File

@ -277,34 +277,45 @@ class Translator {
} }
async findKanji(text, dictionaries) { async findKanji(text, dictionaries) {
let definitions = [];
const processed = {};
const titles = Object.keys(dictionaries); const titles = Object.keys(dictionaries);
const kanjiUnique = {};
const kanjiList = [];
for (const c of text) { for (const c of text) {
if (!processed[c]) { if (!kanjiUnique.hasOwnProperty(c)) {
definitions.push(...await this.database.findKanji(c, titles)); kanjiList.push(c);
processed[c] = true; kanjiUnique[c] = true;
} }
} }
const definitions = await this.database.findKanjiBulk(kanjiList, titles);
if (definitions.length === 0) {
return definitions;
}
if (definitions.length > 1) {
definitions.sort((a, b) => a.index - b.index);
}
const kanjiList2 = [];
for (const definition of definitions) { for (const definition of definitions) {
kanjiList2.push(definition.character);
const tags = await this.expandTags(definition.tags, definition.dictionary); const tags = await this.expandTags(definition.tags, definition.dictionary);
tags.push(dictTagBuildSource(definition.dictionary)); tags.push(dictTagBuildSource(definition.dictionary));
definition.tags = dictTagsSort(tags); definition.tags = dictTagsSort(tags);
definition.stats = await this.expandStats(definition.stats, definition.dictionary); definition.stats = await this.expandStats(definition.stats, definition.dictionary);
definition.frequencies = []; definition.frequencies = [];
for (const meta of await this.database.findKanjiMeta(definition.character, titles)) { }
if (meta.mode === 'freq') {
definition.frequencies.push({ for (const meta of await this.database.findKanjiMetaBulk(kanjiList2, titles)) {
if (meta.mode !== 'freq') { continue; }
definitions[meta.index].frequencies.push({
character: meta.character, character: meta.character,
frequency: meta.data, frequency: meta.data,
dictionary: meta.dictionary dictionary: meta.dictionary
}); });
} }
}
}
return definitions; return definitions;
} }