2016-03-20 17:52:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-03-24 02:25:32 +00:00
|
|
|
|
2016-03-20 17:52:27 +00:00
|
|
|
class Translator {
|
2016-03-26 21:16:21 +00:00
|
|
|
constructor() {
|
2016-08-07 20:01:27 +00:00
|
|
|
this.loaded = false;
|
|
|
|
this.tagMeta = null;
|
|
|
|
this.dictionary = new Dictionary();
|
2016-04-02 19:56:47 +00:00
|
|
|
this.deinflector = new Deinflector();
|
2016-03-26 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 03:36:13 +00:00
|
|
|
loadData(callback) {
|
2016-04-04 05:22:38 +00:00
|
|
|
if (this.loaded) {
|
2016-09-11 19:40:45 +00:00
|
|
|
return Promise.resolve();
|
2016-03-26 21:16:21 +00:00
|
|
|
}
|
2016-03-20 17:52:27 +00:00
|
|
|
|
2016-09-11 19:40:45 +00:00
|
|
|
return loadJson('bg/data/rules.json').then(rules => {
|
2016-08-24 03:53:11 +00:00
|
|
|
this.deinflector.setRules(rules);
|
2016-08-24 05:28:37 +00:00
|
|
|
return loadJson('bg/data/tags.json');
|
2016-08-30 03:27:04 +00:00
|
|
|
}).then(tagMeta => {
|
2016-08-24 03:53:11 +00:00
|
|
|
this.tagMeta = tagMeta;
|
2016-09-13 22:59:18 +00:00
|
|
|
return this.dictionary.prepareDb();
|
2016-08-30 03:27:04 +00:00
|
|
|
}).then(exists => {
|
2016-09-14 03:36:13 +00:00
|
|
|
if (exists) {
|
|
|
|
return;
|
2016-08-24 03:33:04 +00:00
|
|
|
}
|
2016-09-14 03:36:13 +00:00
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback({state: 'begin', progress: 0});
|
|
|
|
}
|
|
|
|
|
2016-09-14 05:43:16 +00:00
|
|
|
const banks = {};
|
2016-09-15 03:08:49 +00:00
|
|
|
const bankCallback = (loaded, total, indexUrl) => {
|
|
|
|
banks[indexUrl] = {loaded, total};
|
2016-09-14 03:36:13 +00:00
|
|
|
|
2016-09-15 03:08:49 +00:00
|
|
|
let percent = 0.0;
|
2016-09-14 05:43:16 +00:00
|
|
|
for (const url in banks) {
|
2016-09-15 03:08:49 +00:00
|
|
|
percent += banks[url].loaded / banks[url].total;
|
2016-09-14 05:43:16 +00:00
|
|
|
}
|
2016-09-14 03:36:13 +00:00
|
|
|
|
2016-09-16 04:03:58 +00:00
|
|
|
percent /= 3.0;
|
2016-09-15 03:08:49 +00:00
|
|
|
|
|
|
|
if (callback) {
|
2016-09-16 04:03:58 +00:00
|
|
|
callback({state: 'update', progress: Math.ceil(100.0 * percent)});
|
2016-09-14 03:36:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
this.dictionary.importTermDict('bg/data/edict/index.json', bankCallback),
|
|
|
|
this.dictionary.importTermDict('bg/data/enamdict/index.json', bankCallback),
|
|
|
|
this.dictionary.importKanjiDict('bg/data/kanjidic/index.json', bankCallback),
|
|
|
|
]).then(() => {
|
|
|
|
return this.dictionary.sealDb();
|
|
|
|
}).then(() => {
|
|
|
|
if (callback) {
|
2016-09-16 04:03:58 +00:00
|
|
|
callback({state: 'end', progress: 100.0});
|
2016-09-14 03:36:13 +00:00
|
|
|
}
|
|
|
|
});
|
2016-08-21 20:32:36 +00:00
|
|
|
}).then(() => {
|
2016-08-24 03:53:11 +00:00
|
|
|
this.loaded = true;
|
2016-08-21 20:32:36 +00:00
|
|
|
});
|
2016-03-20 17:52:27 +00:00
|
|
|
}
|
2016-03-21 01:45:37 +00:00
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
findTermGroups(text) {
|
2016-09-11 19:29:18 +00:00
|
|
|
const deinflectGroups = {};
|
2016-09-11 05:35:31 +00:00
|
|
|
const deinflectPromises = [];
|
2016-09-11 19:29:18 +00:00
|
|
|
|
2016-03-25 03:20:46 +00:00
|
|
|
for (let i = text.length; i > 0; --i) {
|
2016-09-11 05:35:31 +00:00
|
|
|
deinflectPromises.push(
|
2016-09-11 02:36:28 +00:00
|
|
|
this.deinflector.deinflect(text.slice(0, i), term => {
|
2016-09-11 05:35:31 +00:00
|
|
|
return this.dictionary.findTerm(term).then(definitions => definitions.map(definition => definition.tags));
|
2016-09-11 19:29:18 +00:00
|
|
|
}).then(deinflects => {
|
2016-09-11 05:35:31 +00:00
|
|
|
const processPromises = [];
|
2016-09-11 19:29:18 +00:00
|
|
|
for (const deinflect of deinflects) {
|
|
|
|
processPromises.push(this.processTerm(
|
|
|
|
deinflectGroups,
|
|
|
|
deinflect.source,
|
|
|
|
deinflect.tags,
|
|
|
|
deinflect.rules,
|
|
|
|
deinflect.root
|
|
|
|
));
|
2016-09-11 02:36:28 +00:00
|
|
|
}
|
2016-09-11 05:35:31 +00:00
|
|
|
|
|
|
|
return Promise.all(processPromises);
|
2016-09-11 02:36:28 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2016-03-23 03:17:11 +00:00
|
|
|
|
2016-09-11 19:29:18 +00:00
|
|
|
return Promise.all(deinflectPromises).then(() => deinflectGroups);
|
2016-09-11 02:36:28 +00:00
|
|
|
}
|
2016-03-23 03:17:11 +00:00
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
findTerm(text) {
|
2016-09-11 19:29:18 +00:00
|
|
|
return this.findTermGroups(text).then(deinflectGroups => {
|
2016-09-20 03:17:58 +00:00
|
|
|
const definitions = [];
|
2016-09-11 19:29:18 +00:00
|
|
|
for (const key in deinflectGroups) {
|
|
|
|
definitions.push(deinflectGroups[key]);
|
2016-05-08 01:24:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
let length = 0;
|
|
|
|
for (const result of definitions) {
|
|
|
|
length = Math.max(length, result.source.length);
|
2016-04-02 05:13:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:17:58 +00:00
|
|
|
return {
|
|
|
|
length,
|
|
|
|
definitions: sortTermDefs(definitions)
|
|
|
|
};
|
2016-04-02 05:13:37 +00:00
|
|
|
});
|
2016-03-21 01:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 01:38:29 +00:00
|
|
|
findKanji(text) {
|
2016-03-21 03:34:50 +00:00
|
|
|
const processed = {};
|
2016-09-11 19:29:18 +00:00
|
|
|
const promises = [];
|
2016-04-25 01:59:29 +00:00
|
|
|
|
2016-08-10 04:23:05 +00:00
|
|
|
for (const c of text) {
|
2016-04-25 01:59:29 +00:00
|
|
|
if (!processed[c]) {
|
2016-09-11 19:29:18 +00:00
|
|
|
promises.push(this.dictionary.findKanji(c).then((definitions) => definitions));
|
2016-03-21 03:34:50 +00:00
|
|
|
processed[c] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 00:39:21 +00:00
|
|
|
return Promise.all(promises).then(sets => this.processKanji(sets.reduce((a, b) => a.concat(b), [])));
|
2016-03-21 01:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 20:42:12 +00:00
|
|
|
processTerm(groups, source, tags, rules, root) {
|
2016-09-11 02:36:28 +00:00
|
|
|
return this.dictionary.findTerm(root).then(definitions => {
|
|
|
|
for (const definition of definitions) {
|
|
|
|
if (definition.id in groups) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-26 03:54:04 +00:00
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
let matched = tags.length === 0;
|
|
|
|
for (const tag of tags) {
|
2016-09-11 19:29:18 +00:00
|
|
|
if (definition.tags.includes(tag)) {
|
2016-09-11 02:36:28 +00:00
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-17 03:11:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
if (!matched) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-08 05:42:56 +00:00
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
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] || '',
|
|
|
|
};
|
|
|
|
|
2016-09-20 03:17:58 +00:00
|
|
|
applyTagMeta(tagItem, this.tagMeta);
|
2016-09-11 02:36:28 +00:00
|
|
|
tagItems.push(tagItem);
|
|
|
|
}
|
2016-08-07 06:04:03 +00:00
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
let score = 0;
|
|
|
|
for (const tagItem of tagItems) {
|
|
|
|
score += tagItem.score;
|
|
|
|
}
|
2016-08-07 06:04:03 +00:00
|
|
|
|
2016-09-11 02:36:28 +00:00
|
|
|
groups[definition.id] = {
|
|
|
|
score,
|
|
|
|
source,
|
|
|
|
rules,
|
|
|
|
expression: definition.expression,
|
|
|
|
reading: definition.reading,
|
|
|
|
glossary: definition.glossary,
|
2016-09-20 03:17:58 +00:00
|
|
|
tags: sortTags(tagItems)
|
2016-09-11 02:36:28 +00:00
|
|
|
};
|
2016-04-19 02:38:03 +00:00
|
|
|
}
|
2016-09-11 02:36:28 +00:00
|
|
|
});
|
2016-03-21 03:34:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 19:40:45 +00:00
|
|
|
processKanji(definitions) {
|
|
|
|
for (const definition of definitions) {
|
2016-08-20 20:42:12 +00:00
|
|
|
const tagItems = [];
|
2016-09-11 19:40:45 +00:00
|
|
|
for (const tag of definition.tags) {
|
2016-08-20 20:42:12 +00:00
|
|
|
const tagItem = {
|
|
|
|
name: tag,
|
2016-08-20 20:04:59 +00:00
|
|
|
class: 'default',
|
|
|
|
order: Number.MAX_SAFE_INTEGER,
|
|
|
|
desc: '',
|
|
|
|
};
|
|
|
|
|
2016-09-20 03:17:58 +00:00
|
|
|
applyTagMeta(tagItem, this.tagMeta);
|
2016-08-20 20:42:12 +00:00
|
|
|
tagItems.push(tagItem);
|
2016-08-20 20:04:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 03:17:58 +00:00
|
|
|
definition.tags = sortTags(tagItems);
|
2016-08-08 02:56:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 19:40:45 +00:00
|
|
|
return definitions;
|
2016-08-08 05:42:56 +00:00
|
|
|
}
|
2016-03-20 17:52:27 +00:00
|
|
|
}
|