Work on DB integration
This commit is contained in:
parent
67f906ab24
commit
a062b25178
@ -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) {
|
||||||
|
this.entities[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
findTerm(term) {
|
return this.db.entities.bulkAdd(dict.e, 'name, 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);
|
importKanjiDict(dict) {
|
||||||
results = results.concat(
|
return this.db.kanji.bulkAdd(dict.d, 'character, onyomi, kunyomi, tags, glossary');
|
||||||
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 results;
|
fetchEntities() {
|
||||||
|
if (this.entities !== null) {
|
||||||
|
return Promise.resolve(this.entities);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
|
||||||
const def = this.kanjiDicts[name].c[kanji];
|
|
||||||
if (def) {
|
|
||||||
const [k, o, t, ...g] = def;
|
|
||||||
results.push({
|
results.push({
|
||||||
character: kanji,
|
character: row.character,
|
||||||
kunyomi: k.split(' '),
|
onyomi: row.onyomi.split(' '),
|
||||||
onyomi: o.split(' '),
|
kunyomi: row.kunyomi.split(' '),
|
||||||
tags: t.split(' '),
|
tags: row.tags.split(' '),
|
||||||
glossary: g
|
glossary: row.glossary
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user