diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-11-12 19:29:30 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-11-12 19:29:30 -0800 |
commit | b5cc47a9d1d7df7ec5a596fb72fad21f79d2e01c (patch) | |
tree | 6bfa32412591355593d25d6dc007470ea44ca9bd | |
parent | 0ff41d5843b3bf39994ae32e4ab623bfe0e4e3ca (diff) |
WIP
-rw-r--r-- | ext/bg/js/database.js | 57 | ||||
-rw-r--r-- | ext/bg/js/options-form.js | 2 | ||||
-rw-r--r-- | ext/bg/js/translator.js | 71 | ||||
-rw-r--r-- | ext/bg/js/util.js | 1 | ||||
-rw-r--r-- | ext/bg/js/yomichan.js | 24 |
5 files changed, 92 insertions, 63 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 24ee7c03..7f4c6ac6 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -21,7 +21,6 @@ class Database { constructor() { this.db = null; this.dbVer = 6; - this.entities = null; } init() { @@ -72,22 +71,24 @@ 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 => { - results.push({ - expression: row.expression, - reading: row.reading, - tags: splitField(row.tags), - glossary: row.glossary, - id: row.id - }); + if (dictionaries.includes(row.dictionary)) { + results.push({ + expression: row.expression, + reading: row.reading, + tags: splitField(row.tags), + 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,33 +98,39 @@ 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 => { - results.push({ - character: row.character, - onyomi: splitField(row.onyomi), - kunyomi: splitField(row.kunyomi), - tags: splitField(row.tags), - glossary: row.meanings - }); - }).then(() => results); + if (dictionaries.includes(row.dictionary)) { + results.push({ + character: row.character, + onyomi: splitField(row.onyomi), + kunyomi: splitField(row.kunyomi), + tags: splitField(row.tags), + glossary: row.meanings + }); + } + }).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) { 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; diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 501848ad..3d1530ad 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -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(() => { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 486740c0..ed8c720b 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -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; diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 914164b1..3ec80350 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -39,6 +39,7 @@ function promiseCallback(promise, callback) { return promise.then(result => { callback({result}); }).catch(error => { + console.log(error); callback({error}); }); } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 7bca579d..d471add4 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -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}) { |