dictionary result caching

This commit is contained in:
Alex Yatskov 2016-12-20 22:43:27 -08:00
parent dd3db8faad
commit 45bf72b40b

View File

@ -42,10 +42,11 @@ class Translator {
} }
findTerm(text, dictionaries, enableSoftKatakanaSearch) { 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); const textHiragana = wanakana._katakanaToHiragana(text);
if (text !== textHiragana && enableSoftKatakanaSearch) { if (text !== textHiragana && enableSoftKatakanaSearch) {
return this.findDeinflectionGroups(textHiragana, dictionaries).then(groupsHiragana => { return this.findDeinflectionGroups(textHiragana, dictionaries, cache).then(groupsHiragana => {
for (const key in groupsHiragana) { for (const key in groupsHiragana) {
groups[key] = groups[key] || groupsHiragana[key]; groups[key] = groups[key] || groupsHiragana[key];
} }
@ -74,9 +75,7 @@ class Translator {
} }
findKanji(text, dictionaries) { findKanji(text, dictionaries) {
const processed = {}; const processed = {}, promises = [];
const promises = [];
for (const c of text) { for (const c of text) {
if (!processed[c]) { if (!processed[c]) {
promises.push(this.database.findKanji(c, dictionaries)); 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), []))); return Promise.all(promises).then(sets => this.processKanji(sets.reduce((a, b) => a.concat(b), [])));
} }
findDeinflectionGroups(text, dictionaries) { findDeinflectionGroups(text, dictionaries, cache) {
const definer = term => this.database.findTerm(term, dictionaries); const definer = term => {
const groups = {}; if (cache.hasOwnProperty(term)) {
const promises = []; 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) { for (let i = text.length; i > 0; --i) {
promises.push( promises.push(
this.deinflector.deinflect(text.slice(0, i), definer).then(deinflections => { this.deinflector.deinflect(text.slice(0, i), definer).then(deinflections => {