move deinflector to async

This commit is contained in:
Alex Yatskov 2017-07-10 14:30:34 -07:00
parent b6f3919ef6
commit 49352c5fa1
2 changed files with 37 additions and 43 deletions

View File

@ -26,26 +26,7 @@ class Deinflection {
this.children = []; this.children = [];
} }
deinflect(definer, reasons) { async deinflect(definer, reasons) {
const define = () => {
return definer(this.term).then(definitions => {
if (this.rules.length === 0) {
this.definitions = definitions;
} else {
for (const rule of this.rules) {
for (const definition of definitions) {
if (definition.rules.includes(rule)) {
this.definitions.push(definition);
}
}
}
}
return this.definitions.length > 0;
});
};
const promises = [];
for (const reason in reasons) { for (const reason in reasons) {
for (const variant of reasons[reason]) { for (const variant of reasons[reason]) {
let accept = this.rules.length === 0; let accept = this.rules.length === 0;
@ -68,20 +49,31 @@ class Deinflection {
} }
const child = new Deinflection(term, {reason, rules: variant.rulesOut}); const child = new Deinflection(term, {reason, rules: variant.rulesOut});
promises.push( if (await child.deinflect(definer, reasons)) {
child.deinflect(definer, reasons).then(valid => valid && this.children.push(child)) this.children.push(child);
); }
} }
} }
return Promise.all(promises).then(define).then(valid => { const definitions = await definer(this.term);
if (valid && this.children.length > 0) { if (this.rules.length === 0) {
const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions}); this.definitions = definitions;
this.children.push(child); } else {
for (const rule of this.rules) {
for (const definition of definitions) {
if (definition.rules.includes(rule)) {
this.definitions.push(definition);
}
}
} }
}
return valid || this.children.length > 0; if (this.definitions.length > 0 && this.children.length > 0) {
}); const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions});
this.children.push(child);
}
return this.definitions.length > 0 || this.children.length > 0;
} }
gather() { gather() {
@ -112,16 +104,16 @@ class Deinflection {
class Deinflector { class Deinflector {
constructor() { constructor(reasons) {
this.reasons = {};
}
setReasons(reasons) {
this.reasons = reasons; this.reasons = reasons;
} }
deinflect(term, definer) { async deinflect(term, definer) {
const node = new Deinflection(term); const node = new Deinflection(term);
return node.deinflect(definer, this.reasons).then(success => success ? node.gather() : []); if (await node.deinflect(definer, this.reasons)) {
return node.gather();
} else {
return [];
}
} }
} }

View File

@ -19,17 +19,19 @@
class Translator { class Translator {
constructor() { constructor() {
this.database = new Database(); this.database = null;
this.deinflector = new Deinflector(); this.deinflector = null;
this.loaded = false;
} }
async prepare() { async prepare() {
if (!this.loaded) { if (!this.database) {
const reasons = await jsonLoadInt('/bg/lang/deinflect.json'); this.database = new Database();
this.deinflector.setReasons(reasons);
await this.database.prepare(); await this.database.prepare();
this.loaded = true; }
if (!this.deinflector) {
const reasons = await jsonLoadInt('/bg/lang/deinflect.json');
this.deinflector = new Deinflector(reasons);
} }
} }