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
|
|
|
|
2016-04-18 01:38:29 +00:00
|
|
|
findTerm(text) {
|
2016-03-21 03:34:50 +00:00
|
|
|
const groups = {};
|
2016-03-25 03:20:46 +00:00
|
|
|
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 = [];
|
2016-08-10 04:23:05 +00:00
|
|
|
for (const 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;
|
|
|
|
}
|
|
|
|
|
2016-08-10 04:23:05 +00:00
|
|
|
for (const 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 = [];
|
2016-08-10 04:23:05 +00:00
|
|
|
for (const 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-08-20 20:04:59 +00:00
|
|
|
const s1 = v1.score;
|
|
|
|
const s2 = v2.score;
|
|
|
|
if (s1 > s2) {
|
2016-04-02 05:13:37 +00:00
|
|
|
return -1;
|
2016-08-20 20:04:59 +00:00
|
|
|
} else if (s1 < s2) {
|
2016-04-02 05:13:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-18 05:01:36 +00:00
|
|
|
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;
|
2016-08-10 04:23:05 +00:00
|
|
|
for (const 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
|
|
|
}
|
|
|
|
|
2016-04-18 01:38:29 +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
|
|
|
|
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-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-20 20:42:12 +00:00
|
|
|
processTerm(groups, source, tags, rules, root) {
|
|
|
|
for (const entry of this.dictionary.findTerm(root)) {
|
2016-05-18 19:30:45 +00:00
|
|
|
if (entry.id in groups) {
|
2016-03-26 03:54:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-20 20:42:12 +00:00
|
|
|
let matched = tags.length === 0;
|
|
|
|
for (const tag of tags) {
|
|
|
|
if (entry.tags.indexOf(tag) !== -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-20 20:42:12 +00:00
|
|
|
const tagItems = [];
|
|
|
|
for (const tag of entry.tags) {
|
|
|
|
const tagItem = {
|
|
|
|
name: tag,
|
2016-08-20 20:04:59 +00:00
|
|
|
class: 'default',
|
|
|
|
order: Number.MAX_SAFE_INTEGER,
|
|
|
|
score: 0,
|
2016-08-20 20:42:12 +00:00
|
|
|
desc: entry.entities[tag] || '',
|
2016-08-20 20:04:59 +00:00
|
|
|
};
|
2016-08-07 06:04:03 +00:00
|
|
|
|
2016-08-20 20:42:12 +00:00
|
|
|
this.applyTagMeta(tagItem);
|
|
|
|
tagItems.push(tagItem);
|
2016-08-20 20:04:59 +00:00
|
|
|
}
|
2016-08-07 06:04:03 +00:00
|
|
|
|
2016-08-20 20:04:59 +00:00
|
|
|
let score = 0;
|
2016-08-20 20:42:12 +00:00
|
|
|
for (const tagItem of tagItems) {
|
|
|
|
score += tagItem.score;
|
2016-04-19 02:38:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-08 05:42:56 +00:00
|
|
|
groups[entry.id] = {
|
2016-08-20 20:04:59 +00:00
|
|
|
score,
|
2016-08-20 20:42:12 +00:00
|
|
|
source,
|
|
|
|
rules,
|
2016-08-08 05:42:56 +00:00
|
|
|
expression: entry.expression,
|
2016-08-20 20:04:59 +00:00
|
|
|
reading: entry.reading,
|
|
|
|
glossary: entry.glossary,
|
2016-08-20 20:42:12 +00:00
|
|
|
tags: Translator.sortTags(tagItems)
|
2016-08-08 05:42:56 +00:00
|
|
|
};
|
2016-03-21 03:34:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 02:56:50 +00:00
|
|
|
processKanji(entries) {
|
2016-08-20 20:42:12 +00:00
|
|
|
const results = [];
|
2016-08-08 02:56:50 +00:00
|
|
|
|
2016-08-10 04:23:05 +00:00
|
|
|
for (const entry of entries) {
|
2016-08-20 20:42:12 +00:00
|
|
|
const tagItems = [];
|
|
|
|
for (const tag of entry.tags) {
|
|
|
|
const tagItem = {
|
|
|
|
name: tag,
|
2016-08-20 20:04:59 +00:00
|
|
|
class: 'default',
|
|
|
|
order: Number.MAX_SAFE_INTEGER,
|
|
|
|
desc: '',
|
|
|
|
};
|
|
|
|
|
2016-08-20 20:42:12 +00:00
|
|
|
this.applyTagMeta(tagItem);
|
|
|
|
tagItems.push(tagItem);
|
2016-08-20 20:04:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 20:42:12 +00:00
|
|
|
results.push({
|
2016-08-08 02:56:50 +00:00
|
|
|
character: entry.character,
|
2016-08-20 20:04:59 +00:00
|
|
|
kunyomi: entry.kunyomi,
|
|
|
|
onyomi: entry.onyomi,
|
2016-08-20 20:42:12 +00:00
|
|
|
glossary: entry.glossary,
|
|
|
|
tags: Translator.sortTags(tagItems)
|
2016-08-08 02:56:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-20 20:42:12 +00:00
|
|
|
return results;
|
2016-08-08 05:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applyTagMeta(tag) {
|
2016-08-09 02:50:49 +00:00
|
|
|
const symbol = tag.name.split(':')[0];
|
|
|
|
for (const prop in this.tagMeta[symbol] || {}) {
|
|
|
|
tag[prop] = this.tagMeta[symbol][prop];
|
2016-08-08 05:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|