summaryrefslogtreecommitdiff
path: root/ext/bg/dictionary.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-03-31 20:03:39 -0700
committerAlex Yatskov <alex@foosoft.net>2016-03-31 20:03:39 -0700
commit7eadff3457690074c5c0140a6e9ffd6164021176 (patch)
treead8ba8c31cba11f54ca8cab186d1d36090e070c0 /ext/bg/dictionary.js
parentb97e75ba32781341c221f549780f3444d0916714 (diff)
Moving large files to CSV format, deleting unused kradfile
Diffstat (limited to 'ext/bg/dictionary.js')
-rw-r--r--ext/bg/dictionary.js77
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;
+ }
}
}