yomichan/ext/bg/js/translator.js

251 lines
7.2 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;
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-05-08 01:24:31 +00:00
loadData({loadEnamDict=true}, callback) {
2016-04-04 05:22:38 +00:00
if (this.loaded) {
2016-03-26 21:16:21 +00:00
callback();
return;
}
2016-03-20 17:52:27 +00:00
2016-08-08 16:09:03 +00:00
Translator.loadData('bg/data/rules.json')
.then((response) => {
this.deinflector.setRules(JSON.parse(response));
return Translator.loadData('bg/data/tags.json');
})
.then((response) => {
this.tagMeta = JSON.parse(response);
return Translator.loadData('bg/data/edict.json');
})
.then((response) => {
this.dictionary.addTermDict('edict', JSON.parse(response));
return Translator.loadData('bg/data/kanjidic.json');
})
.then((response) => {
this.dictionary.addKanjiDict('kanjidic', JSON.parse(response));
return loadEnamDict ? Translator.loadData('bg/data/enamdict.json') : Promise.resolve(null);
})
.then((response) => {
if (response !== null) {
this.dictionary.addTermDict('enamdict', JSON.parse(response));
2016-04-02 05:13:37 +00:00
}
2016-03-20 17:52:27 +00:00
2016-08-08 16:09:03 +00:00
this.loaded = true;
callback();
2016-04-02 05:13:37 +00:00
});
2016-03-20 17:52:27 +00:00
}
2016-03-21 01:45:37 +00:00
findTerm(text) {
2016-03-21 03:34:50 +00:00
const groups = {};
for (let i = text.length; i > 0; --i) {
2016-03-21 01:45:37 +00:00
const term = text.slice(0, i);
2016-03-23 03:17:11 +00:00
const dfs = this.deinflector.deinflect(term, t => {
const tags = [];
for (let d of this.dictionary.findTerm(t)) {
2016-03-23 03:17:11 +00:00
tags.push(d.tags);
}
return tags;
});
2016-05-08 01:24:31 +00:00
if (dfs === null) {
continue;
}
for (let df of dfs) {
2016-05-08 01:24:31 +00:00
this.processTerm(groups, df.source, df.tags, df.rules, df.root);
2016-03-21 01:45:37 +00:00
}
2016-03-24 01:12:33 +00:00
}
2016-03-21 01:45:37 +00:00
2016-05-06 03:06:13 +00:00
let definitions = [];
for (let key in groups) {
2016-05-06 03:06:13 +00:00
definitions.push(groups[key]);
2016-03-24 01:12:33 +00:00
}
2016-03-21 01:45:37 +00:00
2016-05-06 03:06:13 +00:00
definitions = definitions.sort((v1, v2) => {
2016-04-02 05:13:37 +00:00
const sl1 = v1.source.length;
const sl2 = v2.source.length;
if (sl1 > sl2) {
return -1;
} else if (sl1 < sl2) {
return 1;
}
2016-04-19 03:35:40 +00:00
const p1 = v1.popular;
const p2 = v2.popular;
2016-04-02 05:13:37 +00:00
if (p1 && !p2) {
return -1;
} else if (!p1 && p2) {
return 1;
}
const rl1 = v1.rules.length;
const rl2 = v2.rules.length;
if (rl1 < rl2) {
return -1;
2016-05-09 21:33:28 +00:00
} else if (rl1 > rl2) {
2016-04-02 05:13:37 +00:00
return 1;
}
return v2.expression.localeCompare(v1.expression);
2016-04-02 05:13:37 +00:00
});
2016-03-21 01:45:37 +00:00
2016-03-24 01:12:33 +00:00
let length = 0;
for (let result of definitions) {
2016-03-24 01:12:33 +00:00
length = Math.max(length, result.source.length);
2016-03-21 03:34:50 +00:00
}
2016-03-24 01:12:33 +00:00
2016-05-06 03:06:13 +00:00
return {definitions: definitions, length: length};
2016-03-21 01:45:37 +00:00
}
findKanji(text) {
2016-05-08 01:24:31 +00:00
let definitions = [];
2016-03-21 03:34:50 +00:00
const processed = {};
2016-04-25 01:59:29 +00:00
for (let c of text) {
2016-04-25 01:59:29 +00:00
if (!processed[c]) {
2016-05-06 03:06:13 +00:00
definitions = definitions.concat(this.dictionary.findKanji(c));
2016-03-21 03:34:50 +00:00
processed[c] = true;
}
}
2016-08-08 02:56:50 +00:00
return this.processKanji(definitions);
2016-03-21 01:45:37 +00:00
}
2016-08-08 05:42:56 +00:00
processTerm(groups, dfSource, dfTags, dfRules=[], dfRoot='') {
for (let entry of this.dictionary.findTerm(dfRoot)) {
if (entry.id in groups) {
2016-03-26 03:54:04 +00:00
continue;
}
2016-08-08 05:42:56 +00:00
let matched = dfTags.length === 0;
for (let t of dfTags) {
if (entry.tags.indexOf(t) !== -1) {
2016-04-17 03:11:27 +00:00
matched = true;
break;
}
}
2016-08-08 05:42:56 +00:00
if (!matched) {
continue;
}
2016-08-07 20:01:27 +00:00
let popular = false;
2016-08-07 06:04:03 +00:00
2016-08-08 05:42:56 +00:00
let tags = [];
for (let t of entry.tags) {
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: entry.entities[t] || '', name: t};
this.applyTagMeta(tag);
tags.push(tag);
2016-08-07 06:04:03 +00:00
//
// TODO: Handle tagging as popular through data.
//
2016-08-08 05:42:56 +00:00
if (t === 'P') {
2016-04-19 03:35:40 +00:00
popular = true;
2016-04-19 02:38:03 +00:00
}
}
2016-08-08 05:42:56 +00:00
groups[entry.id] = {
expression: entry.expression,
reading: entry.reading,
glossary: entry.glossary,
tags: Translator.sortTags(tags),
source: dfSource,
rules: dfRules,
popular: popular
};
2016-03-21 03:34:50 +00:00
}
}
2016-08-08 02:56:50 +00:00
processKanji(entries) {
2016-08-08 05:42:56 +00:00
const processed = [];
2016-08-08 02:56:50 +00:00
2016-08-08 05:42:56 +00:00
for (let entry of entries) {
const tags = [];
for (let t of entry.tags) {
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t};
this.applyTagMeta(tag);
tags.push(tag);
2016-08-08 02:56:50 +00:00
}
2016-08-08 05:42:56 +00:00
processed.push({
2016-08-08 02:56:50 +00:00
character: entry.character,
kunyomi: entry.kunyomi,
onyomi: entry.onyomi,
2016-08-08 05:42:56 +00:00
tags: Translator.sortTags(tags),
2016-08-08 02:56:50 +00:00
glossary: entry.glossary
});
}
2016-08-08 05:42:56 +00:00
return processed;
}
applyTagMeta(tag) {
for (const prop in this.tagMeta[tag.name] || {}) {
tag[prop] = this.tagMeta[tag.name][prop] || tag[prop];
}
}
static sortTags(tags) {
return tags.sort((v1, v2) => {
const order1 = v1.order;
const order2 = v2.order;
if (order1 < order2) {
return -1;
} else if (order1 > order2) {
return 1;
}
const name1 = v1.name;
const name2 = v2.name;
if (name1 < name2) {
return -1;
} else if (name1 > name2) {
return 1;
}
return 0;
});
2016-08-08 02:56:50 +00:00
}
2016-04-18 02:06:57 +00:00
static isKanji(c) {
const code = c.charCodeAt(0);
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
}
2016-08-08 16:09:03 +00:00
static loadData(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.open('GET', chrome.extension.getURL(url), true);
xhr.send();
});
2016-04-02 05:13:37 +00:00
}
2016-03-20 17:52:27 +00:00
}