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

View File

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

View File

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

View File

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

View File

@ -239,11 +239,31 @@ class Yomichan {
} }
api_findKanji({text, callback}) { 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}) { 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}) { api_renderText({template, data, callback}) {