yomichan/ext/bg/js/translator.js

417 lines
15 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-20 04:28:09 +00:00
const reasons = await requestJson(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
async findTermsGrouped(text, dictionaries, alphanumeric, options) {
2017-09-14 06:22:41 +00:00
const titles = Object.keys(dictionaries);
2017-07-10 21:10:58 +00:00
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
2017-09-14 06:22:41 +00:00
const definitionsGrouped = dictTermsGroup(definitions, dictionaries);
2019-08-31 01:06:21 +00:00
await this.buildTermFrequencies(definitionsGrouped, titles);
2017-09-14 06:22:41 +00:00
2017-10-12 06:59:09 +00:00
if (options.general.compactTags) {
for (const definition of definitionsGrouped) {
dictTermsCompressTags(definition.definitions);
}
}
2017-09-14 06:22:41 +00:00
return {length, definitions: definitionsGrouped};
}
async findTermsMerged(text, dictionaries, alphanumeric, options) {
2017-10-15 02:19:16 +00:00
const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches);
const titles = Object.keys(dictionaries);
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
const definitionsBySequence = dictTermsMergeBySequence(definitions, options.general.mainDictionary);
const definitionsMerged = [];
2017-10-06 22:19:40 +00:00
const mergedByTermIndices = new Set();
for (const sequence in definitionsBySequence) {
2017-10-24 20:10:47 +00:00
if (sequence < 0) {
continue;
}
const result = definitionsBySequence[sequence];
const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), options.general.mainDictionary);
for (const definition of rawDefinitionsBySequence) {
const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary);
definitionTags.push(dictTagBuildSource(definition.dictionary));
definition.definitionTags = definitionTags;
const termTags = await this.expandTags(definition.termTags, definition.dictionary);
definition.termTags = termTags;
}
const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence);
2017-10-15 02:19:16 +00:00
const secondarySearchResults = [];
2017-10-24 20:10:47 +00:00
if (secondarySearchTitles.length > 0) {
2017-10-15 02:19:16 +00:00
for (const expression of result.expressions.keys()) {
if (expression === text) {
continue;
}
for (const reading of result.expressions.get(expression).keys()) {
for (const definition of await this.database.findTermsExact(expression, reading, secondarySearchTitles)) {
const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary);
definitionTags.push(dictTagBuildSource(definition.dictionary));
definition.definitionTags = definitionTags;
const termTags = await this.expandTags(definition.termTags, definition.dictionary);
definition.termTags = termTags;
2017-10-15 02:19:16 +00:00
secondarySearchResults.push(definition);
}
}
}
}
dictTermsMergeByGloss(result, definitionsBySequence['-1'].concat(secondarySearchResults), definitionsByGloss, mergedByTermIndices);
for (const gloss in definitionsByGloss) {
const definition = definitionsByGloss[gloss];
dictTagsSort(definition.definitionTags);
result.definitions.push(definition);
}
2017-10-06 22:19:40 +00:00
dictTermsSort(result.definitions, dictionaries);
const expressions = [];
for (const expression of result.expressions.keys()) {
for (const reading of result.expressions.get(expression).keys()) {
const termTags = result.expressions.get(expression).get(reading);
expressions.push({
expression: expression,
reading: reading,
termTags: dictTagsSort(termTags),
termFrequency: (score => {
if (score > 0) {
return 'popular';
} else if (score < 0) {
return 'rare';
} else {
return 'normal';
}
})(termTags.map(tag => tag.score).reduce((p, v) => p + v, 0))
});
}
}
result.expressions = expressions;
2017-10-17 08:16:38 +00:00
result.expression = Array.from(result.expression);
result.reading = Array.from(result.reading);
definitionsMerged.push(result);
}
2017-10-06 22:19:40 +00:00
const strayDefinitions = definitionsBySequence['-1'].filter((definition, index) => !mergedByTermIndices.has(index));
for (const groupedDefinition of dictTermsGroup(strayDefinitions, dictionaries)) {
groupedDefinition.expressions = [{expression: groupedDefinition.expression, reading: groupedDefinition.reading}];
definitionsMerged.push(groupedDefinition);
}
2019-08-31 01:06:21 +00:00
await this.buildTermFrequencies(definitionsMerged, titles);
2017-10-12 06:59:09 +00:00
if (options.general.compactTags) {
for (const definition of definitionsMerged) {
dictTermsCompressTags(definition.definitions);
}
}
return {length, definitions: dictTermsSort(definitionsMerged)};
}
2017-09-14 06:22:41 +00:00
async findTermsSplit(text, dictionaries, alphanumeric) {
const titles = Object.keys(dictionaries);
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
2019-08-31 01:06:21 +00:00
await this.buildTermFrequencies(definitions, titles);
2017-09-14 06:22:41 +00:00
return {length, definitions};
2017-07-10 21:10:58 +00:00
}
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 textHiragana = jpKatakanaToHiragana(text);
2019-08-31 01:06:21 +00:00
const titles = Object.keys(dictionaries);
const deinflections = await this.findTermDeinflections(text, textHiragana, titles);
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) {
const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary);
definitionTags.push(dictTagBuildSource(definition.dictionary));
const termTags = await this.expandTags(definition.termTags, definition.dictionary);
2017-09-13 01:29:16 +00:00
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,
definitionTags: dictTagsSort(definitionTags),
termTags: dictTagsSort(termTags),
sequence: definition.sequence
2017-07-10 21:10:58 +00:00
});
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
}
2019-08-31 01:06:21 +00:00
async findTermDeinflections(text, text2, titles) {
const deinflections = (text === text2 ? this.getDeinflections(text) : this.getDeinflections2(text, text2));
if (deinflections.length === 0) {
return [];
}
const uniqueDeinflectionTerms = [];
const uniqueDeinflectionArrays = [];
const uniqueDeinflectionsMap = {};
for (const deinflection of deinflections) {
const term = deinflection.term;
let deinflectionArray;
if (uniqueDeinflectionsMap.hasOwnProperty(term)) {
deinflectionArray = uniqueDeinflectionsMap[term];
} else {
deinflectionArray = [];
uniqueDeinflectionTerms.push(term);
uniqueDeinflectionArrays.push(deinflectionArray);
uniqueDeinflectionsMap[term] = deinflectionArray;
}
deinflectionArray.push(deinflection);
}
const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles);
2019-08-31 01:06:21 +00:00
for (const definition of definitions) {
const definitionRules = Deinflector.rulesToRuleFlags(definition.rules);
for (const deinflection of uniqueDeinflectionArrays[definition.index]) {
const deinflectionRules = deinflection.rules;
if (deinflectionRules === 0 || (definitionRules & deinflectionRules) !== 0) {
deinflection.definitions.push(definition);
}
}
2019-08-31 01:06:21 +00:00
}
return deinflections.filter(e => e.definitions.length > 0);
}
getDeinflections(text) {
const 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.push(...this.deinflector.deinflect(textSlice));
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
}
2019-08-31 01:06:21 +00:00
getDeinflections2(text, text2) {
const deinflections = [];
for (let i = text.length; i > 0; --i) {
const textSlice = text.slice(0, i);
const text2Slice = text2.slice(0, i);
deinflections.push(...this.deinflector.deinflect(textSlice));
2019-08-31 01:06:21 +00:00
if (textSlice !== text2Slice) {
deinflections.push(...this.deinflector.deinflect(text2Slice));
2019-08-31 01:06:21 +00:00
}
}
return deinflections;
}
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.push(...await this.database.findKanji(c, titles));
2017-07-10 21:10:58 +00:00
processed[c] = true;
}
}
2017-02-26 19:12:54 +00:00
for (const definition of definitions) {
2017-09-18 02:16:08 +00:00
const tags = await this.expandTags(definition.tags, definition.dictionary);
2017-07-10 21:10:58 +00:00
tags.push(dictTagBuildSource(definition.dictionary));
2017-09-18 02:16:08 +00:00
2017-03-03 05:01:49 +00:00
definition.tags = dictTagsSort(tags);
2017-09-18 02:57:39 +00:00
definition.stats = await this.expandStats(definition.stats, definition.dictionary);
2017-09-23 15:46:34 +00:00
definition.frequencies = [];
2017-09-23 20:31:03 +00:00
for (const meta of await this.database.findKanjiMeta(definition.character, titles)) {
2017-09-23 15:46:34 +00:00
if (meta.mode === 'freq') {
definition.frequencies.push({
character: meta.character,
frequency: meta.data,
dictionary: meta.dictionary
});
}
}
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
}
2017-09-13 23:42:04 +00:00
2019-08-31 01:06:21 +00:00
async buildTermFrequencies(definitions, titles) {
const terms = [];
for (const definition of definitions) {
if (definition.expressions) {
terms.push(...definition.expressions);
} else {
terms.push(definition);
}
}
if (terms.length === 0) {
return;
}
2019-08-31 01:06:21 +00:00
// Create mapping of unique terms
const expressionsUnique = [];
const termsUnique = [];
const termsUniqueMap = {};
for (let i = 0, ii = terms.length; i < ii; ++i) {
const term = terms[i];
const expression = term.expression;
term.frequencies = [];
2019-08-31 01:06:21 +00:00
if (termsUniqueMap.hasOwnProperty(expression)) {
termsUniqueMap[expression].push(term);
} else {
const termList = [term];
expressionsUnique.push(expression);
termsUnique.push(termList);
termsUniqueMap[expression] = termList;
}
}
const metas = await this.database.findTermMetaBulk(expressionsUnique, titles);
for (const meta of metas) {
if (meta.mode !== 'freq') {
continue;
}
for (const term of termsUnique[meta.index]) {
term.frequencies.push({
expression: meta.expression,
frequency: meta.data,
dictionary: meta.dictionary
});
2017-09-23 15:46:34 +00:00
}
2017-09-14 06:22:41 +00:00
}
}
2017-09-18 02:16:08 +00:00
async expandTags(names, title) {
2017-09-14 17:15:54 +00:00
const tags = [];
2017-09-18 02:16:08 +00:00
for (const name of names) {
const base = Translator.getNameBase(name);
let meta = this.database.findTagForTitleCached(base, title);
if (typeof meta === 'undefined') {
meta = await this.database.findTagForTitle(base, title);
2017-09-13 23:42:04 +00:00
}
const tag = Object.assign({}, meta !== null ? meta : {}, {name});
2017-09-14 17:15:54 +00:00
tags.push(dictTagSanitize(tag));
2017-09-13 23:42:04 +00:00
}
2017-09-14 17:15:54 +00:00
return tags;
2017-09-13 23:42:04 +00:00
}
2017-09-18 02:16:08 +00:00
2017-09-18 02:57:39 +00:00
async expandStats(items, title) {
2017-09-21 06:15:02 +00:00
const stats = {};
2017-09-18 02:16:08 +00:00
for (const name in items) {
const base = Translator.getNameBase(name);
let meta = this.database.findTagForTitleCached(base, title);
if (typeof meta === 'undefined') {
meta = await this.database.findTagForTitle(base, title);
if (meta === null) {
continue;
2017-09-18 02:16:08 +00:00
}
}
const group = stats[meta.category] = stats[meta.category] || [];
const stat = Object.assign({}, meta, {name, value: items[name]});
2017-09-21 06:15:02 +00:00
group.push(dictTagSanitize(stat));
2017-09-18 02:16:08 +00:00
}
2017-09-22 06:20:51 +00:00
for (const category in stats) {
stats[category].sort((a, b) => {
if (a.notes < b.notes) {
return -1;
} else if (a.notes > b.notes) {
return 1;
} else {
return 0;
}
});
}
2017-09-18 02:57:39 +00:00
return stats;
2017-09-18 02:16:08 +00:00
}
static getNameBase(name) {
const pos = name.indexOf(':');
return (pos >= 0 ? name.substr(0, pos) : name);
}
2016-03-20 17:52:27 +00:00
}