aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/translator.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-07-10 14:10:58 -0700
committerAlex Yatskov <alex@foosoft.net>2017-07-10 14:10:58 -0700
commitb6f3919ef63f969769fbf030e8fd8b14b0e1c214 (patch)
tree2aff726effa13594097945399e2f91b6674fdd83 /ext/bg/js/translator.js
parentf49a69c993425cce57aea16e19adab378f8cbba9 (diff)
move translator to async
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r--ext/bg/js/translator.js160
1 files changed, 66 insertions, 94 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index f1858247..35b49608 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -19,135 +19,107 @@
class Translator {
constructor() {
- this.loaded = false;
- this.ruleMeta = null;
this.database = new Database();
this.deinflector = new Deinflector();
+ this.loaded = false;
}
- prepare() {
- if (this.loaded) {
- return Promise.resolve();
- }
-
- const promises = [
- jsonLoadInt('/bg/lang/deinflect.json'),
- this.database.prepare()
- ];
-
- return Promise.all(promises).then(([reasons]) => {
+ async prepare() {
+ if (!this.loaded) {
+ const reasons = await jsonLoadInt('/bg/lang/deinflect.json');
this.deinflector.setReasons(reasons);
+ await this.database.prepare();
this.loaded = true;
- });
+ }
}
- findTerms(text, dictionaries, alphanumeric) {
- const titles = Object.keys(dictionaries);
- const cache = {};
+ async findTermsGrouped(text, dictionaries, alphanumeric) {
+ const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
+ return {length, definitions: dictTermsGroup(definitions, dictionaries)};
+ }
+ async findTerms(text, dictionaries, alphanumeric) {
if (!alphanumeric && text.length > 0) {
const c = text[0];
if (!jpIsKana(c) && !jpIsKanji(c)) {
- return Promise.resolve({length: 0, definitions: []});
+ return {length: 0, definitions: []};
}
}
- return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => {
- const textHiragana = jpKatakanaToHiragana(text);
- if (text === textHiragana) {
- return deinfLiteral;
- } else {
- return this.findTermsDeinflected(textHiragana, titles, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana));
- }
- }).then(deinflections => {
- let definitions = [];
- for (const deinflection of deinflections) {
- for (const definition of deinflection.definitions) {
- const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
- tags.push(dictTagBuildSource(definition.dictionary));
- definitions.push({
- source: deinflection.source,
- reasons: deinflection.reasons,
- score: definition.score,
- id: definition.id,
- dictionary: definition.dictionary,
- expression: definition.expression,
- reading: definition.reading,
- glossary: definition.glossary,
- tags: dictTagsSort(tags)
- });
- }
- }
-
- definitions = dictTermsUndupe(definitions);
- definitions = dictTermsSort(definitions, dictionaries);
-
- let length = 0;
- for (const definition of definitions) {
- length = Math.max(length, definition.source.length);
- }
-
- return {length, definitions};
- });
- }
-
- findTermsGrouped(text, dictionaries, alphanumeric) {
- return this.findTerms(text, dictionaries, alphanumeric).then(({length, definitions}) => {
- return {length, definitions: dictTermsGroup(definitions, dictionaries)};
- });
- }
-
- findKanji(text, dictionaries) {
+ const cache = {};
const titles = Object.keys(dictionaries);
- const processed = {};
- const promises = [];
-
- for (const c of text) {
- if (!processed[c]) {
- promises.push(this.database.findKanji(c, titles));
- processed[c] = true;
- }
+ let deinflections = await this.findTermsDeinflected(text, titles, cache);
+ const textHiragana = jpKatakanaToHiragana(text);
+ if (text !== textHiragana) {
+ deinflections = deinflections.concat(await this.findTermsDeinflected(textHiragana, titles, cache));
}
- return Promise.all(promises).then(defSets => {
- const definitions = defSets.reduce((a, b) => a.concat(b), []);
- for (const definition of definitions) {
+ let definitions = [];
+ for (const deinflection of deinflections) {
+ for (const definition of deinflection.definitions) {
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
tags.push(dictTagBuildSource(definition.dictionary));
- definition.tags = dictTagsSort(tags);
+ definitions.push({
+ source: deinflection.source,
+ reasons: deinflection.reasons,
+ score: definition.score,
+ id: definition.id,
+ dictionary: definition.dictionary,
+ expression: definition.expression,
+ reading: definition.reading,
+ glossary: definition.glossary,
+ tags: dictTagsSort(tags)
+ });
}
+ }
+
+ definitions = dictTermsUndupe(definitions);
+ definitions = dictTermsSort(definitions, dictionaries);
- return definitions;
- });
+ let length = 0;
+ for (const definition of definitions) {
+ length = Math.max(length, definition.source.length);
+ }
+
+ return {length, definitions};
}
- findTermsDeinflected(text, dictionaries, cache) {
- const definer = term => {
+ async findTermsDeinflected(text, dictionaries, cache) {
+ await this.prepare();
+
+ const definer = async term => {
if (cache.hasOwnProperty(term)) {
- return Promise.resolve(cache[term]);
+ return cache[term];
+ } else {
+ return cache[term] = await this.database.findTerms(term, dictionaries);
}
-
- return this.database.findTerms(term, dictionaries).then(definitions => cache[term] = definitions);
};
- const promises = [];
+ let deinflections = [];
for (let i = text.length; i > 0; --i) {
- promises.push(this.deinflector.deinflect(text.slice(0, i), definer));
+ const textSlice = text.slice(0, i);
+ deinflections = deinflections.concat(await this.deinflector.deinflect(textSlice, definer));
}
- return Promise.all(promises).then(results => {
- let deinflections = [];
- for (const result of results) {
- deinflections = deinflections.concat(result);
- }
-
- return deinflections;
- });
+ return deinflections;
}
- processKanji(definitions) {
+ async findKanji(text, dictionaries) {
+ await this.prepare();
+
+ let definitions = [];
+ const processed = {};
+ const titles = Object.keys(dictionaries);
+ for (const c of text) {
+ if (!processed[c]) {
+ definitions = definitions.concat(await this.database.findKanji(c, titles));
+ processed[c] = true;
+ }
+ }
+
for (const definition of definitions) {
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
+ tags.push(dictTagBuildSource(definition.dictionary));
definition.tags = dictTagsSort(tags);
}