diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-30 22:09:16 -0400 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-30 22:09:16 -0400 |
commit | 44119eea2c8ad4c8eed38070ef1a3ce27fa9359e (patch) | |
tree | f68af0730a7831d5b5b8f8f4d1cbd0fa996d062c | |
parent | 1286b5115f101a822e0083d0c642b38a28b4fea7 (diff) |
Fix deinflections not being handled correctly
-rw-r--r-- | ext/bg/js/translator.js | 58 |
1 files changed, 40 insertions, 18 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 4eb4b03a..65d746ea 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -218,22 +218,55 @@ class Translator { return []; } - const definitions = await this.database.findTermsBulk(deinflections.map(e => e.term), titles); + const uniqueDeinflectionTerms = []; + const uniqueDeinflectionArrays = []; + const uniqueDeinflectionsMap = {}; + for (const deinflection of deinflections) { + const term = deinflection.term; + let deinflectionArray; + if (uniqueDeinflectionsMap.hasOwnProperty(term)) { + deinflectionArray = uniqueDeinflectionsMap[term]; + } else { + deinflectionArray = []; + uniqueDeinflectionTerms.push(term); + uniqueDeinflectionArrays.push(deinflectionArray); + uniqueDeinflectionsMap[term] = deinflectionArray; + } + deinflectionArray.push(deinflection); + } + + const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles); - for (const d of definitions) { - deinflections[d.index].definitions.push(d); + for (const definition of definitions) { + for (const deinflection of uniqueDeinflectionArrays[definition.index]) { + if (Translator.definitionContainsAnyRule(definition, deinflection.rules)) { + deinflection.definitions.push(definition); + } + } } return deinflections.filter(e => e.definitions.length > 0); } + static definitionContainsAnyRule(definition, rules) { + if (rules.length === 0) { + return true; + } + const definitionRules = definition.rules; + for (const rule of rules) { + if (definitionRules.includes(rule)) { + return true; + } + } + return false; + } + getDeinflections(text) { const deinflections = []; - const deinflectionsKeys = {}; for (let i = text.length; i > 0; --i) { const textSlice = text.slice(0, i); - Translator.addUniqueDeinflections(this.deinflector.deinflect(textSlice), deinflections, deinflectionsKeys); + deinflections.push(...this.deinflector.deinflect(textSlice)); } return deinflections; @@ -241,30 +274,19 @@ class Translator { getDeinflections2(text, text2) { const deinflections = []; - const deinflectionsKeys = {}; for (let i = text.length; i > 0; --i) { const textSlice = text.slice(0, i); const text2Slice = text2.slice(0, i); - Translator.addUniqueDeinflections(this.deinflector.deinflect(textSlice), deinflections, deinflectionsKeys); + deinflections.push(...this.deinflector.deinflect(textSlice)); if (textSlice !== text2Slice) { - Translator.addUniqueDeinflections(this.deinflector.deinflect(text2Slice), deinflections, deinflectionsKeys); + deinflections.push(...this.deinflector.deinflect(text2Slice)); } } return deinflections; } - static addUniqueDeinflections(newValues, deinflections, deinflectionsKeys) { - for (const value of newValues) { - const key = value.term; - if (!deinflectionsKeys.hasOwnProperty(key)) { - deinflections.push(value); - deinflectionsKeys[key] = true; - } - } - } - async findKanji(text, dictionaries) { let definitions = []; const processed = {}; |