Fix deinflections not being handled correctly

This commit is contained in:
toasted-nutbread 2019-09-30 22:09:16 -04:00
parent 1286b5115f
commit 44119eea2c

View File

@ -218,22 +218,55 @@ class Translator {
return []; 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);
}
for (const d of definitions) { const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles);
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); 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) { getDeinflections(text) {
const deinflections = []; const deinflections = [];
const deinflectionsKeys = {};
for (let i = text.length; i > 0; --i) { for (let i = text.length; i > 0; --i) {
const textSlice = text.slice(0, i); const textSlice = text.slice(0, i);
Translator.addUniqueDeinflections(this.deinflector.deinflect(textSlice), deinflections, deinflectionsKeys); deinflections.push(...this.deinflector.deinflect(textSlice));
} }
return deinflections; return deinflections;
@ -241,30 +274,19 @@ class Translator {
getDeinflections2(text, text2) { getDeinflections2(text, text2) {
const deinflections = []; const deinflections = [];
const deinflectionsKeys = {};
for (let i = text.length; i > 0; --i) { for (let i = text.length; i > 0; --i) {
const textSlice = text.slice(0, i); const textSlice = text.slice(0, i);
const text2Slice = text2.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) { if (textSlice !== text2Slice) {
Translator.addUniqueDeinflections(this.deinflector.deinflect(text2Slice), deinflections, deinflectionsKeys); deinflections.push(...this.deinflector.deinflect(text2Slice));
} }
} }
return deinflections; 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) { async findKanji(text, dictionaries) {
let definitions = []; let definitions = [];
const processed = {}; const processed = {};