aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-09-10 18:57:00 -0700
committerAlex Yatskov <alex@foosoft.net>2016-09-10 18:57:00 -0700
commit05ac93128539cf4be5417a50728329c3ae070580 (patch)
treef9a4bfec98acec6343cce4e4da5010ab389ff0f6 /ext/bg/js
parent133abb6f585bee94fcdee23689779042e3ccfd65 (diff)
Work on deinflector
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/deinflector.js52
-rw-r--r--ext/bg/js/translator.js8
2 files changed, 34 insertions, 26 deletions
diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js
index 0eabd0f3..2d567e6b 100644
--- a/ext/bg/js/deinflector.js
+++ b/ext/bg/js/deinflector.js
@@ -26,26 +26,30 @@ class Deinflection {
}
validate(validator) {
- for (const tags of validator(this.term)) {
- if (this.tags.length === 0) {
- return true;
- }
-
- for (const tag of this.tags) {
- if (tags.indexOf(tag) !== -1) {
+ return validator(this.term).then(tagSets => {
+ for (const tags of tagSets) {
+ if (this.tags.length === 0) {
return true;
}
+
+ for (const tag of this.tags) {
+ if (tags.indexOf(tag) !== -1) {
+ return true;
+ }
+ }
}
- }
- return false;
+ return false;
+ });
}
deinflect(validator, rules) {
- if (this.validate(validator)) {
- const child = new Deinflection(this.term, this.tags);
- this.children.push(child);
- }
+ const promises = [
+ this.validate(validator).then(valid => {
+ const child = new Deinflection(this.term, this.tags);
+ this.children.push(child);
+ })
+ ];
for (const rule in rules) {
for (const variant of rules[rule]) {
@@ -63,13 +67,19 @@ class Deinflection {
const term = this.term.slice(0, -variant.ki.length) + variant.ko;
const child = new Deinflection(term, variant.to, rule);
- if (child.deinflect(validator, rules)) {
- this.children.push(child);
- }
+ promises.push(
+ child.deinflect(validator, rules).then(valid => {
+ if (valid) {
+ this.children.push(child);
+ }
+ }
+ ));
}
}
- return this.children.length > 0;
+ return Promise.all(promises).then(() => {
+ return this.children.length > 0;
+ });
}
gather() {
@@ -105,10 +115,8 @@ class Deinflector {
deinflect(term, validator) {
const node = new Deinflection(term);
- if (node.deinflect(validator, this.rules)) {
- return node.gather();
- }
-
- return null;
+ return node.deinflect(validator, this.rules).then(success => {
+ return success ? node.gather() : null;
+ });
}
}
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 5768c3d6..8938162a 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -59,12 +59,12 @@ class Translator {
for (let i = text.length; i > 0; --i) {
const term = text.slice(0, i);
const dfs = this.deinflector.deinflect(term, t => {
- const tags = [];
+ const tagSets = [];
for (const d of this.dictionary.findTerm(t)) {
- tags.push(d.tags);
+ tagSets.push(d.tags);
}
- return tags;
+ return tagSets;
});
if (dfs === null) {
@@ -114,7 +114,7 @@ class Translator {
length = Math.max(length, result.source.length);
}
- return {definitions: definitions, length: length};
+ return {definitions, length};
}
findKanji(text) {