yomichan/ext/bg/js/translator.js

157 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-03-20 10:52:27 -07: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-23 19:25:32 -07:00
2016-03-20 10:52:27 -07:00
class Translator {
2016-03-26 14:16:21 -07:00
constructor() {
2016-08-07 13:01:27 -07:00
this.loaded = false;
2016-12-17 18:45:19 -08:00
this.ruleMeta = null;
2016-11-07 08:29:21 -08:00
this.database = new Database();
2016-04-02 12:56:47 -07:00
this.deinflector = new Deinflector();
2016-03-26 14:16:21 -07:00
}
2016-11-13 11:58:23 -08:00
prepare() {
2016-04-03 22:22:38 -07:00
if (this.loaded) {
2016-09-11 12:40:45 -07:00
return Promise.resolve();
2016-03-26 14:16:21 -07:00
}
2016-03-20 10:52:27 -07:00
2016-11-13 11:58:23 -08:00
const promises = [
2017-03-04 19:53:28 -08:00
jsonLoadInt('/bg/lang/deinflect.json'),
2016-11-13 11:58:23 -08:00
this.database.prepare()
];
2016-12-17 18:45:19 -08:00
return Promise.all(promises).then(([reasons]) => {
this.deinflector.setReasons(reasons);
2016-08-23 20:53:11 -07:00
this.loaded = true;
2016-08-21 13:32:36 -07:00
});
2016-03-20 10:52:27 -07:00
}
2016-03-20 18:45:37 -07:00
2017-05-24 20:29:50 -07:00
findTerms(text, dictionaries, alphanumeric) {
const titles = Object.keys(dictionaries);
2016-12-20 22:43:27 -08:00
const cache = {};
if (!alphanumeric && text.length > 0) {
const c = text[0];
if (!jpIsKana(c) && !jpIsKanji(c)) {
return Promise.resolve({length: 0, definitions: []});
}
}
return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => {
const textHiragana = wanakana._katakanaToHiragana(text);
2017-05-24 20:29:50 -07:00
if (text === textHiragana) {
return deinfLiteral;
2017-05-24 20:29:50 -07:00
} else {
return this.findTermsDeinflected(textHiragana, titles, cache).then(deinfHiragana => deinfLiteral.concat(deinfHiragana));
}
2016-12-23 15:44:16 -08:00
}).then(deinflections => {
let definitions = [];
2017-02-26 11:12:54 -08:00
for (const deinflection of deinflections) {
for (const definition of deinflection.definitions) {
2017-03-02 21:01:49 -08:00
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
tags.push(dictTagBuildSource(definition.dictionary));
2016-12-23 15:44:16 -08:00
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,
2017-03-02 21:01:49 -08:00
tags: dictTagsSort(tags)
2016-12-23 15:44:16 -08:00
});
}
2016-05-07 18:24:31 -07:00
}
2017-03-02 21:01:49 -08:00
definitions = dictTermsUndupe(definitions);
definitions = dictTermsSort(definitions, dictionaries);
2016-12-23 15:44:16 -08:00
2016-09-10 19:36:28 -07:00
let length = 0;
2017-02-26 11:12:54 -08:00
for (const definition of definitions) {
2016-12-23 15:44:16 -08:00
length = Math.max(length, definition.source.length);
2016-04-01 22:13:37 -07:00
}
2016-12-23 15:44:16 -08:00
return {length, definitions};
2016-04-01 22:13:37 -07:00
});
2016-03-20 18:45:37 -07:00
}
2017-05-24 20:29:50 -07:00
findTermsGrouped(text, dictionaries, alphanumeric) {
return this.findTerms(text, dictionaries, alphanumeric).then(({length, definitions}) => {
2017-03-02 21:01:49 -08:00
return {length, definitions: dictTermsGroup(definitions, dictionaries)};
2017-01-08 15:33:45 -08:00
});
}
2016-11-12 19:29:30 -08:00
findKanji(text, dictionaries) {
const titles = Object.keys(dictionaries);
const processed = {};
const promises = [];
2017-02-26 11:12:54 -08:00
for (const c of text) {
2016-04-24 18:59:29 -07:00
if (!processed[c]) {
promises.push(this.database.findKanji(c, titles));
2016-03-20 20:34:50 -07:00
processed[c] = true;
}
}
2017-01-08 11:18:55 -08:00
return Promise.all(promises).then(defSets => {
const definitions = defSets.reduce((a, b) => a.concat(b), []);
2017-02-26 11:12:54 -08:00
for (const definition of definitions) {
2017-03-02 21:01:49 -08:00
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
tags.push(dictTagBuildSource(definition.dictionary));
definition.tags = dictTagsSort(tags);
2017-01-08 11:18:55 -08:00
}
return definitions;
});
2016-03-20 18:45:37 -07:00
}
2017-01-15 22:17:49 -08:00
findTermsDeinflected(text, dictionaries, cache) {
2016-12-20 22:43:27 -08:00
const definer = term => {
if (cache.hasOwnProperty(term)) {
return Promise.resolve(cache[term]);
}
2017-01-15 22:17:49 -08:00
return this.database.findTerms(term, dictionaries).then(definitions => cache[term] = definitions);
2016-12-20 22:43:27 -08:00
};
2016-11-12 19:29:30 -08:00
2016-12-23 15:44:16 -08:00
const promises = [];
2016-11-12 19:29:30 -08:00
for (let i = text.length; i > 0; --i) {
2016-12-23 15:44:16 -08:00
promises.push(this.deinflector.deinflect(text.slice(0, i), definer));
2016-11-12 19:29:30 -08:00
}
2016-12-23 15:44:16 -08:00
return Promise.all(promises).then(results => {
let deinflections = [];
2017-02-26 11:12:54 -08:00
for (const result of results) {
2016-12-23 15:44:16 -08:00
deinflections = deinflections.concat(result);
2016-04-18 19:38:03 -07:00
}
2016-12-19 21:23:17 -08:00
2016-12-23 15:44:16 -08:00
return deinflections;
});
2016-03-20 20:34:50 -07:00
}
2016-09-11 12:40:45 -07:00
processKanji(definitions) {
2017-02-26 11:12:54 -08:00
for (const definition of definitions) {
2017-03-02 21:01:49 -08:00
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
definition.tags = dictTagsSort(tags);
2016-08-07 19:56:50 -07:00
}
2016-09-11 12:40:45 -07:00
return definitions;
2016-08-07 22:42:56 -07:00
}
2016-03-20 10:52:27 -07:00
}