From a062b25178dd8d916a8cdf87cffa36b5365ab021 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 21 Aug 2016 19:51:12 -0700 Subject: [PATCH] Work on DB integration --- ext/bg/js/dictionary.js | 101 ++++++++++++++++++++++------------------ ext/bg/js/translator.js | 7 +-- 2 files changed, 59 insertions(+), 49 deletions(-) diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 082d1479..7b885db2 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -19,10 +19,9 @@ class Dictionary { constructor() { - this.termDicts = {}; - this.kanjiDicts = {}; this.db = new Dexie('dict'); this.dbVer = 1; + this.entities = null; } loadDb() { @@ -35,59 +34,69 @@ class Dictionary { }); } - importTermDict(name, dict) { - this.termDicts[name] = dict; + initDb() { + this.entities = {}; + return this.db.version(this.dbVer).stores({ + terms: 'expression, reading', + entities: 'name', + kanji: 'character', + }); } - importKanjiDict(name, dict) { - this.kanjiDicts[name] = dict; - } - - findTerm(term) { - let results = []; - - for (const name in this.termDicts) { - const dict = this.termDicts[name]; - if (!(term in dict.i)) { - continue; + importTermDict(dict) { + this.entities = {}; + return this.db.terms.bulkAdd(dict.d, 'expression, reading, tags, glossary').then(() => { + for (const [key, value] of dict.e) { + this.entities[key] = value; } - const indices = dict.i[term].split(' ').map(Number); - results = results.concat( - indices.map(index => { - const [e, r, t, ...g] = dict.d[index]; - return { - expression: e, - reading: r, - tags: t.split(' '), - glossary: g, - entities: dict.e, - id: index - }; - }) - ); + return this.db.entities.bulkAdd(dict.e, 'name, value'); + }); + } + + importKanjiDict(dict) { + return this.db.kanji.bulkAdd(dict.d, 'character, onyomi, kunyomi, tags, glossary'); + } + + fetchEntities() { + if (this.entities !== null) { + return Promise.resolve(this.entities); } - return results; + this.entities = {}; + return this.db.entities.each((row) => { + this.entities[row.name] = row.value; + }).then(() => { + return Promise.resolve(this.entities); + }); + } + + findterm(term) { + const results = []; + return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => { + results.push({ + expression: row.expression, + reading: row.reading, + tags: row.tags.split(' '), + glossary: row.glossary, + entities: this.entities, + id: results.length + }); + }).then(() => { + Promise.resolve(results); + }); } findKanji(kanji) { const results = []; - - for (const name in this.kanjiDicts) { - const def = this.kanjiDicts[name].c[kanji]; - if (def) { - const [k, o, t, ...g] = def; - results.push({ - character: kanji, - kunyomi: k.split(' '), - onyomi: o.split(' '), - tags: t.split(' '), - glossary: g - }); - } - } - - return results; + return this.db.kanji.where('character').equals(kanji).each((row) => { + results.push({ + character: row.character, + onyomi: row.onyomi.split(' '), + kunyomi: row.kunyomi.split(' '), + tags: row.tags.split(' '), + glossary: row.glossary + }); + }); } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f373fe83..9fd1ab59 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -41,15 +41,16 @@ class Translator { this.loaded = true; callback(); }).catch(() => { + this.dictionary.initDb(); return Translator.loadData('bg/data/edict.json'); }).then((response) => { - this.dictionary.importTermDict('edict', JSON.parse(response)); + this.dictionary.importTermDict(JSON.parse(response)); return Translator.loadData('bg/data/enamdict.json'); }).then((response) => { - this.dictionary.importTermDict('enamdict', JSON.parse(response)); + this.dictionary.importTermDict(JSON.parse(response)); return Translator.loadData('bg/data/kanjidic.json'); }).then((response) => { - this.dictionary.importKanjiDict('kanjidic', JSON.parse(response)); + this.dictionary.importKanjiDict(JSON.parse(response)); this.loaded = true; callback(); });