diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-04-17 18:13:20 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-04-17 18:13:20 -0700 |
commit | f079db0471424a873f22315c7911571d467e97ad (patch) | |
tree | 96860218da185c478e269a7c12e6210ccba7ee3f /ext/bg/js | |
parent | bcd34149ab120f4bd0b823fbd6ae4efadfb02e90 (diff) |
Support switching between edict and enamdict
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/dictionary.js | 39 | ||||
-rw-r--r-- | ext/bg/js/templates.js | 2 | ||||
-rw-r--r-- | ext/bg/js/translator.js | 14 | ||||
-rw-r--r-- | ext/bg/js/yomichan.js | 4 |
4 files changed, 24 insertions, 35 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 6870601e..b91e53bf 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -31,35 +31,24 @@ class Dictionary { this.kanjiDicts[name] = dict; } - findTerm(term) { - let results = []; - - for (const name in this.termDicts) { - const dict = this.termDicts[name]; - const indices = dict.indices[term] || []; - - results = results.concat( - indices.map(index => { - const [e, r, t, ...g] = dict.defs[index]; - return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')}; - }) - ); - } - - return results; + findTerm(term, dict) { + const db = this.termDicts[dict]; + const indices = db.indices[term] || []; + + return indices.map(index => { + const [e, r, t, ...g] = db.defs[index]; + return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')}; + }); } - findKanji(kanji) { - const results = []; + findKanji(kanji, dict) { + const def = this.termDicts[dict][kanji]; - for (const name in this.termDicts) { - const def = this.termDicts[name][kanji]; - if (def) { - const [c, k, o, g] = def; - results.push({id: index, character: c, kunyomi: k, onyomi: o, glossary: g}); - } + if (def) { + const [c, k, o, g] = def; + return {id: index, character: c, kunyomi: k, onyomi: o, glossary: g}; } - return results; + return null; } } diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 3388ec9b..46a4001f 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -11,7 +11,7 @@ templates['defs.html'] = template({"1":function(container,depth0,helpers,partial return "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <title></title>\n <link rel=\"stylesheet\" href=\"" + alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper))) - + "/css/popup.css\">\n </head>\n <body>\n <div class=\"dictionary\">\n <a href=\"javascript:selectDict('edict');\">単</a><a href=\"javascript:selectDict('enamdict')\">名</a><a href=\"javascript:selectDict('kanjidic');\">漢</a>\n </div>\n\n" + + "/css/popup.css\">\n </head>\n <body>\n <div class=\"dictionary\">\n <a href=\"javascript:setActiveDict('edict');\">単</a><a href=\"javascript:setActiveDict('enamdict')\">名</a><a href=\"javascript:setActiveDict('kanjidic');\">漢</a>\n </div>\n\n" + ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.defs : depth0),{"name":"each","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n <script src=\"" + alias4(((helper = (helper = helpers.root || (depth0 != null ? depth0.root : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"root","hash":{},"data":data}) : helper))) diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 46d63ed0..454d5079 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -63,14 +63,14 @@ class Translator { } } - findTerm(text) { + findTerm(text, dict) { const groups = {}; for (let i = text.length; i > 0; --i) { const term = text.slice(0, i); const dfs = this.deinflector.deinflect(term, t => { const tags = []; - for (const d of this.dictionary.findTerm(t)) { + for (const d of this.dictionary.findTerm(t, dict)) { tags.push(d.tags); } @@ -79,7 +79,7 @@ class Translator { if (dfs !== null) { for (const df of dfs) { - this.processTerm(groups, df.source, df.tags, df.rules, df.root); + this.processTerm(dict, groups, df.source, df.tags, df.rules, df.root); } } } @@ -125,13 +125,13 @@ class Translator { return {results: results, length: length}; } - findKanji(text) { + findKanji(text, dict) { let results = []; const processed = {}; for (const c of text) { if (!processed.has(c)) { - results = results.concat(this.dictionary.findKanji(c)); + results = results.concat(this.dictionary.findKanji(c, dict)); processed[c] = true; } } @@ -139,8 +139,8 @@ class Translator { return results; } - processTerm(groups, source, tags, rules=[], root='') { - for (const entry of this.dictionary.findTerm(root)) { + processTerm(dict, groups, source, tags, rules=[], root='') { + for (const entry of this.dictionary.findTerm(root, dict)) { if (entry.id in groups) { continue; } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index a66c1244..719aaed8 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -39,8 +39,8 @@ class Yomichan { onMessage(request, sender, callback) { const {action, data} = request; const handlers = { - findKanji: ({text}) => this.translator.onFindKanji(text), - findTerm: ({text}) => this.translator.findTerm(text), + findKanji: ({text, dict}) => this.translator.onFindKanji(text, dict), + findTerm: ({text, dict}) => this.translator.findTerm(text, dict), getState: () => this.state, getOptions: () => this.options, renderText: ({data, template}) => Handlebars.templates[template](data) |