From bf28dea2838619c6446367f240e240cdddef3586 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 12 Apr 2016 20:58:41 -0700 Subject: Updating dictionaries --- ext/bg/js/dictionary.js | 33 +++++++-------------------------- ext/bg/js/translator.js | 15 ++------------- 2 files changed, 9 insertions(+), 39 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index a68c2daf..feb8c92b 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -19,31 +19,20 @@ class Dictionary { constructor() { - this.terms = []; - this.termIndices = {}; - - this.kanji = []; - this.kanjiIndices = {}; + this.termDicts = {}; + this.kanjiDicts = {}; } - addTermData(terms) { - let index = this.terms.length; - for (const [e, r, g, t] of terms) { - this.storeIndex(this.termIndices, e, index); - this.storeIndex(this.termIndices, r, index++); - this.terms.push([e, r, g, t]); - } + addTermDict(name, dict) { + this.termDicts[name] = dict; } - addKanjiData(kanji) { - let index = this.kanji.length; - for (const [c, k, o, g] of kanji) { - this.storeIndex(this.kanjiIndices, c, index++); - this.kanji.push([c, k, o, g]); - } + addKanjiDict(name, dict) { + this.kanjiDicts[name] = dict; } findTerm(term) { + const results = []; return (this.termIndices[term] || []).map(index => { const [e, r, g, t] = this.terms[index]; return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')}; @@ -56,12 +45,4 @@ class Dictionary { return {id: index, character: c, kunyomi: k, onyomi: o, glossary: g}; }); } - - storeIndex(indices, term, index) { - if (term.length > 0) { - const indices = this.termIndices[term] || []; - indices.push(index); - this.termIndices[term] = indices; - } - } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index e8224320..5414a553 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -46,11 +46,11 @@ class Translator { this.deinflector.setRules(JSON.parse(response)); break; case 'kanjidic': - this.dictionary.addKanjiData(Translator.parseCsv(response)); + this.dictionary.addKanjiDict('kanjidic', JSON.parse(response)); break; case 'edict': case 'enamdict': - this.dictionary.addTermData(Translator.parseCsv(response)); + this.dictionary.addTermDict(key, JSON.parse(response)); break; } @@ -164,15 +164,4 @@ class Translator { xhr.open('GET', chrome.extension.getURL(url), true); xhr.send(); } - - static parseCsv(data) { - const result = []; - for (const row of data.split('\n')) { - if (row.length > 0) { - result.push(row.split('\t')); - } - } - - return result; - } } -- cgit v1.2.3