This commit is contained in:
Alex Yatskov 2016-11-05 16:44:29 -07:00
parent 909218c82b
commit fd2820bc1a
4 changed files with 5983 additions and 63 deletions

5895
ext/bg/data/rules.json Normal file

File diff suppressed because it is too large Load Diff

33
ext/bg/data/tags.json Normal file
View File

@ -0,0 +1,33 @@
{
"P": {"class": "popular", "desc": "popular term", "order": 1, "score": 5},
"exp": {"class": "expression", "order": 2},
"id": {"class": "expression", "order": 2},
"arch": {"class": "archaism", "order": 2, "score": -1},
"iK": {"class": "archaism", "order": 2, "score": -1},
"news": {"class": "frequent", "desc": "appears frequently in Mainichi Shimbun", "order": 3},
"ichi": {"class": "frequent", "desc": "listed as common in Ichimango Goi Bunruishuu", "order": 3},
"spec": {"class": "frequent", "desc": "common words not included in frequency lists", "order": 3},
"gai": {"class": "frequent", "desc": "common loanword", "order": 3},
"jouyou": {"class": "frequent", "desc": "included in list of regular-use characters", "order": 3},
"jinmeiyou": {"class": "frequent", "desc": "included in list of characters for use in personal names", "order": 3},
"jlpt": {"desc": "corresponding Japanese Language Proficiency Test level"},
"grade": {"desc": "school grade level at which the character is taught"},
"strokes": {"desc": "number of strokes needed to write the character"},
"heisig": {"desc": "frame number in Remembering the Kanji"},
"surname": {"class": "name", "order": 4},
"place": {"class": "name", "order": 4},
"unclass": {"class": "name", "order": 4},
"company": {"class": "name", "order": 4},
"product": {"class": "name", "order": 4},
"work": {"class": "name", "order": 4},
"masc": {"class": "name", "order": 4},
"fem": {"class": "name", "order": 4},
"person": {"class": "name", "order": 4},
"given": {"class": "name", "order": 4},
"station": {"class": "name", "order": 4},
"organization": {"class": "name", "order": 4}
}

View File

@ -20,7 +20,7 @@
class Dictionary { class Dictionary {
constructor() { constructor() {
this.db = null; this.db = null;
this.dbVer = 2; this.dbVer = 3;
this.entities = null; this.entities = null;
} }
@ -135,49 +135,31 @@ class Dictionary {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); const entitiesLoaded = entities => {
return loadJson(indexUrl).then(index => { this.entities = entities || {};
const entities = [];
for (const [name, value] of index.ents) { const rows = [];
entities.push({name, value}); for (const name in entities || {}) {
rows.push({name, value: entities[name]});
} }
return this.db.entities.bulkAdd(entities).then(() => { return this.db.entities.bulkAdd(rows);
if (this.entities === null) { };
this.entities = {};
}
for (const entity of entities) { const entriesLoaded = (entries, total, current) => {
this.entities[entity.name] = entity.value; const rows = [];
} for (const [expression, reading, tags, ...glossary] of entries) {
}).then(() => { rows.push({expression, reading, tags, glossary});
const loaders = []; }
for (let i = 1; i <= index.banks; ++i) {
const bankUrl = `${indexDir}/bank_${i}.json`;
loaders.push(() => {
return loadJson(bankUrl).then(definitions => {
const rows = [];
for (const [expression, reading, tags, ...glossary] of definitions) {
rows.push({expression, reading, tags, glossary});
}
return this.db.terms.bulkAdd(rows).then(() => { return this.db.terms.bulkAdd(rows).then(() => {
if (callback) { if (callback) {
callback(i, index.banks, indexUrl); callback(current, total, indexUrl);
}
});
});
});
} }
let chain = Promise.resolve();
for (const loader of loaders) {
chain = chain.then(loader);
}
return chain;
}); });
}); };
return importJsonDb(indexUrl, entitiesLoaded, entriesLoaded);
} }
importKanjiDict(indexUrl, callback) { importKanjiDict(indexUrl, callback) {
@ -185,33 +167,19 @@ class Dictionary {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); const entriesLoaded = (entries, total, current) => {
return loadJson(indexUrl).then(index => { const rows = [];
const loaders = []; for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
for (let i = 1; i <= index.banks; ++i) { rows.push({character, onyomi, kunyomi, tags, meanings});
const bankUrl = `${indexDir}/bank_${i}.json`;
loaders.push(() => {
return loadJson(bankUrl).then(definitions => {
const rows = [];
for (const [character, onyomi, kunyomi, tags, ...meanings] of definitions) {
rows.push({character, onyomi, kunyomi, tags, meanings});
}
return this.db.kanji.bulkAdd(rows).then(() => {
if (callback) {
callback(i, index.banks, indexUrl);
}
});
});
});
} }
let chain = Promise.resolve(); return this.db.kanji.bulkAdd(rows).then(() => {
for (const loader of loaders) { if (callback) {
chain = chain.then(loader); callback(current, total, indexUrl);
} }
});
};
return chain; return importJsonDb(indexUrl, null, entriesLoaded);
});
} }
} }

View File

@ -116,3 +116,27 @@ function applyTagMeta(tag, meta) {
function splitField(field) { function splitField(field) {
return field.length === 0 ? [] : field.split(' '); return field.length === 0 ? [] : field.split(' ');
} }
function importJsonDb(indexUrl, entitiesLoaded, entriesLoaded) {
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
return loadJson(indexUrl).then(index => {
if (entitiesLoaded !== null) {
return entitiesLoaded(index.entities, index.banks).then(() => index);
}
return index;
}).then(index => {
const loaders = [];
for (let i = 1; i <= index.banks; ++i) {
const bankUrl = `${indexDir}/bank_${i}.json`;
loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(entries, index.banks, i)));
}
let chain = Promise.resolve();
for (const loader of loaders) {
chain = chain.then(loader);
}
return chain;
});
}