aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/deinflector.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-08-17 19:19:34 -0700
committerAlex Yatskov <alex@foosoft.net>2017-08-17 19:19:34 -0700
commit7586572fbaab7de698ec13f8712cc95e24ab6273 (patch)
tree665417b73c928694b96c00a98dd882c618e1fd1d /ext/bg/js/deinflector.js
parent3475150b2d1424d43f5be6fcfbdbb719a576866f (diff)
parent191336522c220b0a3cfe41515ed23946b3462217 (diff)
Merge branch 'dev'
Diffstat (limited to 'ext/bg/js/deinflector.js')
-rw-r--r--ext/bg/js/deinflector.js66
1 files changed, 29 insertions, 37 deletions
diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js
index 6484f953..0abde99d 100644
--- a/ext/bg/js/deinflector.js
+++ b/ext/bg/js/deinflector.js
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
+ * Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* This program is free software: you can redistribute it and/or modify
@@ -26,26 +26,7 @@ class Deinflection {
this.children = [];
}
- 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 = [];
+ async deinflect(definer, reasons) {
for (const reason in reasons) {
for (const variant of reasons[reason]) {
let accept = this.rules.length === 0;
@@ -68,20 +49,31 @@ class Deinflection {
}
const child = new Deinflection(term, {reason, rules: variant.rulesOut});
- promises.push(
- child.deinflect(definer, reasons).then(valid => valid && this.children.push(child))
- );
+ if (await child.deinflect(definer, reasons)) {
+ this.children.push(child);
+ }
}
}
- return Promise.all(promises).then(define).then(valid => {
- if (valid && this.children.length > 0) {
- const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions});
- this.children.push(child);
+ const definitions = await definer(this.term);
+ 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);
+ }
+ }
}
+ }
+
+ 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 valid || this.children.length > 0;
- });
+ return this.definitions.length > 0 || this.children.length > 0;
}
gather() {
@@ -112,16 +104,16 @@ class Deinflection {
class Deinflector {
- constructor() {
- this.reasons = {};
- }
-
- setReasons(reasons) {
+ constructor(reasons) {
this.reasons = reasons;
}
- deinflect(term, definer) {
+ async deinflect(term, definer) {
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 [];
+ }
}
}