summaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/deinflector.js35
-rw-r--r--ext/bg/js/translator.js75
2 files changed, 45 insertions, 65 deletions
diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js
index 5cfb7f75..5ed191cd 100644
--- a/ext/bg/js/deinflector.js
+++ b/ext/bg/js/deinflector.js
@@ -23,24 +23,25 @@ class Deinflection {
this.term = term;
this.rules = rules;
this.reason = reason;
+ this.definitions = [];
}
- deinflect(validator, reasons, entry=false) {
+ deinflect(definer, reasons, entry=false) {
const validate = () => {
- if (entry) {
- return Promise.resolve(true);
- }
-
- return validator(this.term).then(sets => {
- for (const rules of sets) {
+ return definer(this.term).then(definitions => {
+ if (entry) {
+ this.definitions = definitions;
+ } else {
for (const rule of this.rules) {
- if (rules.includes(rule)) {
- return true;
+ for (const definition of definitions) {
+ if (definition.rules.includes(rule)) {
+ this.definitions.push(definition);
+ }
}
}
}
- return false;
+ return this.definitions.length > 0;
});
};
@@ -74,7 +75,7 @@ class Deinflection {
const child = new Deinflection(term, variant.rulesOut, reason);
promises.push(
- child.deinflect(validator, reasons).then(valid => {
+ child.deinflect(definer, reasons).then(valid => {
if (valid) {
this.children.push(child);
}
@@ -90,12 +91,18 @@ class Deinflection {
gather() {
if (this.children.length === 0) {
- return [{root: this.term, rules: this.rules, reasons: []}];
+ return [{
+ root: this.term,
+ rules: this.rules,
+ definitions: this.definitions,
+ reasons: []
+ }];
}
const paths = [];
for (const child of this.children) {
for (const path of child.gather()) {
+ path.definitions = path.definitions.concat(this.definitions);
if (this.reason.length > 0) {
path.reasons.push(this.reason);
}
@@ -119,8 +126,8 @@ class Deinflector {
this.reasons = reasons;
}
- deinflect(term, validator) {
+ deinflect(term, definer) {
const node = new Deinflection(term);
- return node.deinflect(validator, this.reasons, true).then(success => success ? node.gather() : []);
+ return node.deinflect(definer, this.reasons, true).then(success => success ? node.gather() : []);
}
}
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 7d19b5f9..472211e0 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -88,68 +88,41 @@ class Translator {
}
findDeinflectionGroups(text, dictionaries) {
- const deinflectionGroups = {};
- const deinflectionPromises = [];
+ const definer = term => this.database.findTerm(term, dictionaries);
+ const groups = {};
+ const promises = [];
for (let i = text.length; i > 0; --i) {
- deinflectionPromises.push(
- this.deinflector.deinflect(text.slice(0, i), term => {
- return this.database.findTerm(term, dictionaries).then(definitions => definitions.map(definition => definition.rules));
- }).then(deinflections => {
- const processPromises = [];
+ promises.push(
+ this.deinflector.deinflect(text.slice(0, i), definer).then(deinflections => {
for (const deinflection of deinflections) {
- processPromises.push(
- this.processDeinflection(
- deinflectionGroups,
- deinflection.source,
- deinflection.rules,
- deinflection.reasons,
- deinflection.root,
- dictionaries
- )
- );
+ this.processDeinflection(groups, deinflection);
}
-
- return Promise.all(processPromises);
})
);
}
- return Promise.all(deinflectionPromises).then(() => deinflectionGroups);
+ return Promise.all(promises).then(() => groups);
}
- processDeinflection(groups, source, rules, reasons, root, dictionaries) {
- return this.database.findTerm(root, dictionaries).then(definitions => {
- for (const definition of definitions) {
- if (definition.id in groups) {
- continue;
- }
-
- let matched = rules.length === 0;
- for (const rule of rules) {
- if (definition.rules.includes(rule)) {
- matched = true;
- break;
- }
- }
-
- if (!matched) {
- continue;
- }
-
- const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
- groups[definition.id] = {
- source,
- reasons,
- score: definition.score,
- dictionary: definition.dictionary,
- expression: definition.expression,
- reading: definition.reading,
- glossary: definition.glossary,
- tags: sortTags(tags)
- };
+ processDeinflection(groups, {source, rules, reasons, root, definitions}, dictionaries) {
+ for (const definition of definitions) {
+ if (definition.id in groups) {
+ continue;
}
- });
+
+ const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
+ groups[definition.id] = {
+ source,
+ reasons,
+ score: definition.score,
+ dictionary: definition.dictionary,
+ expression: definition.expression,
+ reading: definition.reading,
+ glossary: definition.glossary,
+ tags: sortTags(tags)
+ };
+ }
}
processKanji(definitions) {