diff options
Diffstat (limited to 'ext/bg')
-rw-r--r-- | ext/bg/dictionary.js | 77 |
1 files changed, 36 insertions, 41 deletions
diff --git a/ext/bg/dictionary.js b/ext/bg/dictionary.js index eff54890..30c34687 100644 --- a/ext/bg/dictionary.js +++ b/ext/bg/dictionary.js @@ -19,43 +19,33 @@ class Dictionary { constructor() { - this.termDicts = []; - this.kanjiDicts = []; - } - - addTermDict(termDict) { - this.termDicts.push(termDict); - } + this.terms = []; + this.termIndices = {}; - addKanjiDict(kanjiDict) { - this.kanjiDicts.push(kanjiDict); + this.kanji = []; + this.kanjiIndices = {}; } - - findTerm(term) { - let results = []; - for (let dict of this.termDicts) { - results = results.concat(this.findTermInDict(term, dict)); + addTermDict(terms) { + let index = this.terms.length; + for (const [e, r, g, t] in terms) { + this.storeIndex(this.termIndices, e, index); + this.storeIndex(this.termIndices, r, index++); + this.terms.push([e, r, g, t]); } - - return results; } - findKanji(kanji) { - const results = []; - for (let dict of this.kanjiDicts) { - const result = this.findKanjiInDict(kanji, dict); - if (result !== null) { - results.push(result); - } + addKanjiDict(kanji) { + let index = this.kanji.length; + for (const [c, k, o, g] in kanji) { + this.storeIndex(this.kanjiIndices, c, index++); + this.kanji.push([c, k, o, g]); } - - return results; } - findTermInDict(term, dict) { - return (dict.indices[term] || []).map(index => { - const [e, r, g, t] = dict.defs[index]; + findTerm(term) { + return (this.termIndices[term] || []).map(index => { + const [e, r, g, t] = this.terms[index]; return { id: index, expression: e, @@ -66,19 +56,24 @@ class Dictionary { }); } - findKanjiInDict(kanji, dict) { - const def = dict.defs[kanji]; - if (def === null) { - return null; - } + findKanji(kanji) { + return (this.kanjiIndices[kanji] || []).map(index => { + const [c, k, o, g] = def; + return { + id: kanji.charCodeAt(0), + character: c, + kunyomi: k, + onyomi: o, + glossary: g + }; + }); + } - const [c, k, o, g] = def; - return { - id: kanji.charCodeAt(0), - character: c, - kunyomi: k, - onyomi: o, - glossary: g - }; + storeIndex(indices, term, index) { + if (term.length > 0) { + const indices = this.termIndices[term] || []; + indices.push(term); + this.termIndices[term] = indices; + } } } |