fixing various deinflection issues

This commit is contained in:
Alex Yatskov 2016-12-20 21:54:01 -08:00
parent b07ad8eed4
commit dd3db8faad
3 changed files with 31 additions and 45 deletions

View File

@ -2009,14 +2009,6 @@
"v5" "v5"
] ]
}, },
{
"kanaIn": "った",
"kanaOut": "く",
"rulesIn": [],
"rulesOut": [
"v5"
]
},
{ {
"kanaIn": "った", "kanaIn": "った",
"kanaOut": "つ", "kanaOut": "つ",

View File

@ -18,18 +18,18 @@
class Deinflection { class Deinflection {
constructor(term, rules=[], reason='') { constructor(term, {rules=[], definitions=[], reason=''} = {}) {
this.children = [];
this.term = term; this.term = term;
this.rules = rules; this.rules = rules;
this.definitions = definitions;
this.reason = reason; this.reason = reason;
this.definitions = []; this.children = [];
} }
deinflect(definer, reasons, entry=false) { deinflect(definer, reasons) {
const validate = () => { const define = () => {
return definer(this.term).then(definitions => { return definer(this.term).then(definitions => {
if (entry) { if (this.rules.length === 0) {
this.definitions = definitions; this.definitions = definitions;
} else { } else {
for (const rule of this.rules) { for (const rule of this.rules) {
@ -45,26 +45,20 @@ class Deinflection {
}); });
}; };
const promises = [ const promises = [];
validate().then(valid => {
const child = new Deinflection(this.term, this.rules);
this.children.push(child);
})
];
for (const reason in reasons) { for (const reason in reasons) {
for (const variant of reasons[reason]) { for (const variant of reasons[reason]) {
let allowed = entry; let accept = this.rules.length === 0;
if (!allowed) { if (!accept) {
for (const rule of this.rules) { for (const rule of this.rules) {
if (variant.rulesIn.includes(rule)) { if (variant.rulesIn.includes(rule)) {
allowed = true; accept = true;
break; break;
} }
} }
} }
if (!allowed || !this.term.endsWith(variant.kanaIn)) { if (!accept || !this.term.endsWith(variant.kanaIn)) {
continue; continue;
} }
@ -73,46 +67,46 @@ class Deinflection {
continue; continue;
} }
const child = new Deinflection(term, variant.rulesOut, reason); const child = new Deinflection(term, {reason, rules: variant.rulesOut});
promises.push( promises.push(
child.deinflect(definer, reasons).then(valid => { child.deinflect(definer, reasons).then(valid => valid && this.children.push(child))
if (valid) { );
this.children.push(child);
}
}
));
} }
} }
return Promise.all(promises).then(() => { return Promise.all(promises).then(define).then(valid => {
return this.children.length > 0; if (valid && this.children.length > 0) {
const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions});
this.children.push(child);
}
return valid || this.children.length > 0;
}); });
} }
gather() { gather() {
if (this.children.length === 0) { if (this.children.length === 0) {
return [{ return [{
root: this.term, source: this.term,
rules: this.rules, rules: this.rules,
definitions: this.definitions, definitions: this.definitions,
reasons: [] reasons: [this.reason]
}]; }];
} }
const paths = []; const results = [];
for (const child of this.children) { for (const child of this.children) {
for (const path of child.gather()) { for (const result of child.gather()) {
path.definitions = path.definitions.concat(this.definitions);
if (this.reason.length > 0) { if (this.reason.length > 0) {
path.reasons.push(this.reason); result.reasons.push(this.reason);
} }
path.source = this.term; result.source = this.term;
paths.push(path); results.push(result);
} }
} }
return paths; return results;
} }
} }
@ -128,6 +122,6 @@ class Deinflector {
deinflect(term, definer) { deinflect(term, definer) {
const node = new Deinflection(term); const node = new Deinflection(term);
return node.deinflect(definer, this.reasons, true).then(success => success ? node.gather() : []); return node.deinflect(definer, this.reasons).then(success => success ? node.gather() : []);
} }
} }

View File

@ -105,7 +105,7 @@ class Translator {
return Promise.all(promises).then(() => groups); return Promise.all(promises).then(() => groups);
} }
processDeinflection(groups, {source, rules, reasons, root, definitions}, dictionaries) { processDeinflection(groups, {source, rules, reasons, definitions}, dictionaries) {
for (const definition of definitions) { for (const definition of definitions) {
if (definition.id in groups) { if (definition.id in groups) {
continue; continue;