diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-12-20 22:43:27 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-12-20 22:43:27 -0800 |
commit | 45bf72b40bb1fd7a3c29679e34a0f5646bd7346c (patch) | |
tree | 455a5267dfdd4141ec9b6e4f860a75336865d82e /ext/bg/js/translator.js | |
parent | dd3db8faadb55ac2013df168a857a1d10d9184cd (diff) |
dictionary result caching
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r-- | ext/bg/js/translator.js | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 0b6b7c2d..f29b90c9 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -42,10 +42,11 @@ class Translator { } findTerm(text, dictionaries, enableSoftKatakanaSearch) { - return this.findDeinflectionGroups(text, dictionaries).then(groups => { + const cache = {}; + return this.findDeinflectionGroups(text, dictionaries, cache).then(groups => { const textHiragana = wanakana._katakanaToHiragana(text); if (text !== textHiragana && enableSoftKatakanaSearch) { - return this.findDeinflectionGroups(textHiragana, dictionaries).then(groupsHiragana => { + return this.findDeinflectionGroups(textHiragana, dictionaries, cache).then(groupsHiragana => { for (const key in groupsHiragana) { groups[key] = groups[key] || groupsHiragana[key]; } @@ -74,9 +75,7 @@ class Translator { } findKanji(text, dictionaries) { - const processed = {}; - const promises = []; - + const processed = {}, promises = []; for (const c of text) { if (!processed[c]) { promises.push(this.database.findKanji(c, dictionaries)); @@ -87,11 +86,16 @@ class Translator { return Promise.all(promises).then(sets => this.processKanji(sets.reduce((a, b) => a.concat(b), []))); } - findDeinflectionGroups(text, dictionaries) { - const definer = term => this.database.findTerm(term, dictionaries); - const groups = {}; - const promises = []; + findDeinflectionGroups(text, dictionaries, cache) { + const definer = term => { + if (cache.hasOwnProperty(term)) { + return Promise.resolve(cache[term]); + } + + return this.database.findTerm(term, dictionaries).then(definitions => cache[term] = definitions); + }; + const groups = {}, promises = []; for (let i = text.length; i > 0; --i) { promises.push( this.deinflector.deinflect(text.slice(0, i), definer).then(deinflections => { |