Work on DB integration

This commit is contained in:
Alex Yatskov 2016-08-21 19:51:12 -07:00
parent 67f906ab24
commit a062b25178
2 changed files with 59 additions and 49 deletions

View File

@ -19,10 +19,9 @@
class Dictionary { class Dictionary {
constructor() { constructor() {
this.termDicts = {};
this.kanjiDicts = {};
this.db = new Dexie('dict'); this.db = new Dexie('dict');
this.dbVer = 1; this.dbVer = 1;
this.entities = null;
} }
loadDb() { loadDb() {
@ -35,59 +34,69 @@ class Dictionary {
}); });
} }
importTermDict(name, dict) { initDb() {
this.termDicts[name] = dict; this.entities = {};
return this.db.version(this.dbVer).stores({
terms: 'expression, reading',
entities: 'name',
kanji: 'character',
});
} }
importKanjiDict(name, dict) { importTermDict(dict) {
this.kanjiDicts[name] = dict; this.entities = {};
} return this.db.terms.bulkAdd(dict.d, 'expression, reading, tags, glossary').then(() => {
for (const [key, value] of dict.e) {
findTerm(term) { this.entities[key] = value;
let results = [];
for (const name in this.termDicts) {
const dict = this.termDicts[name];
if (!(term in dict.i)) {
continue;
} }
const indices = dict.i[term].split(' ').map(Number); return this.db.entities.bulkAdd(dict.e, 'name, value');
results = results.concat( });
indices.map(index => { }
const [e, r, t, ...g] = dict.d[index];
return { importKanjiDict(dict) {
expression: e, return this.db.kanji.bulkAdd(dict.d, 'character, onyomi, kunyomi, tags, glossary');
reading: r, }
tags: t.split(' '),
glossary: g, fetchEntities() {
entities: dict.e, if (this.entities !== null) {
id: index 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) { findKanji(kanji) {
const results = []; const results = [];
return this.db.kanji.where('character').equals(kanji).each((row) => {
for (const name in this.kanjiDicts) { results.push({
const def = this.kanjiDicts[name].c[kanji]; character: row.character,
if (def) { onyomi: row.onyomi.split(' '),
const [k, o, t, ...g] = def; kunyomi: row.kunyomi.split(' '),
results.push({ tags: row.tags.split(' '),
character: kanji, glossary: row.glossary
kunyomi: k.split(' '), });
onyomi: o.split(' '), });
tags: t.split(' '),
glossary: g
});
}
}
return results;
} }
} }

View File

@ -41,15 +41,16 @@ class Translator {
this.loaded = true; this.loaded = true;
callback(); callback();
}).catch(() => { }).catch(() => {
this.dictionary.initDb();
return Translator.loadData('bg/data/edict.json'); return Translator.loadData('bg/data/edict.json');
}).then((response) => { }).then((response) => {
this.dictionary.importTermDict('edict', JSON.parse(response)); this.dictionary.importTermDict(JSON.parse(response));
return Translator.loadData('bg/data/enamdict.json'); return Translator.loadData('bg/data/enamdict.json');
}).then((response) => { }).then((response) => {
this.dictionary.importTermDict('enamdict', JSON.parse(response)); this.dictionary.importTermDict(JSON.parse(response));
return Translator.loadData('bg/data/kanjidic.json'); return Translator.loadData('bg/data/kanjidic.json');
}).then((response) => { }).then((response) => {
this.dictionary.importKanjiDict('kanjidic', JSON.parse(response)); this.dictionary.importKanjiDict(JSON.parse(response));
this.loaded = true; this.loaded = true;
callback(); callback();
}); });