yomichan/ext/bg/js/translator.js

164 lines
5.4 KiB
JavaScript
Raw Normal View History

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;
2016-12-18 02:45:19 +00:00
this.ruleMeta = null;
2016-11-07 16:29:21 +00:00
this.database = new Database();
2016-04-02 19:56:47 +00:00
this.deinflector = new Deinflector();
2016-03-26 21:16:21 +00:00
}
2016-11-13 19:58:23 +00:00
prepare() {
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-11-13 19:58:23 +00:00
const promises = [
2016-12-18 03:08:28 +00:00
loadJsonInt('bg/data/deinflect.json'),
2016-11-13 19:58:23 +00:00
this.database.prepare()
];
2016-12-18 02:45:19 +00:00
return Promise.all(promises).then(([reasons]) => {
this.deinflector.setReasons(reasons);
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-11-14 05:53:56 +00:00
findTerm(text, dictionaries, enableSoftKatakanaSearch) {
2016-12-18 05:42:53 +00:00
return this.findDeinflectionGroups(text, dictionaries).then(groups => {
const textHiragana = wanakana._katakanaToHiragana(text);
if (text !== textHiragana && enableSoftKatakanaSearch) {
2016-12-18 05:42:53 +00:00
return this.findDeinflectionGroups(textHiragana, dictionaries).then(groupsHiragana => {
for (const key in groupsHiragana) {
groups[key] = groups[key] || groupsHiragana[key];
}
return groups;
});
} else {
return groups;
}
}).then(groups => {
2016-09-20 03:17:58 +00:00
const definitions = [];
for (const key in groups) {
definitions.push(groups[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-11-13 03:29:30 +00:00
findKanji(text, dictionaries) {
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-11-13 03:29:30 +00:00
promises.push(this.database.findKanji(c, dictionaries));
2016-03-21 03:34:50 +00:00
processed[c] = true;
}
}
return Promise.all(promises).then(sets => this.processKanji(sets.reduce((a, b) => a.concat(b), [])));
2016-03-21 01:45:37 +00:00
}
2016-12-18 05:42:53 +00:00
findDeinflectionGroups(text, dictionaries) {
const deinflectionGroups = {};
const deinflectionPromises = [];
2016-11-13 03:29:30 +00:00
for (let i = text.length; i > 0; --i) {
2016-12-18 05:42:53 +00:00
deinflectionPromises.push(
2016-11-13 03:29:30 +00:00
this.deinflector.deinflect(text.slice(0, i), term => {
2016-12-18 02:45:19 +00:00
return this.database.findTerm(term, dictionaries).then(definitions => definitions.map(definition => definition.rules));
2016-12-18 05:42:53 +00:00
}).then(deinflections => {
2016-11-13 03:29:30 +00:00
const processPromises = [];
2016-12-18 05:42:53 +00:00
for (const deinflection of deinflections) {
2016-12-18 02:45:19 +00:00
processPromises.push(
this.processDeinflection(
2016-12-18 05:42:53 +00:00
deinflectionGroups,
deinflection.source,
deinflection.rules,
deinflection.reasons,
deinflection.root,
2016-12-18 02:45:19 +00:00
dictionaries
)
);
2016-11-13 03:29:30 +00:00
}
return Promise.all(processPromises);
})
);
}
2016-12-18 05:42:53 +00:00
return Promise.all(deinflectionPromises).then(() => deinflectionGroups);
2016-11-13 03:29:30 +00:00
}
2016-12-18 02:45:19 +00:00
processDeinflection(groups, source, rules, reasons, root, dictionaries) {
2016-11-13 03:29:30 +00:00
return this.database.findTerm(root, dictionaries).then(definitions => {
2016-09-11 02:36:28 +00:00
for (const definition of definitions) {
if (definition.id in groups) {
continue;
}
2016-03-26 03:54:04 +00:00
2016-12-18 02:45:19 +00:00
let matched = rules.length === 0;
for (const rule of rules) {
if (definition.rules.includes(rule)) {
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-12-18 05:26:46 +00:00
const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
2016-09-11 02:36:28 +00:00
groups[definition.id] = {
source,
2016-12-18 02:45:19 +00:00
reasons,
score: definition.score,
2016-12-18 05:42:53 +00:00
dictionary: definition.dictionary,
2016-09-11 02:36:28 +00:00
expression: definition.expression,
reading: definition.reading,
glossary: definition.glossary,
2016-12-18 05:26:46 +00:00
tags: sortTags(tags)
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-12-18 07:38:14 +00:00
const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
2016-12-18 05:26:46 +00:00
definition.tags = sortTags(tags);
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
}