2016-03-20 02:32:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class Dictionary {
|
2016-03-21 00:15:40 +00:00
|
|
|
constructor() {
|
2016-08-22 02:51:12 +00:00
|
|
|
this.entities = null;
|
2016-08-30 02:51:37 +00:00
|
|
|
this.db = new Dexie('dict');
|
2016-03-21 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 03:33:04 +00:00
|
|
|
existsDb() {
|
|
|
|
return Dexie.exists('dict');
|
2016-08-21 20:32:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 03:33:04 +00:00
|
|
|
initDb() {
|
2016-08-29 04:02:51 +00:00
|
|
|
this.entities = null;
|
2016-08-24 03:33:04 +00:00
|
|
|
this.db.version(1).stores({
|
|
|
|
terms: '++id,expression,reading',
|
2016-08-24 03:53:11 +00:00
|
|
|
entities: '++,name',
|
|
|
|
kanji: '++,character'
|
2016-08-22 02:51:12 +00:00
|
|
|
});
|
2016-03-21 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 03:33:04 +00:00
|
|
|
findTerm(term) {
|
2016-04-18 01:38:29 +00:00
|
|
|
const results = [];
|
2016-09-11 02:40:56 +00:00
|
|
|
return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
|
2016-08-22 02:51:12 +00:00
|
|
|
results.push({
|
2016-08-24 03:33:04 +00:00
|
|
|
expression: row.expression,
|
|
|
|
reading: row.reading,
|
|
|
|
tags: row.tags.split(' '),
|
|
|
|
glossary: row.glossary,
|
|
|
|
id: row.id
|
2016-08-22 02:51:12 +00:00
|
|
|
});
|
|
|
|
}).then(() => {
|
2016-08-30 02:51:37 +00:00
|
|
|
return this.getEntities();
|
2016-09-11 02:40:56 +00:00
|
|
|
}).then(entities => {
|
2016-08-30 02:51:37 +00:00
|
|
|
for (const result of results) {
|
|
|
|
result.entities = entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2016-08-22 02:51:12 +00:00
|
|
|
});
|
|
|
|
}
|
2016-04-13 06:12:20 +00:00
|
|
|
|
2016-08-22 02:51:12 +00:00
|
|
|
findKanji(kanji) {
|
|
|
|
const results = [];
|
2016-09-11 19:29:18 +00:00
|
|
|
return this.db.kanji.where('character').equals(kanji).each(row => {
|
2016-08-22 02:51:12 +00:00
|
|
|
results.push({
|
2016-09-11 19:29:18 +00:00
|
|
|
character: row.character,
|
|
|
|
onyomi: row.onyomi.split(' '),
|
|
|
|
kunyomi: row.kunyomi.split(' '),
|
|
|
|
tags: row.tags.split(' '),
|
|
|
|
glossary: row.meanings
|
2016-08-22 02:51:12 +00:00
|
|
|
});
|
2016-09-11 19:29:18 +00:00
|
|
|
}).then(() => results);
|
2016-04-01 03:03:39 +00:00
|
|
|
}
|
2016-08-24 03:33:04 +00:00
|
|
|
|
2016-08-30 02:51:37 +00:00
|
|
|
getEntities(tags) {
|
2016-08-24 05:22:09 +00:00
|
|
|
if (this.entities !== null) {
|
2016-08-30 02:51:37 +00:00
|
|
|
return this.entities;
|
2016-08-24 05:22:09 +00:00
|
|
|
}
|
2016-08-24 03:33:04 +00:00
|
|
|
|
2016-09-11 02:40:56 +00:00
|
|
|
return this.db.entities.toArray(rows => {
|
2016-08-24 05:22:09 +00:00
|
|
|
this.entities = {};
|
|
|
|
for (const row of rows) {
|
|
|
|
this.entities[row.name] = row.value;
|
|
|
|
}
|
2016-08-30 02:51:37 +00:00
|
|
|
|
|
|
|
return this.entities;
|
2016-08-24 05:22:09 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-24 03:33:04 +00:00
|
|
|
|
|
|
|
importTermDict(indexUrl) {
|
2016-08-24 05:22:09 +00:00
|
|
|
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
|
|
|
|
|
2016-09-11 02:40:56 +00:00
|
|
|
return loadJson(indexUrl).then(index => {
|
2016-08-24 03:33:04 +00:00
|
|
|
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 = [];
|
2016-08-24 16:14:23 +00:00
|
|
|
for (let i = 1; i <= index.banks; ++i) {
|
|
|
|
const bankUrl = `${indexDir}/bank_${i}.json`;
|
2016-08-24 03:53:11 +00:00
|
|
|
loaders.push(() => {
|
2016-09-11 02:40:56 +00:00
|
|
|
return loadJson(bankUrl).then(definitions => {
|
2016-08-24 03:33:04 +00:00
|
|
|
const rows = [];
|
2016-09-11 02:40:56 +00:00
|
|
|
for (const [expression, reading, tags, ...glossary] of definitions) {
|
2016-08-24 03:53:11 +00:00
|
|
|
rows.push({expression, reading, tags, glossary});
|
2016-08-24 03:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.db.terms.bulkAdd(rows);
|
2016-08-24 03:53:11 +00:00
|
|
|
});
|
|
|
|
});
|
2016-08-24 03:33:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 03:53:11 +00:00
|
|
|
let chain = Promise.resolve();
|
|
|
|
for (const loader of loaders) {
|
|
|
|
chain = chain.then(loader);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain;
|
2016-08-24 03:33:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:22:09 +00:00
|
|
|
importKanjiDict(indexUrl) {
|
|
|
|
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
|
|
|
|
|
2016-09-11 02:40:56 +00:00
|
|
|
return loadJson(indexUrl).then(index => {
|
2016-08-24 05:22:09 +00:00
|
|
|
const loaders = [];
|
2016-08-24 16:14:23 +00:00
|
|
|
for (let i = 1; i <= index.banks; ++i) {
|
|
|
|
const bankUrl = `${indexDir}/bank_${i}.json`;
|
2016-08-24 05:22:09 +00:00
|
|
|
loaders.push(() => {
|
2016-09-11 02:40:56 +00:00
|
|
|
return loadJson(bankUrl).then(definitions => {
|
2016-08-24 05:22:09 +00:00
|
|
|
const rows = [];
|
2016-09-11 02:40:56 +00:00
|
|
|
for (const [character, onyomi, kunyomi, tags, ...meanings] of definitions) {
|
2016-08-24 16:14:23 +00:00
|
|
|
rows.push({character, onyomi, kunyomi, tags, meanings});
|
2016-08-24 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.db.kanji.bulkAdd(rows);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let chain = Promise.resolve();
|
|
|
|
for (const loader of loaders) {
|
|
|
|
chain = chain.then(loader);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain;
|
|
|
|
});
|
|
|
|
}
|
2016-03-20 02:32:35 +00:00
|
|
|
}
|