This commit is contained in:
Alex Yatskov 2016-11-12 19:29:30 -08:00
parent 0ff41d5843
commit b5cc47a9d1
5 changed files with 92 additions and 63 deletions

View File

@ -21,7 +21,6 @@ class Database {
constructor() {
this.db = null;
this.dbVer = 6;
this.entities = null;
}
init() {
@ -72,13 +71,14 @@ class Database {
return this.db.meta.put({name: 'version', value: this.dbVer});
}
findTerm(term) {
findTerm(term, dictionaries) {
if (this.db === null) {
return Promise.reject('database not initialized');
}
const results = [];
return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
if (dictionaries.includes(row.dictionary)) {
results.push({
expression: row.expression,
reading: row.reading,
@ -86,8 +86,9 @@ class Database {
glossary: row.glossary,
id: row.id
});
}
}).then(() => {
return this.getEntities();
return this.getEntities(dictionaries);
}).then(entities => {
for (const result of results) {
result.entities = entities;
@ -97,13 +98,14 @@ class Database {
});
}
findKanji(kanji) {
findKanji(kanji, dictionaries) {
if (this.db === null) {
return Promise.reject('database not initialized');
}
const results = [];
return this.db.kanji.where('character').equals(kanji).each(row => {
if (dictionaries.includes(row.dictionary)) {
results.push({
character: row.character,
onyomi: splitField(row.onyomi),
@ -111,19 +113,24 @@ class Database {
tags: splitField(row.tags),
glossary: row.meanings
});
}).then(() => results);
}
}).then(() => {
return this.getEntities(dictionaries);
}).then(entities => {
for (const result of results) {
result.entities = entities;
}
getEntities(tags) {
return results;
});
}
getEntities(dictionaries) {
if (this.db === null) {
return Promise.reject('database not initialized');
}
if (this.entities !== null) {
return Promise.resolve(this.entities);
}
return this.db.entities.toArray(rows => {
return this.db.entities.where('dictionary').anyOf(dictionaries).toArray(rows => {
this.entities = {};
for (const row of rows) {
this.entities[row.name] = row.value;

View File

@ -140,7 +140,7 @@ function populateDictionaries(opts) {
dictGroups.append($(html));
});
$('.dict-enable-terms, .dict-enable.kanji').change(onOptionsChanged);
$('.dict-enable-terms, .dict-enable-kanji').change(onOptionsChanged);
}).catch(error => {
dictError.show().find('span').text(error);
}).then(() => {

View File

@ -77,39 +77,11 @@ class Translator {
});
}
findTermGroups(text) {
const deinflectGroups = {};
const deinflectPromises = [];
for (let i = text.length; i > 0; --i) {
deinflectPromises.push(
this.deinflector.deinflect(text.slice(0, i), term => {
return this.database.findTerm(term).then(definitions => definitions.map(definition => definition.tags));
}).then(deinflects => {
const processPromises = [];
for (const deinflect of deinflects) {
processPromises.push(this.processTerm(
deinflectGroups,
deinflect.source,
deinflect.tags,
deinflect.rules,
deinflect.root
));
}
return Promise.all(processPromises);
})
);
}
return Promise.all(deinflectPromises).then(() => deinflectGroups);
}
findTerm(text, enableSoftKatakanaSearch) {
return this.findTermGroups(text).then(groups => {
findTerm(text, enableSoftKatakanaSearch, dictionaries) {
return this.findTermGroups(text, dictionaries).then(groups => {
const textHiragana = wanakana._katakanaToHiragana(text);
if (text !== textHiragana && enableSoftKatakanaSearch) {
return this.findTermGroups(textHiragana).then(groupsHiragana => {
return this.findTermGroups(textHiragana, dictionaries).then(groupsHiragana => {
for (const key in groupsHiragana) {
groups[key] = groups[key] || groupsHiragana[key];
}
@ -137,13 +109,13 @@ class Translator {
});
}
findKanji(text) {
findKanji(text, dictionaries) {
const processed = {};
const promises = [];
for (const c of text) {
if (!processed[c]) {
promises.push(this.database.findKanji(c).then((definitions) => definitions));
promises.push(this.database.findKanji(c, dictionaries));
processed[c] = true;
}
}
@ -151,8 +123,37 @@ class Translator {
return Promise.all(promises).then(sets => this.processKanji(sets.reduce((a, b) => a.concat(b), [])));
}
processTerm(groups, source, tags, rules, root) {
return this.database.findTerm(root).then(definitions => {
findTermGroups(text, dictionaries) {
const deinflectGroups = {};
const deinflectPromises = [];
for (let i = text.length; i > 0; --i) {
deinflectPromises.push(
this.deinflector.deinflect(text.slice(0, i), term => {
return this.database.findTerm(term, dictionaries).then(definitions => definitions.map(definition => definition.tags));
}).then(deinflects => {
const processPromises = [];
for (const deinflect of deinflects) {
processPromises.push(this.processTerm(
deinflectGroups,
deinflect.source,
deinflect.tags,
deinflect.rules,
deinflect.root,
dictionaries
));
}
return Promise.all(processPromises);
})
);
}
return Promise.all(deinflectPromises).then(() => deinflectGroups);
}
processTerm(groups, source, tags, rules, root, dictionaries) {
return this.database.findTerm(root, dictionaries).then(definitions => {
for (const definition of definitions) {
if (definition.id in groups) {
continue;

View File

@ -39,6 +39,7 @@ function promiseCallback(promise, callback) {
return promise.then(result => {
callback({result});
}).catch(error => {
console.log(error);
callback({error});
});
}

View File

@ -239,11 +239,31 @@ class Yomichan {
}
api_findKanji({text, callback}) {
promiseCallback(this.translator.findKanji(text), callback);
const dictionaries = [];
for (const title in this.options.dictionaries) {
if (this.options.dictionaries[title].enableKanji) {
dictionaries.push(title);
}
}
promiseCallback(
this.translator.findKanji(text, dictionaries),
callback
);
}
api_findTerm({text, callback}) {
promiseCallback(this.translator.findTerm(text, this.options.enableSoftKatakanaSearch), callback);
const dictionaries = [];
for (const title in this.options.dictionaries) {
if (this.options.dictionaries[title].enableTerms) {
dictionaries.push(title);
}
}
promiseCallback(
this.translator.findTerm(text, this.options.enableSoftKatakanaSearch, dictionaries),
callback
);
}
api_renderText({template, data, callback}) {