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,22 +71,24 @@ 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 => {
results.push({ if (dictionaries.includes(row.dictionary)) {
expression: row.expression, results.push({
reading: row.reading, expression: row.expression,
tags: splitField(row.tags), reading: row.reading,
glossary: row.glossary, tags: splitField(row.tags),
id: row.id glossary: row.glossary,
}); 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,33 +98,39 @@ 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 => {
results.push({ if (dictionaries.includes(row.dictionary)) {
character: row.character, results.push({
onyomi: splitField(row.onyomi), character: row.character,
kunyomi: splitField(row.kunyomi), onyomi: splitField(row.onyomi),
tags: splitField(row.tags), kunyomi: splitField(row.kunyomi),
glossary: row.meanings tags: splitField(row.tags),
}); glossary: row.meanings
}).then(() => results); });
}
}).then(() => {
return this.getEntities(dictionaries);
}).then(entities => {
for (const result of results) {
result.entities = entities;
}
return results;
});
} }
getEntities(tags) { 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}) {