deinflector optimizations
This commit is contained in:
parent
0aa603694c
commit
28b8bae6a7
File diff suppressed because it is too large
Load Diff
@ -23,24 +23,25 @@ class Deinflection {
|
||||
this.term = term;
|
||||
this.rules = rules;
|
||||
this.reason = reason;
|
||||
this.definitions = [];
|
||||
}
|
||||
|
||||
deinflect(validator, reasons, entry=false) {
|
||||
deinflect(definer, reasons, entry=false) {
|
||||
const validate = () => {
|
||||
if (entry) {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
return validator(this.term).then(sets => {
|
||||
for (const rules of sets) {
|
||||
return definer(this.term).then(definitions => {
|
||||
if (entry) {
|
||||
this.definitions = definitions;
|
||||
} else {
|
||||
for (const rule of this.rules) {
|
||||
if (rules.includes(rule)) {
|
||||
return true;
|
||||
for (const definition of definitions) {
|
||||
if (definition.rules.includes(rule)) {
|
||||
this.definitions.push(definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return this.definitions.length > 0;
|
||||
});
|
||||
};
|
||||
|
||||
@ -74,7 +75,7 @@ class Deinflection {
|
||||
|
||||
const child = new Deinflection(term, variant.rulesOut, reason);
|
||||
promises.push(
|
||||
child.deinflect(validator, reasons).then(valid => {
|
||||
child.deinflect(definer, reasons).then(valid => {
|
||||
if (valid) {
|
||||
this.children.push(child);
|
||||
}
|
||||
@ -90,12 +91,18 @@ class Deinflection {
|
||||
|
||||
gather() {
|
||||
if (this.children.length === 0) {
|
||||
return [{root: this.term, rules: this.rules, reasons: []}];
|
||||
return [{
|
||||
root: this.term,
|
||||
rules: this.rules,
|
||||
definitions: this.definitions,
|
||||
reasons: []
|
||||
}];
|
||||
}
|
||||
|
||||
const paths = [];
|
||||
for (const child of this.children) {
|
||||
for (const path of child.gather()) {
|
||||
path.definitions = path.definitions.concat(this.definitions);
|
||||
if (this.reason.length > 0) {
|
||||
path.reasons.push(this.reason);
|
||||
}
|
||||
@ -119,8 +126,8 @@ class Deinflector {
|
||||
this.reasons = reasons;
|
||||
}
|
||||
|
||||
deinflect(term, validator) {
|
||||
deinflect(term, definer) {
|
||||
const node = new Deinflection(term);
|
||||
return node.deinflect(validator, this.reasons, true).then(success => success ? node.gather() : []);
|
||||
return node.deinflect(definer, this.reasons, true).then(success => success ? node.gather() : []);
|
||||
}
|
||||
}
|
||||
|
@ -88,68 +88,41 @@ class Translator {
|
||||
}
|
||||
|
||||
findDeinflectionGroups(text, dictionaries) {
|
||||
const deinflectionGroups = {};
|
||||
const deinflectionPromises = [];
|
||||
const definer = term => this.database.findTerm(term, dictionaries);
|
||||
const groups = {};
|
||||
const promises = [];
|
||||
|
||||
for (let i = text.length; i > 0; --i) {
|
||||
deinflectionPromises.push(
|
||||
this.deinflector.deinflect(text.slice(0, i), term => {
|
||||
return this.database.findTerm(term, dictionaries).then(definitions => definitions.map(definition => definition.rules));
|
||||
}).then(deinflections => {
|
||||
const processPromises = [];
|
||||
promises.push(
|
||||
this.deinflector.deinflect(text.slice(0, i), definer).then(deinflections => {
|
||||
for (const deinflection of deinflections) {
|
||||
processPromises.push(
|
||||
this.processDeinflection(
|
||||
deinflectionGroups,
|
||||
deinflection.source,
|
||||
deinflection.rules,
|
||||
deinflection.reasons,
|
||||
deinflection.root,
|
||||
dictionaries
|
||||
)
|
||||
);
|
||||
this.processDeinflection(groups, deinflection);
|
||||
}
|
||||
|
||||
return Promise.all(processPromises);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return Promise.all(deinflectionPromises).then(() => deinflectionGroups);
|
||||
return Promise.all(promises).then(() => groups);
|
||||
}
|
||||
|
||||
processDeinflection(groups, source, rules, reasons, root, dictionaries) {
|
||||
return this.database.findTerm(root, dictionaries).then(definitions => {
|
||||
for (const definition of definitions) {
|
||||
if (definition.id in groups) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let matched = rules.length === 0;
|
||||
for (const rule of rules) {
|
||||
if (definition.rules.includes(rule)) {
|
||||
matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!matched) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
|
||||
groups[definition.id] = {
|
||||
source,
|
||||
reasons,
|
||||
score: definition.score,
|
||||
dictionary: definition.dictionary,
|
||||
expression: definition.expression,
|
||||
reading: definition.reading,
|
||||
glossary: definition.glossary,
|
||||
tags: sortTags(tags)
|
||||
};
|
||||
processDeinflection(groups, {source, rules, reasons, root, definitions}, dictionaries) {
|
||||
for (const definition of definitions) {
|
||||
if (definition.id in groups) {
|
||||
continue;
|
||||
}
|
||||
});
|
||||
|
||||
const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
|
||||
groups[definition.id] = {
|
||||
source,
|
||||
reasons,
|
||||
score: definition.score,
|
||||
dictionary: definition.dictionary,
|
||||
expression: definition.expression,
|
||||
reading: definition.reading,
|
||||
glossary: definition.glossary,
|
||||
tags: sortTags(tags)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
processKanji(definitions) {
|
||||
|
Loading…
Reference in New Issue
Block a user