aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-10-19 10:21:10 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-10-19 10:39:08 -0400
commita25167df7338187a443f5e1281fd068fe394905e (patch)
tree134a55dfe37c64a80561362cb2a1f281bea1b600 /ext/bg/js
parentd212d0aba132efb17d5dd2f354dfe596d6f8d018 (diff)
Use findKanjiBulk and findKanjiMetaBulk in Translator.findKanji
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/translator.js41
1 files changed, 26 insertions, 15 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 601ee30c..61acaa79 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -277,33 +277,44 @@ class Translator {
}
async findKanji(text, dictionaries) {
- let definitions = [];
- const processed = {};
const titles = Object.keys(dictionaries);
+ const kanjiUnique = {};
+ const kanjiList = [];
for (const c of text) {
- if (!processed[c]) {
- definitions.push(...await this.database.findKanji(c, titles));
- processed[c] = true;
+ if (!kanjiUnique.hasOwnProperty(c)) {
+ kanjiList.push(c);
+ 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) {
+ kanjiList2.push(definition.character);
+
const tags = await this.expandTags(definition.tags, definition.dictionary);
tags.push(dictTagBuildSource(definition.dictionary));
definition.tags = dictTagsSort(tags);
definition.stats = await this.expandStats(definition.stats, definition.dictionary);
-
definition.frequencies = [];
- for (const meta of await this.database.findKanjiMeta(definition.character, titles)) {
- if (meta.mode === 'freq') {
- definition.frequencies.push({
- character: meta.character,
- frequency: meta.data,
- dictionary: meta.dictionary
- });
- }
- }
+ }
+
+ for (const meta of await this.database.findKanjiMetaBulk(kanjiList2, titles)) {
+ if (meta.mode !== 'freq') { continue; }
+ definitions[meta.index].frequencies.push({
+ character: meta.character,
+ frequency: meta.data,
+ dictionary: meta.dictionary
+ });
}
return definitions;