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

View File

@ -116,3 +116,27 @@ function applyTagMeta(tag, meta) {
function splitField(field) {
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;
});
}