aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-09-10 19:36:28 -0700
committerAlex Yatskov <alex@foosoft.net>2016-09-10 19:36:28 -0700
commitfa391bd5d366ec06d816905abde6ca0a1d4d7791 (patch)
tree5eb0fb6ab2422ecc53881dc5638ea35b4b0b5867 /ext
parent05ac93128539cf4be5417a50728329c3ae070580 (diff)
WIP
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/translator.js176
1 files changed, 89 insertions, 87 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 8938162a..dcc2e5ac 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -54,67 +54,67 @@ class Translator {
});
}
- findTerm(text) {
+ findTermGroups(text) {
const groups = {};
+ const promises = [];
+
for (let i = text.length; i > 0; --i) {
- const term = text.slice(0, i);
- const dfs = this.deinflector.deinflect(term, t => {
- const tagSets = [];
- for (const d of this.dictionary.findTerm(t)) {
- tagSets.push(d.tags);
- }
+ promises.append(
+ this.deinflector.deinflect(text.slice(0, i), term => {
+ return this.dictionary.findTerm(term).then(definitions => definitions.map(def => def.tags));
+ }).then(inflects => {
+ for (const inflect of inflects || []) {
+ this.processTerm(groups, df.source, df.tags, df.rules, df.root);
+ }
+ })
+ );
+ }
- return tagSets;
- });
+ return Promise.all(promises).then(Promise.resolve(groups));
+ }
- if (dfs === null) {
- continue;
+ findTerm(text) {
+ return this.findTermGroups(text).then(groups => {
+ let definitions = [];
+ for (const key in groups) {
+ definitions.push(groups[key]);
}
- for (const df of dfs) {
- this.processTerm(groups, df.source, df.tags, df.rules, df.root);
- }
- }
+ definitions = definitions.sort((v1, v2) => {
+ const sl1 = v1.source.length;
+ const sl2 = v2.source.length;
+ if (sl1 > sl2) {
+ return -1;
+ } else if (sl1 < sl2) {
+ return 1;
+ }
- let definitions = [];
- for (const key in groups) {
- definitions.push(groups[key]);
- }
+ const s1 = v1.score;
+ const s2 = v2.score;
+ if (s1 > s2) {
+ return -1;
+ } else if (s1 < s2) {
+ return 1;
+ }
- definitions = definitions.sort((v1, v2) => {
- const sl1 = v1.source.length;
- const sl2 = v2.source.length;
- if (sl1 > sl2) {
- return -1;
- } else if (sl1 < sl2) {
- return 1;
- }
+ const rl1 = v1.rules.length;
+ const rl2 = v2.rules.length;
+ if (rl1 < rl2) {
+ return -1;
+ } else if (rl1 > rl2) {
+ return 1;
+ }
- const s1 = v1.score;
- const s2 = v2.score;
- if (s1 > s2) {
- return -1;
- } else if (s1 < s2) {
- return 1;
- }
+ return v2.expression.localeCompare(v1.expression);
+ });
- const rl1 = v1.rules.length;
- const rl2 = v2.rules.length;
- if (rl1 < rl2) {
- return -1;
- } else if (rl1 > rl2) {
- return 1;
+ let length = 0;
+ for (const result of definitions) {
+ length = Math.max(length, result.source.length);
}
- return v2.expression.localeCompare(v1.expression);
+ return {definitions, length};
});
-
- let length = 0;
- for (const result of definitions) {
- length = Math.max(length, result.source.length);
- }
-
- return {definitions, length};
}
findKanji(text) {
@@ -132,52 +132,54 @@ class Translator {
}
processTerm(groups, source, tags, rules, root) {
- for (const entry of this.dictionary.findTerm(root)) {
- if (entry.id in groups) {
- continue;
- }
+ return this.dictionary.findTerm(root).then(definitions => {
+ for (const definition of definitions) {
+ if (definition.id in groups) {
+ continue;
+ }
- let matched = tags.length === 0;
- for (const tag of tags) {
- if (entry.tags.indexOf(tag) !== -1) {
- matched = true;
- break;
+ let matched = tags.length === 0;
+ for (const tag of tags) {
+ if (definition.tags.indexOf(tag) !== -1) {
+ matched = true;
+ break;
+ }
}
- }
- if (!matched) {
- continue;
- }
+ if (!matched) {
+ continue;
+ }
- const tagItems = [];
- for (const tag of entry.tags) {
- const tagItem = {
- name: tag,
- class: 'default',
- order: Number.MAX_SAFE_INTEGER,
- score: 0,
- desc: entry.entities[tag] || '',
- };
+ const tagItems = [];
+ for (const tag of definition.tags) {
+ const tagItem = {
+ name: tag,
+ class: 'default',
+ order: Number.MAX_SAFE_INTEGER,
+ score: 0,
+ desc: definition.entities[tag] || '',
+ };
+
+ this.applyTagMeta(tagItem);
+ tagItems.push(tagItem);
+ }
- this.applyTagMeta(tagItem);
- tagItems.push(tagItem);
- }
+ let score = 0;
+ for (const tagItem of tagItems) {
+ score += tagItem.score;
+ }
- let score = 0;
- for (const tagItem of tagItems) {
- score += tagItem.score;
+ groups[definition.id] = {
+ score,
+ source,
+ rules,
+ expression: definition.expression,
+ reading: definition.reading,
+ glossary: definition.glossary,
+ tags: Translator.sortTags(tagItems)
+ };
}
-
- groups[entry.id] = {
- score,
- source,
- rules,
- expression: entry.expression,
- reading: entry.reading,
- glossary: entry.glossary,
- tags: Translator.sortTags(tagItems)
- };
- }
+ });
}
processKanji(entries) {