yomichan/ext/bg/js/translator.js

244 lines
6.7 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-24 05:28:37 +00:00
loadJson('bg/data/rules.json').then((rules) => {
2016-08-24 03:53:11 +00:00
this.deinflector.setRules(rules);
2016-08-24 05:28:37 +00:00
return loadJson('bg/data/tags.json');
2016-08-24 03:53:11 +00:00
}).then((tagMeta) => {
this.tagMeta = tagMeta;
2016-08-24 03:33:04 +00:00
return this.dictionary.existsDb();
}).then((exists) => {
if (exists) {
return this.dictionary.loadDb();
} else {
this.dictionary.initDb();
return Promise.all([
this.dictionary.importTermDict('bg/data/edict/index.json'),
this.dictionary.importTermDict('bg/data/enamdict/index.json')
]);
}
2016-08-21 20:32:36 +00:00
}).then(() => {
2016-08-24 03:33:04 +00:00
this.dictionary.findTerm('猫').then((result) => {
console.log(result);
2016-08-22 15:48:19 +00:00
});
2016-08-24 03:53:11 +00:00
this.loaded = true;
callback();
2016-08-21 20:32:36 +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 = [];
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;
}
const s1 = v1.score;
const s2 = v2.score;
if (s1 > s2) {
2016-04-02 05:13:37 +00:00
return -1;
} 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;
}
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
}
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)) {
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,
class: 'default',
order: Number.MAX_SAFE_INTEGER,
score: 0,
2016-08-20 20:42:12 +00:00
desc: entry.entities[tag] || '',
};
2016-08-07 06:04:03 +00:00
2016-08-20 20:42:12 +00:00
this.applyTagMeta(tagItem);
tagItems.push(tagItem);
}
2016-08-07 06:04:03 +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] = {
score,
2016-08-20 20:42:12 +00:00
source,
rules,
2016-08-08 05:42:56 +00:00
expression: entry.expression,
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,
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:42:12 +00:00
results.push({
2016-08-08 02:56:50 +00:00
character: entry.character,
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-03-20 17:52:27 +00:00
}