2016-03-20 17:52:27 +00:00
|
|
|
/*
|
2020-01-01 17:00:00 +00:00
|
|
|
* Copyright (C) 2016-2020 Alex Yatskov <alex@foosoft.net>
|
2016-03-20 17:52:27 +00:00
|
|
|
* 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
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-03-20 17:52:27 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* Deinflector
|
|
|
|
* dictEnabledSet
|
|
|
|
* dictTagBuildSource
|
|
|
|
* dictTagSanitize
|
|
|
|
* dictTagsSort
|
|
|
|
* dictTermsCompressTags
|
|
|
|
* dictTermsGroup
|
|
|
|
* dictTermsMergeByGloss
|
|
|
|
* dictTermsMergeBySequence
|
|
|
|
* dictTermsSort
|
|
|
|
* dictTermsUndupe
|
2020-03-12 00:33:01 +00:00
|
|
|
* jp
|
2020-03-11 02:30:36 +00:00
|
|
|
* requestJson
|
|
|
|
*/
|
2016-03-24 02:25:32 +00:00
|
|
|
|
2016-03-20 17:52:27 +00:00
|
|
|
class Translator {
|
2020-03-31 00:39:04 +00:00
|
|
|
constructor(database) {
|
|
|
|
this.database = database;
|
2017-07-10 21:30:34 +00:00
|
|
|
this.deinflector = null;
|
2020-02-14 01:24:54 +00:00
|
|
|
this.tagCache = new Map();
|
2016-03-26 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
async prepare() {
|
2020-03-31 00:39:04 +00:00
|
|
|
const url = chrome.runtime.getURL('/bg/lang/deinflect.json');
|
|
|
|
const reasons = await requestJson(url, 'GET');
|
|
|
|
this.deinflector = new Deinflector(reasons);
|
2016-03-20 17:52:27 +00:00
|
|
|
}
|
2016-03-21 01:45:37 +00:00
|
|
|
|
2019-10-19 17:32:05 +00:00
|
|
|
async purgeDatabase() {
|
2020-02-14 01:24:54 +00:00
|
|
|
this.tagCache.clear();
|
2019-10-19 17:32:05 +00:00
|
|
|
await this.database.purge();
|
|
|
|
}
|
|
|
|
|
2019-11-02 20:21:06 +00:00
|
|
|
async deleteDictionary(dictionaryName) {
|
2020-02-14 01:24:54 +00:00
|
|
|
this.tagCache.clear();
|
2019-11-02 20:21:06 +00:00
|
|
|
await this.database.deleteDictionary(dictionaryName);
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:34:12 +00:00
|
|
|
async getSequencedDefinitions(definitions, mainDictionary) {
|
2020-01-28 23:58:14 +00:00
|
|
|
const [definitionsBySequence, defaultDefinitions] = dictTermsMergeBySequence(definitions, mainDictionary);
|
2019-10-19 15:34:12 +00:00
|
|
|
|
2020-01-28 23:58:14 +00:00
|
|
|
const sequenceList = [];
|
|
|
|
const sequencedDefinitions = [];
|
|
|
|
for (const [key, value] of definitionsBySequence.entries()) {
|
|
|
|
sequenceList.push(key);
|
|
|
|
sequencedDefinitions.push({definitions: value, rawDefinitions: []});
|
|
|
|
}
|
2019-10-19 15:34:12 +00:00
|
|
|
|
|
|
|
for (const definition of await this.database.findTermsBySequenceBulk(sequenceList, mainDictionary)) {
|
|
|
|
sequencedDefinitions[definition.index].rawDefinitions.push(definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {sequencedDefinitions, defaultDefinitions};
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:25:22 +00:00
|
|
|
async getMergedSecondarySearchResults(text, expressionsMap, secondarySearchDictionaries) {
|
2020-02-22 16:30:24 +00:00
|
|
|
if (secondarySearchDictionaries.size === 0) {
|
2019-10-19 16:42:26 +00:00
|
|
|
return [];
|
2019-10-19 16:24:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 16:42:26 +00:00
|
|
|
const expressionList = [];
|
|
|
|
const readingList = [];
|
2019-10-19 16:24:38 +00:00
|
|
|
for (const expression of expressionsMap.keys()) {
|
2019-10-19 16:42:26 +00:00
|
|
|
if (expression === text) { continue; }
|
2019-10-19 16:24:38 +00:00
|
|
|
for (const reading of expressionsMap.get(expression).keys()) {
|
2019-10-19 16:42:26 +00:00
|
|
|
expressionList.push(expression);
|
|
|
|
readingList.push(reading);
|
2019-10-19 16:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:25:22 +00:00
|
|
|
const definitions = await this.database.findTermsExactBulk(expressionList, readingList, secondarySearchDictionaries);
|
2019-10-19 16:42:26 +00:00
|
|
|
for (const definition of definitions) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (definitions.length > 1) {
|
|
|
|
definitions.sort((a, b) => a.index - b.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return definitions;
|
2019-10-19 16:24:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:25:22 +00:00
|
|
|
async getMergedDefinition(text, dictionaries, sequencedDefinition, defaultDefinitions, secondarySearchDictionaries, mergedByTermIndices) {
|
2019-10-19 16:16:38 +00:00
|
|
|
const result = sequencedDefinition.definitions;
|
|
|
|
const rawDefinitionsBySequence = sequencedDefinition.rawDefinitions;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-10-24 15:31:18 +00:00
|
|
|
|
2019-10-19 16:16:38 +00:00
|
|
|
const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence);
|
2020-02-15 20:25:22 +00:00
|
|
|
const secondarySearchResults = await this.getMergedSecondarySearchResults(text, result.expressions, secondarySearchDictionaries);
|
2017-10-15 02:19:16 +00:00
|
|
|
|
2019-10-19 16:16:38 +00:00
|
|
|
dictTermsMergeByGloss(result, defaultDefinitions.concat(secondarySearchResults), definitionsByGloss, mergedByTermIndices);
|
2017-10-03 04:20:02 +00:00
|
|
|
|
2020-02-15 21:17:11 +00:00
|
|
|
for (const definition of definitionsByGloss.values()) {
|
2019-10-19 16:16:38 +00:00
|
|
|
dictTagsSort(definition.definitionTags);
|
|
|
|
result.definitions.push(definition);
|
|
|
|
}
|
2017-10-03 04:20:02 +00:00
|
|
|
|
2019-10-19 16:16:38 +00:00
|
|
|
dictTermsSort(result.definitions, dictionaries);
|
|
|
|
|
|
|
|
const expressions = [];
|
2019-12-27 20:08:55 +00:00
|
|
|
for (const [expression, readingMap] of result.expressions.entries()) {
|
2020-02-15 22:15:24 +00:00
|
|
|
for (const [reading, termTagsMap] of readingMap.entries()) {
|
|
|
|
const termTags = [...termTagsMap.values()];
|
2019-11-27 03:01:54 +00:00
|
|
|
const score = termTags.map((tag) => tag.score).reduce((p, v) => p + v, 0);
|
2019-12-27 20:08:55 +00:00
|
|
|
expressions.push(Translator.createExpression(expression, reading, dictTagsSort(termTags), Translator.scoreToTermFrequency(score)));
|
2017-10-01 01:17:02 +00:00
|
|
|
}
|
2019-10-19 16:16:38 +00:00
|
|
|
}
|
2017-10-03 04:20:02 +00:00
|
|
|
|
2019-10-19 16:16:38 +00:00
|
|
|
result.expressions = expressions;
|
|
|
|
result.expression = Array.from(result.expression);
|
|
|
|
result.reading = Array.from(result.reading);
|
2017-10-01 01:17:02 +00:00
|
|
|
|
2019-10-19 16:16:38 +00:00
|
|
|
return result;
|
|
|
|
}
|
2017-10-17 08:16:38 +00:00
|
|
|
|
2020-02-15 20:09:59 +00:00
|
|
|
async findTerms(mode, text, details, options) {
|
|
|
|
switch (mode) {
|
2019-11-05 01:09:34 +00:00
|
|
|
case 'group':
|
2019-11-05 01:43:40 +00:00
|
|
|
return await this.findTermsGrouped(text, details, options);
|
2019-11-05 01:09:34 +00:00
|
|
|
case 'merge':
|
2019-11-05 01:43:40 +00:00
|
|
|
return await this.findTermsMerged(text, details, options);
|
2019-11-05 01:09:34 +00:00
|
|
|
case 'split':
|
2019-11-05 01:43:40 +00:00
|
|
|
return await this.findTermsSplit(text, details, options);
|
2020-02-15 20:09:59 +00:00
|
|
|
case 'simple':
|
|
|
|
return await this.findTermsSimple(text, details, options);
|
2019-11-05 01:09:34 +00:00
|
|
|
default:
|
|
|
|
return [[], 0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:43:40 +00:00
|
|
|
async findTermsGrouped(text, details, options) {
|
2019-11-05 01:09:34 +00:00
|
|
|
const dictionaries = dictEnabledSet(options);
|
2019-12-22 20:34:49 +00:00
|
|
|
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
|
2019-11-05 00:35:57 +00:00
|
|
|
|
|
|
|
const definitionsGrouped = dictTermsGroup(definitions, dictionaries);
|
2020-02-15 20:01:21 +00:00
|
|
|
await this.buildTermMeta(definitionsGrouped, dictionaries);
|
2019-11-05 00:35:57 +00:00
|
|
|
|
|
|
|
if (options.general.compactTags) {
|
|
|
|
for (const definition of definitionsGrouped) {
|
|
|
|
dictTermsCompressTags(definition.definitions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:09:34 +00:00
|
|
|
return [definitionsGrouped, length];
|
2019-11-05 00:35:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 01:43:40 +00:00
|
|
|
async findTermsMerged(text, details, options) {
|
2019-11-05 01:09:34 +00:00
|
|
|
const dictionaries = dictEnabledSet(options);
|
2020-02-15 20:25:22 +00:00
|
|
|
const secondarySearchDictionaries = new Map();
|
|
|
|
for (const [title, dictionary] of dictionaries.entries()) {
|
|
|
|
if (!dictionary.allowSecondarySearches) { continue; }
|
|
|
|
secondarySearchDictionaries.set(title, dictionary);
|
|
|
|
}
|
|
|
|
|
2019-12-22 20:34:49 +00:00
|
|
|
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
|
2019-10-19 16:16:38 +00:00
|
|
|
const {sequencedDefinitions, defaultDefinitions} = await this.getSequencedDefinitions(definitions, options.general.mainDictionary);
|
|
|
|
const definitionsMerged = [];
|
|
|
|
const mergedByTermIndices = new Set();
|
|
|
|
|
|
|
|
for (const sequencedDefinition of sequencedDefinitions) {
|
|
|
|
const result = await this.getMergedDefinition(
|
|
|
|
text,
|
|
|
|
dictionaries,
|
|
|
|
sequencedDefinition,
|
|
|
|
defaultDefinitions,
|
2020-02-15 20:25:22 +00:00
|
|
|
secondarySearchDictionaries,
|
2019-10-19 16:16:38 +00:00
|
|
|
mergedByTermIndices
|
|
|
|
);
|
2017-10-01 01:17:02 +00:00
|
|
|
definitionsMerged.push(result);
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:34:12 +00:00
|
|
|
const strayDefinitions = defaultDefinitions.filter((definition, index) => !mergedByTermIndices.has(index));
|
2017-10-05 03:56:45 +00:00
|
|
|
for (const groupedDefinition of dictTermsGroup(strayDefinitions, dictionaries)) {
|
2020-03-01 19:26:45 +00:00
|
|
|
// from dictTermsMergeBySequence
|
|
|
|
const {reasons, score, expression, reading, source, dictionary} = groupedDefinition;
|
|
|
|
const compatibilityDefinition = {
|
|
|
|
reasons,
|
|
|
|
score,
|
|
|
|
expression: [expression],
|
|
|
|
reading: [reading],
|
|
|
|
expressions: [Translator.createExpression(groupedDefinition.expression, groupedDefinition.reading)],
|
|
|
|
source,
|
|
|
|
dictionary,
|
|
|
|
definitions: groupedDefinition.definitions
|
|
|
|
};
|
|
|
|
definitionsMerged.push(compatibilityDefinition);
|
2017-10-05 03:56:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
await this.buildTermMeta(definitionsMerged, dictionaries);
|
2017-10-03 04:20:02 +00:00
|
|
|
|
2017-10-12 06:59:09 +00:00
|
|
|
if (options.general.compactTags) {
|
|
|
|
for (const definition of definitionsMerged) {
|
|
|
|
dictTermsCompressTags(definition.definitions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:09:34 +00:00
|
|
|
return [dictTermsSort(definitionsMerged), length];
|
2017-09-29 02:41:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 01:43:40 +00:00
|
|
|
async findTermsSplit(text, details, options) {
|
2019-11-05 01:09:34 +00:00
|
|
|
const dictionaries = dictEnabledSet(options);
|
2019-12-22 20:34:49 +00:00
|
|
|
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
|
2017-09-14 06:22:41 +00:00
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
await this.buildTermMeta(definitions, dictionaries);
|
2017-09-14 06:22:41 +00:00
|
|
|
|
2019-11-05 01:09:34 +00:00
|
|
|
return [definitions, length];
|
2017-07-10 21:10:58 +00:00
|
|
|
}
|
2017-01-17 04:54:01 +00:00
|
|
|
|
2020-02-15 20:09:59 +00:00
|
|
|
async findTermsSimple(text, details, options) {
|
|
|
|
const dictionaries = dictEnabledSet(options);
|
|
|
|
return await this.findTermsInternal(text, dictionaries, details, options);
|
|
|
|
}
|
|
|
|
|
2019-12-22 20:34:49 +00:00
|
|
|
async findTermsInternal(text, dictionaries, details, options) {
|
2019-12-22 23:26:27 +00:00
|
|
|
text = Translator.getSearchableText(text, options);
|
|
|
|
if (text.length === 0) {
|
|
|
|
return [[], 0];
|
2017-04-22 20:02:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 01:52:08 +00:00
|
|
|
const deinflections = (
|
|
|
|
details.wildcard ?
|
2020-02-15 20:01:21 +00:00
|
|
|
await this.findTermWildcard(text, dictionaries, details.wildcard) :
|
|
|
|
await this.findTermDeinflections(text, dictionaries, options)
|
2019-11-05 01:52:08 +00:00
|
|
|
);
|
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-10-12 23:40:20 +00:00
|
|
|
const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary);
|
|
|
|
definitionTags.push(dictTagBuildSource(definition.dictionary));
|
2017-10-11 06:18:55 +00:00
|
|
|
const termTags = await this.expandTags(definition.termTags, definition.dictionary);
|
2017-09-13 01:29:16 +00:00
|
|
|
|
2019-12-27 20:08:55 +00:00
|
|
|
const {expression, reading} = definition;
|
2020-03-12 00:33:01 +00:00
|
|
|
const furiganaSegments = jp.distributeFurigana(expression, reading);
|
2019-12-27 20:08:55 +00:00
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
definitions.push({
|
|
|
|
source: deinflection.source,
|
2019-12-22 19:07:30 +00:00
|
|
|
rawSource: deinflection.rawSource,
|
2017-07-10 21:10:58 +00:00
|
|
|
reasons: deinflection.reasons,
|
|
|
|
score: definition.score,
|
|
|
|
id: definition.id,
|
|
|
|
dictionary: definition.dictionary,
|
2019-12-27 20:08:55 +00:00
|
|
|
expression,
|
|
|
|
reading,
|
|
|
|
furiganaSegments,
|
2017-07-10 21:10:58 +00:00
|
|
|
glossary: definition.glossary,
|
2017-10-12 23:40:20 +00:00
|
|
|
definitionTags: dictTagsSort(definitionTags),
|
2017-10-11 06:18:55 +00:00
|
|
|
termTags: dictTagsSort(termTags),
|
2017-09-29 02:41:29 +00:00
|
|
|
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) {
|
2019-12-22 19:07:30 +00:00
|
|
|
length = Math.max(length, definition.rawSource.length);
|
2017-07-10 21:10:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 01:09:34 +00:00
|
|
|
return [definitions, length];
|
2016-03-21 01:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
async findTermWildcard(text, dictionaries, wildcard) {
|
|
|
|
const definitions = await this.database.findTermsBulk([text], dictionaries, wildcard);
|
2019-11-05 01:52:08 +00:00
|
|
|
if (definitions.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [{
|
|
|
|
source: text,
|
2019-12-22 19:07:30 +00:00
|
|
|
rawSource: text,
|
2019-11-05 01:52:08 +00:00
|
|
|
term: text,
|
|
|
|
rules: 0,
|
|
|
|
definitions,
|
|
|
|
reasons: []
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
async findTermDeinflections(text, dictionaries, options) {
|
2019-12-22 18:09:35 +00:00
|
|
|
const deinflections = this.getAllDeinflections(text, options);
|
2019-08-31 01:06:21 +00:00
|
|
|
|
|
|
|
if (deinflections.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-10-01 02:09:16 +00:00
|
|
|
const uniqueDeinflectionTerms = [];
|
|
|
|
const uniqueDeinflectionArrays = [];
|
2020-01-25 02:51:58 +00:00
|
|
|
const uniqueDeinflectionsMap = new Map();
|
2019-10-01 02:09:16 +00:00
|
|
|
for (const deinflection of deinflections) {
|
|
|
|
const term = deinflection.term;
|
2020-01-25 02:51:58 +00:00
|
|
|
let deinflectionArray = uniqueDeinflectionsMap.get(term);
|
|
|
|
if (typeof deinflectionArray === 'undefined') {
|
2019-10-01 02:09:16 +00:00
|
|
|
deinflectionArray = [];
|
|
|
|
uniqueDeinflectionTerms.push(term);
|
|
|
|
uniqueDeinflectionArrays.push(deinflectionArray);
|
2020-01-25 02:51:58 +00:00
|
|
|
uniqueDeinflectionsMap.set(term, deinflectionArray);
|
2019-10-01 02:09:16 +00:00
|
|
|
}
|
|
|
|
deinflectionArray.push(deinflection);
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, dictionaries, null);
|
2019-08-31 01:06:21 +00:00
|
|
|
|
2019-10-01 02:09:16 +00:00
|
|
|
for (const definition of definitions) {
|
2019-10-05 20:24:42 +00:00
|
|
|
const definitionRules = Deinflector.rulesToRuleFlags(definition.rules);
|
2019-10-01 02:09:16 +00:00
|
|
|
for (const deinflection of uniqueDeinflectionArrays[definition.index]) {
|
2019-10-05 20:24:42 +00:00
|
|
|
const deinflectionRules = deinflection.rules;
|
|
|
|
if (deinflectionRules === 0 || (definitionRules & deinflectionRules) !== 0) {
|
2019-10-01 02:09:16 +00:00
|
|
|
deinflection.definitions.push(definition);
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 03:01:54 +00:00
|
|
|
return deinflections.filter((e) => e.definitions.length > 0);
|
2019-08-31 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 19:07:30 +00:00
|
|
|
getAllDeinflections(text, options) {
|
|
|
|
const translationOptions = options.translation;
|
2019-12-22 18:09:35 +00:00
|
|
|
const textOptionVariantArray = [
|
2019-12-22 19:07:30 +00:00
|
|
|
Translator.getTextOptionEntryVariants(translationOptions.convertHalfWidthCharacters),
|
|
|
|
Translator.getTextOptionEntryVariants(translationOptions.convertNumericCharacters),
|
2020-01-24 01:28:38 +00:00
|
|
|
Translator.getTextOptionEntryVariants(translationOptions.convertAlphabeticCharacters),
|
2020-01-24 01:49:41 +00:00
|
|
|
Translator.getTextOptionEntryVariants(translationOptions.convertHiraganaToKatakana),
|
2020-01-24 01:28:38 +00:00
|
|
|
Translator.getTextOptionEntryVariants(translationOptions.convertKatakanaToHiragana)
|
2019-12-22 18:09:35 +00:00
|
|
|
];
|
2016-03-21 03:34:50 +00:00
|
|
|
|
2019-08-31 01:06:21 +00:00
|
|
|
const deinflections = [];
|
2019-12-22 18:09:35 +00:00
|
|
|
const used = new Set();
|
2020-01-24 01:49:41 +00:00
|
|
|
for (const [halfWidth, numeric, alphabetic, katakana, hiragana] of Translator.getArrayVariants(textOptionVariantArray)) {
|
2019-12-22 18:09:35 +00:00
|
|
|
let text2 = text;
|
2019-12-22 19:07:30 +00:00
|
|
|
let sourceMapping = null;
|
|
|
|
if (halfWidth) {
|
|
|
|
if (sourceMapping === null) { sourceMapping = Translator.createTextSourceMapping(text2); }
|
2020-03-12 00:33:01 +00:00
|
|
|
text2 = jp.convertHalfWidthKanaToFullWidth(text2, sourceMapping);
|
2019-12-22 19:07:30 +00:00
|
|
|
}
|
2020-01-24 01:28:38 +00:00
|
|
|
if (numeric) {
|
2020-03-14 20:38:12 +00:00
|
|
|
text2 = jp.convertNumericToFullWidth(text2);
|
2020-01-24 01:28:38 +00:00
|
|
|
}
|
2019-12-22 19:07:30 +00:00
|
|
|
if (alphabetic) {
|
|
|
|
if (sourceMapping === null) { sourceMapping = Translator.createTextSourceMapping(text2); }
|
2020-03-12 00:33:01 +00:00
|
|
|
text2 = jp.convertAlphabeticToKana(text2, sourceMapping);
|
2019-12-22 19:07:30 +00:00
|
|
|
}
|
2020-01-24 01:49:41 +00:00
|
|
|
if (katakana) {
|
2020-03-12 00:33:01 +00:00
|
|
|
text2 = jp.convertHiraganaToKatakana(text2);
|
2020-01-24 01:49:41 +00:00
|
|
|
}
|
2020-01-24 01:28:38 +00:00
|
|
|
if (hiragana) {
|
2020-03-12 00:33:01 +00:00
|
|
|
text2 = jp.convertKatakanaToHiragana(text2);
|
2020-01-24 01:28:38 +00:00
|
|
|
}
|
2019-12-22 18:09:35 +00:00
|
|
|
|
|
|
|
for (let i = text2.length; i > 0; --i) {
|
|
|
|
const text2Substring = text2.substring(0, i);
|
|
|
|
if (used.has(text2Substring)) { break; }
|
|
|
|
used.add(text2Substring);
|
2019-12-22 19:07:30 +00:00
|
|
|
for (const deinflection of this.deinflector.deinflect(text2Substring)) {
|
|
|
|
deinflection.rawSource = Translator.getDeinflectionRawSource(text, i, sourceMapping);
|
|
|
|
deinflections.push(deinflection);
|
|
|
|
}
|
2019-08-31 01:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return deinflections;
|
|
|
|
}
|
|
|
|
|
2019-12-22 19:07:30 +00:00
|
|
|
static getTextOptionEntryVariants(value) {
|
|
|
|
switch (value) {
|
|
|
|
case 'true': return [true];
|
|
|
|
case 'variant': return [false, true];
|
|
|
|
default: return [false];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDeinflectionRawSource(source, length, sourceMapping) {
|
|
|
|
if (sourceMapping === null) {
|
|
|
|
return source.substring(0, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = '';
|
|
|
|
let index = 0;
|
|
|
|
for (let i = 0; i < length; ++i) {
|
|
|
|
const c = sourceMapping[i];
|
|
|
|
result += source.substring(index, index + c);
|
|
|
|
index += c;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static createTextSourceMapping(text) {
|
|
|
|
return new Array(text.length).fill(1);
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:13:03 +00:00
|
|
|
async findKanji(text, options) {
|
|
|
|
const dictionaries = dictEnabledSet(options);
|
2020-02-15 17:30:07 +00:00
|
|
|
const kanjiUnique = new Set();
|
2017-07-10 21:10:58 +00:00
|
|
|
for (const c of text) {
|
2020-02-15 17:30:07 +00:00
|
|
|
kanjiUnique.add(c);
|
2017-07-10 21:10:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
const definitions = await this.database.findKanjiBulk([...kanjiUnique], dictionaries);
|
2019-10-19 14:21:10 +00:00
|
|
|
if (definitions.length === 0) {
|
|
|
|
return definitions;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (definitions.length > 1) {
|
|
|
|
definitions.sort((a, b) => a.index - b.index);
|
|
|
|
}
|
|
|
|
|
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));
|
2020-01-25 03:27:25 +00:00
|
|
|
dictTagsSort(tags);
|
2017-09-18 02:16:08 +00:00
|
|
|
|
2020-01-25 03:27:25 +00:00
|
|
|
const stats = await this.expandStats(definition.stats, definition.dictionary);
|
2019-10-19 14:21:10 +00:00
|
|
|
|
2020-01-25 03:27:25 +00:00
|
|
|
definition.tags = tags;
|
|
|
|
definition.stats = stats;
|
2016-08-08 02:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
await this.buildKanjiMeta(definitions, dictionaries);
|
2020-01-25 03:27:25 +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
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
async buildTermMeta(definitions, dictionaries) {
|
2019-08-31 01:06:21 +00:00
|
|
|
const terms = [];
|
|
|
|
for (const definition of definitions) {
|
|
|
|
if (definition.expressions) {
|
|
|
|
terms.push(...definition.expressions);
|
|
|
|
} else {
|
|
|
|
terms.push(definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (terms.length === 0) {
|
|
|
|
return;
|
2017-10-03 04:20:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 01:06:21 +00:00
|
|
|
// Create mapping of unique terms
|
|
|
|
const expressionsUnique = [];
|
|
|
|
const termsUnique = [];
|
2020-01-25 02:57:01 +00:00
|
|
|
const termsUniqueMap = new Map();
|
2019-08-31 01:06:21 +00:00
|
|
|
for (let i = 0, ii = terms.length; i < ii; ++i) {
|
|
|
|
const term = terms[i];
|
|
|
|
const expression = term.expression;
|
2020-01-25 02:57:01 +00:00
|
|
|
let termList = termsUniqueMap.get(expression);
|
|
|
|
if (typeof termList === 'undefined') {
|
|
|
|
termList = [];
|
2019-08-31 01:06:21 +00:00
|
|
|
expressionsUnique.push(expression);
|
|
|
|
termsUnique.push(termList);
|
2020-02-13 23:12:28 +00:00
|
|
|
termsUniqueMap.set(expression, termList);
|
2019-08-31 01:06:21 +00:00
|
|
|
}
|
2020-01-25 02:57:01 +00:00
|
|
|
termList.push(term);
|
|
|
|
|
|
|
|
// New data
|
|
|
|
term.frequencies = [];
|
2019-08-31 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
const metas = await this.database.findTermMetaBulk(expressionsUnique, dictionaries);
|
2020-01-25 03:24:05 +00:00
|
|
|
for (const {expression, mode, data, dictionary, index} of metas) {
|
|
|
|
switch (mode) {
|
|
|
|
case 'freq':
|
|
|
|
for (const term of termsUnique[index]) {
|
|
|
|
term.frequencies.push({expression, frequency: data, dictionary});
|
|
|
|
}
|
|
|
|
break;
|
2017-09-23 15:46:34 +00:00
|
|
|
}
|
2017-09-14 06:22:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
async buildKanjiMeta(definitions, dictionaries) {
|
2020-01-25 03:27:25 +00:00
|
|
|
const kanjiList = [];
|
|
|
|
for (const definition of definitions) {
|
|
|
|
kanjiList.push(definition.character);
|
|
|
|
definition.frequencies = [];
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:01:21 +00:00
|
|
|
const metas = await this.database.findKanjiMetaBulk(kanjiList, dictionaries);
|
2020-01-25 03:27:25 +00:00
|
|
|
for (const {character, mode, data, dictionary, index} of metas) {
|
|
|
|
switch (mode) {
|
|
|
|
case 'freq':
|
|
|
|
definitions[index].frequencies.push({character, frequency: data, dictionary});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 02:16:08 +00:00
|
|
|
async expandTags(names, title) {
|
2019-10-19 17:32:05 +00:00
|
|
|
const tagMetaList = await this.getTagMetaList(names, title);
|
|
|
|
return tagMetaList.map((meta, index) => {
|
|
|
|
const name = names[index];
|
|
|
|
const tag = dictTagSanitize(Object.assign({}, meta !== null ? meta : {}, {name}));
|
|
|
|
return dictTagSanitize(tag);
|
|
|
|
});
|
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) {
|
2019-10-19 17:32:05 +00:00
|
|
|
const names = Object.keys(items);
|
|
|
|
const tagMetaList = await this.getTagMetaList(names, title);
|
2017-09-18 02:16:08 +00:00
|
|
|
|
2020-02-15 18:12:03 +00:00
|
|
|
const statsGroups = new Map();
|
2019-10-19 17:32:05 +00:00
|
|
|
for (let i = 0; i < names.length; ++i) {
|
|
|
|
const name = names[i];
|
|
|
|
const meta = tagMetaList[i];
|
|
|
|
if (meta === null) { continue; }
|
|
|
|
|
|
|
|
const category = meta.category;
|
2020-02-15 18:12:03 +00:00
|
|
|
let group = statsGroups.get(category);
|
|
|
|
if (typeof group === 'undefined') {
|
|
|
|
group = [];
|
|
|
|
statsGroups.set(category, group);
|
|
|
|
}
|
2019-08-30 23:38:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-15 18:12:03 +00:00
|
|
|
const stats = {};
|
2019-10-19 17:32:05 +00:00
|
|
|
const sortCompare = (a, b) => a.notes - b.notes;
|
2020-02-15 18:12:03 +00:00
|
|
|
for (const [category, group] of statsGroups.entries()) {
|
|
|
|
group.sort(sortCompare);
|
|
|
|
stats[category] = group;
|
2017-09-22 06:20:51 +00:00
|
|
|
}
|
2017-09-18 02:57:39 +00:00
|
|
|
return stats;
|
2017-09-18 02:16:08 +00:00
|
|
|
}
|
2019-08-11 18:12:01 +00:00
|
|
|
|
2019-10-19 17:32:05 +00:00
|
|
|
async getTagMetaList(names, title) {
|
|
|
|
const tagMetaList = [];
|
2020-02-14 01:24:54 +00:00
|
|
|
let cache = this.tagCache.get(title);
|
|
|
|
if (typeof cache === 'undefined') {
|
|
|
|
cache = new Map();
|
|
|
|
this.tagCache.set(title, cache);
|
|
|
|
}
|
2019-10-19 17:32:05 +00:00
|
|
|
|
|
|
|
for (const name of names) {
|
|
|
|
const base = Translator.getNameBase(name);
|
|
|
|
|
2020-02-14 01:24:54 +00:00
|
|
|
let tagMeta = cache.get(base);
|
|
|
|
if (typeof tagMeta === 'undefined') {
|
|
|
|
tagMeta = await this.database.findTagForTitle(base, title);
|
|
|
|
cache.set(base, tagMeta);
|
2019-10-19 17:32:05 +00:00
|
|
|
}
|
2020-02-14 01:24:54 +00:00
|
|
|
|
|
|
|
tagMetaList.push(tagMeta);
|
2019-10-19 17:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tagMetaList;
|
|
|
|
}
|
|
|
|
|
2019-12-27 20:08:55 +00:00
|
|
|
static createExpression(expression, reading, termTags=null, termFrequency=null) {
|
2020-03-12 00:33:01 +00:00
|
|
|
const furiganaSegments = jp.distributeFurigana(expression, reading);
|
2019-12-27 20:08:55 +00:00
|
|
|
return {
|
|
|
|
expression,
|
|
|
|
reading,
|
|
|
|
furiganaSegments,
|
|
|
|
termTags,
|
|
|
|
termFrequency
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-19 18:18:57 +00:00
|
|
|
static scoreToTermFrequency(score) {
|
|
|
|
if (score > 0) {
|
|
|
|
return 'popular';
|
|
|
|
} else if (score < 0) {
|
|
|
|
return 'rare';
|
|
|
|
} else {
|
|
|
|
return 'normal';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:12:01 +00:00
|
|
|
static getNameBase(name) {
|
|
|
|
const pos = name.indexOf(':');
|
2019-11-24 16:02:52 +00:00
|
|
|
return (pos >= 0 ? name.substring(0, pos) : name);
|
2019-08-11 18:12:01 +00:00
|
|
|
}
|
2019-12-22 18:09:35 +00:00
|
|
|
|
|
|
|
static *getArrayVariants(arrayVariants) {
|
|
|
|
const ii = arrayVariants.length;
|
|
|
|
|
|
|
|
let total = 1;
|
|
|
|
for (let i = 0; i < ii; ++i) {
|
|
|
|
total *= arrayVariants[i].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let a = 0; a < total; ++a) {
|
|
|
|
const variant = [];
|
|
|
|
let index = a;
|
|
|
|
for (let i = 0; i < ii; ++i) {
|
|
|
|
const entryVariants = arrayVariants[i];
|
|
|
|
variant.push(entryVariants[index % entryVariants.length]);
|
|
|
|
index = Math.floor(index / entryVariants.length);
|
|
|
|
}
|
|
|
|
yield variant;
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 23:26:27 +00:00
|
|
|
|
|
|
|
static getSearchableText(text, options) {
|
|
|
|
if (!options.scanning.alphanumeric) {
|
2020-03-10 01:34:31 +00:00
|
|
|
let newText = '';
|
|
|
|
for (const c of text) {
|
2020-03-12 00:33:01 +00:00
|
|
|
if (!jp.isCodePointJapanese(c.codePointAt(0))) {
|
2019-12-22 23:26:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-10 01:34:31 +00:00
|
|
|
newText += c;
|
2019-12-22 23:26:27 +00:00
|
|
|
}
|
2020-03-10 01:34:31 +00:00
|
|
|
text = newText;
|
2019-12-22 23:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
2016-03-20 17:52:27 +00:00
|
|
|
}
|