summaryrefslogtreecommitdiff
path: root/ext/bg/js/translator.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-08-17 19:19:34 -0700
committerAlex Yatskov <alex@foosoft.net>2017-08-17 19:19:34 -0700
commit7586572fbaab7de698ec13f8712cc95e24ab6273 (patch)
tree665417b73c928694b96c00a98dd882c618e1fd1d /ext/bg/js/translator.js
parent3475150b2d1424d43f5be6fcfbdbb719a576866f (diff)
parent191336522c220b0a3cfe41515ed23946b3462217 (diff)
Merge branch 'dev'
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r--ext/bg/js/translator.js163
1 files changed, 67 insertions, 96 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 8d65a0cd..1be485c7 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -19,135 +19,106 @@
class Translator {
constructor() {
- this.loaded = false;
- this.ruleMeta = null;
- this.database = new Database();
- this.deinflector = new Deinflector();
+ this.database = null;
+ this.deinflector = null;
}
- prepare() {
- if (this.loaded) {
- return Promise.resolve();
+ async prepare() {
+ if (!this.database) {
+ this.database = new Database();
+ await this.database.prepare();
}
- const promises = [
- jsonLoadInt('/bg/lang/deinflect.json'),
- this.database.prepare()
- ];
-
- return Promise.all(promises).then(([reasons]) => {
- this.deinflector.setReasons(reasons);
- this.loaded = true;
- });
+ if (!this.deinflector) {
+ const url = chrome.extension.getURL('/bg/lang/deinflect.json');
+ const reasons = await requestJson(url, 'GET');
+ this.deinflector = new Deinflector(reasons);
+ }
}
- 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 = wanakana._katakanaToHiragana(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, titles, cache) {
+ 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, titles);
}
-
- 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) {
+ 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);
}