Work on deinflector

This commit is contained in:
Alex Yatskov 2016-09-10 18:57:00 -07:00
parent 133abb6f58
commit 05ac931285
2 changed files with 35 additions and 27 deletions

View File

@ -26,7 +26,8 @@ class Deinflection {
} }
validate(validator) { validate(validator) {
for (const tags of validator(this.term)) { return validator(this.term).then(tagSets => {
for (const tags of tagSets) {
if (this.tags.length === 0) { if (this.tags.length === 0) {
return true; return true;
} }
@ -39,13 +40,16 @@ class Deinflection {
} }
return false; return false;
});
} }
deinflect(validator, rules) { deinflect(validator, rules) {
if (this.validate(validator)) { const promises = [
this.validate(validator).then(valid => {
const child = new Deinflection(this.term, this.tags); const child = new Deinflection(this.term, this.tags);
this.children.push(child); this.children.push(child);
} })
];
for (const rule in rules) { for (const rule in rules) {
for (const variant of rules[rule]) { for (const variant of rules[rule]) {
@ -63,13 +67,19 @@ class Deinflection {
const term = this.term.slice(0, -variant.ki.length) + variant.ko; const term = this.term.slice(0, -variant.ki.length) + variant.ko;
const child = new Deinflection(term, variant.to, rule); const child = new Deinflection(term, variant.to, rule);
if (child.deinflect(validator, rules)) { promises.push(
child.deinflect(validator, rules).then(valid => {
if (valid) {
this.children.push(child); this.children.push(child);
} }
} }
));
}
} }
return Promise.all(promises).then(() => {
return this.children.length > 0; return this.children.length > 0;
});
} }
gather() { gather() {
@ -105,10 +115,8 @@ class Deinflector {
deinflect(term, validator) { deinflect(term, validator) {
const node = new Deinflection(term); const node = new Deinflection(term);
if (node.deinflect(validator, this.rules)) { return node.deinflect(validator, this.rules).then(success => {
return node.gather(); return success ? node.gather() : null;
} });
return null;
} }
} }

View File

@ -59,12 +59,12 @@ class Translator {
for (let i = text.length; i > 0; --i) { for (let i = text.length; i > 0; --i) {
const term = text.slice(0, i); const term = text.slice(0, i);
const dfs = this.deinflector.deinflect(term, t => { const dfs = this.deinflector.deinflect(term, t => {
const tags = []; const tagSets = [];
for (const d of this.dictionary.findTerm(t)) { for (const d of this.dictionary.findTerm(t)) {
tags.push(d.tags); tagSets.push(d.tags);
} }
return tags; return tagSets;
}); });
if (dfs === null) { if (dfs === null) {
@ -114,7 +114,7 @@ class Translator {
length = Math.max(length, result.source.length); length = Math.max(length, result.source.length);
} }
return {definitions: definitions, length: length}; return {definitions, length};
} }
findKanji(text) { findKanji(text) {