yomichan/ext/bg/js/translator.js

128 lines
4.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() {
2017-07-10 21:30:34 +00:00
this.database = null;
this.deinflector = null;
2016-03-26 21:16:21 +00:00
}
2017-07-10 21:10:58 +00:00
async prepare() {
2017-07-10 21:30:34 +00:00
if (!this.database) {
this.database = new Database();
2017-07-10 21:10:58 +00:00
await this.database.prepare();
2017-07-10 21:30:34 +00:00
}
if (!this.deinflector) {
2017-07-10 22:00:38 +00:00
const url = chrome.extension.getURL('/bg/lang/deinflect.json');
2017-07-10 23:24:31 +00:00
const reasons = await jsonRequest(url, 'GET');
2017-07-10 21:30:34 +00:00
this.deinflector = new Deinflector(reasons);
2017-07-10 21:10:58 +00:00
}
2016-03-20 17:52:27 +00:00
}
2016-03-21 01:45:37 +00:00
2017-07-10 21:10:58 +00:00
async findTermsGrouped(text, dictionaries, alphanumeric) {
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
return {length, definitions: dictTermsGroup(definitions, dictionaries)};
}
2017-07-10 21:10:58 +00:00
async findTerms(text, dictionaries, alphanumeric) {
if (!alphanumeric && text.length > 0) {
const c = text[0];
if (!jpIsKana(c) && !jpIsKanji(c)) {
2017-07-10 21:10:58 +00:00
return {length: 0, definitions: []};
}
}
2017-07-10 21:10:58 +00:00
const cache = {};
const titles = Object.keys(dictionaries);
2017-07-10 21:10:58 +00:00
let deinflections = await this.findTermsDeinflected(text, titles, cache);
const textHiragana = jpKatakanaToHiragana(text);
if (text !== textHiragana) {
deinflections = deinflections.concat(await this.findTermsDeinflected(textHiragana, titles, cache));
2016-03-21 03:34:50 +00:00
}
2017-07-10 21:10:58 +00:00
let definitions = [];
for (const deinflection of deinflections) {
for (const definition of deinflection.definitions) {
2017-03-03 05:01:49 +00:00
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
tags.push(dictTagBuildSource(definition.dictionary));
2017-07-10 21:10:58 +00: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,
tags: dictTagsSort(tags)
});
2017-01-08 19:18:55 +00:00
}
2017-07-10 21:10:58 +00:00
}
definitions = dictTermsUndupe(definitions);
definitions = dictTermsSort(definitions, dictionaries);
2017-01-08 19:18:55 +00:00
2017-07-10 21:10:58 +00:00
let length = 0;
for (const definition of definitions) {
length = Math.max(length, definition.source.length);
}
return {length, definitions};
2016-03-21 01:45:37 +00:00
}
2017-07-10 22:20:07 +00:00
async findTermsDeinflected(text, titles, cache) {
2017-07-10 21:10:58 +00:00
const definer = async term => {
2016-12-21 06:43:27 +00:00
if (cache.hasOwnProperty(term)) {
2017-07-10 21:10:58 +00:00
return cache[term];
} else {
2017-07-10 22:20:07 +00:00
return cache[term] = await this.database.findTerms(term, titles);
2016-12-21 06:43:27 +00:00
}
};
2016-11-13 03:29:30 +00:00
2017-07-10 21:10:58 +00:00
let deinflections = [];
2016-11-13 03:29:30 +00:00
for (let i = text.length; i > 0; --i) {
2017-07-10 21:10:58 +00:00
const textSlice = text.slice(0, i);
deinflections = deinflections.concat(await this.deinflector.deinflect(textSlice, definer));
2016-11-13 03:29:30 +00:00
}
2017-07-10 21:10:58 +00:00
return deinflections;
2016-03-21 03:34:50 +00:00
}
2017-07-10 21:10:58 +00:00
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;
}
}
2017-02-26 19:12:54 +00:00
for (const definition of definitions) {
2017-03-03 05:01:49 +00:00
const tags = definition.tags.map(tag => dictTagBuild(tag, definition.tagMeta));
2017-07-10 21:10:58 +00:00
tags.push(dictTagBuildSource(definition.dictionary));
2017-03-03 05:01:49 +00:00
definition.tags = dictTagsSort(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
}