summaryrefslogtreecommitdiff
path: root/ext/bg/js/translator.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-12-19 21:23:17 -0800
committerAlex Yatskov <alex@foosoft.net>2016-12-19 21:23:17 -0800
commit28b8bae6a7cbea7b34faed2ea396b95b5de3b426 (patch)
tree9405c939b046ddb40513ea11206c306150b6e47d /ext/bg/js/translator.js
parent0aa603694c6ab912b4e74c9e87b73295ae393ee2 (diff)
deinflector optimizations
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r--ext/bg/js/translator.js75
1 files changed, 24 insertions, 51 deletions
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) {