This commit is contained in:
Alex Yatskov 2016-08-23 20:33:04 -07:00
parent 9621a0cd4b
commit f106b64876
2 changed files with 97 additions and 51 deletions

View File

@ -23,40 +23,26 @@ class Dictionary {
this.entities = null; this.entities = null;
} }
existsDb() {
return Dexie.exists('dict');
}
loadDb() { loadDb() {
this.db = null; this.db = null;
this.entities = null; this.entities = null;
return new Dexie('dict').open().then((db) => { return this.initDb().open();
this.db = db;
});
} }
resetDb() { initDb() {
this.db = null; this.db = new Dexie('dict');
this.entities = null; this.db.version(1).stores({
terms: '++id,expression,reading',
return new Dexie('dict').delete().then(() => { entities: '++id,name',
return Promise.resolve(new Dexie('dict')); kanji: '++id,character'
}).then((db) => {
this.db = db;
return this.db.version(1).stores({
terms: '++id, e, r',
entities: 'n',
kanji: 'c'
});
}); });
}
importTermDict(dict) { return this.db;
return this.db.terms.bulkAdd(dict.d).then(() => {
this.entities = {};
for (const name in dict.e) {
this.entities[name] = dict.e[name];
}
return this.db.entities.bulkAdd(dict.e);
});
} }
importKanjiDict(dict) { importKanjiDict(dict) {
@ -78,19 +64,19 @@ class Dictionary {
}); });
} }
findterm(term) { findTerm(term) {
const results = []; const results = [];
return this.db.terms.where('e').equals(term).or('r').equals(term).each((row) => { return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => {
results.push({ results.push({
expression: row.e, expression: row.expression,
reading: row.r, reading: row.reading,
tags: row.t.split(' '), tags: row.tags.split(' '),
glossary: row.g, glossary: row.glossary,
entities: this.entities, entities: this.entities,
id: results.id id: row.id
}); });
}).then(() => { }).then(() => {
Promise.resolve(results); return Promise.resolve(results);
}); });
} }
@ -106,4 +92,67 @@ class Dictionary {
}); });
}); });
} }
// importTermDict(dict) {
// return this.db.terms.bulkAdd(dict.d).then(() => {
// this.entities = {};
// for (const name in dict.e) {
// this.entities[name] = dict.e[name];
// }
// return this.db.entities.bulkAdd(dict.e);
// });
// }
importTermDict(indexUrl) {
return Dictionary.loadJson(indexUrl).then((index) => {
const entities = [];
for (const [name, value] of index.ents) {
entities.push({name, value});
}
return this.db.entities.bulkAdd(entities).then(() => {
if (this.entities === null) {
this.entities = {};
}
for (const entity of entities) {
this.entities[entity.name] = entity.value;
}
}).then(() => {
const loaders = [];
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
for (let i = 0; i < index.refs; ++i) {
const refUrl = `${indexDir}/ref_${i}.json`;
loaders.push(
Dictionary.loadJson(refUrl).then((refs) => {
const rows = [];
for (const [e, r, t, ...g] of refs) {
rows.push({
'expression': e,
'reading': r,
'tags': t,
'glossary': g
});
}
return this.db.terms.bulkAdd(rows);
})
);
}
return Promise.all(loaders);
});
});
}
static loadJson(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
xhr.open('GET', chrome.extension.getURL(url), true);
xhr.send();
});
}
} }

View File

@ -36,27 +36,24 @@ class Translator {
return Translator.loadData('bg/data/tags.json'); return Translator.loadData('bg/data/tags.json');
}).then((response) => { }).then((response) => {
this.tagMeta = JSON.parse(response); this.tagMeta = JSON.parse(response);
return this.dictionary.loadDb(); return this.dictionary.existsDb();
}).then((exists) => {
if (exists) {
return this.dictionary.loadDb();
} else {
this.dictionary.initDb();
return Promise.all([
this.dictionary.importTermDict('bg/data/edict/index.json'),
this.dictionary.importTermDict('bg/data/enamdict/index.json')
]);
}
}).then(() => { }).then(() => {
this.loaded = true; this.loaded = true;
callback(); callback();
}).catch(() => {
return this.dictionary.resetDb().then(() => { this.dictionary.findTerm('猫').then((result) => {
return Translator.loadData('bg/data/edict.json'); console.log(result);
}).then((response) => {
return this.dictionary.importTermDict(JSON.parse(response));
}).then(() => {
return Translator.loadData('bg/data/enamdict.json');
}).then((response) => {
return this.dictionary.importTermDict(JSON.parse(response));
}).then(() => {
return Translator.loadData('bg/data/kanjidic.json');
}).then((response) => {
return this.dictionary.importKanjiDict(JSON.parse(response));
}); });
}).then(() => {
this.loaded = true;
callback();
}); });
} }