2016-03-20 17:52:27 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2016-2021 Yomichan Authors
|
2016-03-20 17:52:27 +00:00
|
|
|
*
|
|
|
|
* 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
|
2021-03-09 02:01:55 +00:00
|
|
|
* RegexUtil
|
2020-03-28 21:51:58 +00:00
|
|
|
* TextSourceMap
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2016-03-24 02:25:32 +00:00
|
|
|
|
2020-10-04 16:54:55 +00:00
|
|
|
/**
|
2021-03-25 23:55:31 +00:00
|
|
|
* Class which finds term and kanji dictionary entries for text.
|
2020-10-04 16:54:55 +00:00
|
|
|
*/
|
2016-03-20 17:52:27 +00:00
|
|
|
class Translator {
|
2020-10-04 16:54:55 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Translator instance.
|
2021-04-04 20:22:35 +00:00
|
|
|
* @param japaneseUtil An instance of JapaneseUtil.
|
2020-10-04 16:54:55 +00:00
|
|
|
* @param database An instance of DictionaryDatabase.
|
|
|
|
*/
|
2020-11-29 18:09:02 +00:00
|
|
|
constructor({japaneseUtil, database}) {
|
|
|
|
this._japaneseUtil = japaneseUtil;
|
2020-08-09 17:21:14 +00:00
|
|
|
this._database = database;
|
|
|
|
this._deinflector = null;
|
|
|
|
this._tagCache = new Map();
|
2020-10-02 21:59:14 +00:00
|
|
|
this._stringComparer = new Intl.Collator('en-US'); // Invariant locale
|
2021-10-15 01:26:53 +00:00
|
|
|
this._numberRegex = /[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?/;
|
2016-03-26 21:16:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 16:54:55 +00:00
|
|
|
/**
|
|
|
|
* Initializes the instance for use. The public API should not be used until
|
2020-10-04 17:09:04 +00:00
|
|
|
* this function has been called.
|
|
|
|
* @param deinflectionReasons The raw deinflections reasons data that the Deinflector uses.
|
2020-10-04 16:54:55 +00:00
|
|
|
*/
|
2020-10-04 17:09:04 +00:00
|
|
|
prepare(deinflectionReasons) {
|
|
|
|
this._deinflector = new Deinflector(deinflectionReasons);
|
2016-03-20 17:52:27 +00:00
|
|
|
}
|
2016-03-21 01:45:37 +00:00
|
|
|
|
2020-10-04 16:54:55 +00:00
|
|
|
/**
|
|
|
|
* Clears the database tag cache. This should be executed if the database is changed.
|
|
|
|
*/
|
2020-05-06 23:28:26 +00:00
|
|
|
clearDatabaseCaches() {
|
2020-08-09 17:21:14 +00:00
|
|
|
this._tagCache.clear();
|
2019-11-02 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 16:54:55 +00:00
|
|
|
/**
|
|
|
|
* Finds term definitions for the given text.
|
|
|
|
* @param mode The mode to use for finding terms, which determines the format of the resulting array.
|
2020-12-09 01:27:36 +00:00
|
|
|
* One of: 'group', 'merge', 'split', 'simple'
|
2020-10-04 16:54:55 +00:00
|
|
|
* @param text The text to find terms for.
|
|
|
|
* @param options An object using the following structure:
|
2021-03-25 23:55:31 +00:00
|
|
|
* ```
|
2020-10-04 16:54:55 +00:00
|
|
|
* {
|
2020-12-09 01:27:36 +00:00
|
|
|
* wildcard: (enum: null, 'prefix', 'suffix'),
|
2020-10-04 16:54:55 +00:00
|
|
|
* mainDictionary: (string),
|
2021-09-26 15:08:16 +00:00
|
|
|
* sortFrequencyDictionary: (null or string),
|
|
|
|
* sortFrequencyDictionaryOrder: (enum: 'ascending', 'descending'),
|
2021-04-29 01:17:05 +00:00
|
|
|
* removeNonJapaneseCharacters: (boolean),
|
2020-10-04 23:36:21 +00:00
|
|
|
* convertHalfWidthCharacters: (enum: 'false', 'true', 'variant'),
|
|
|
|
* convertNumericCharacters: (enum: 'false', 'true', 'variant'),
|
|
|
|
* convertAlphabeticCharacters: (enum: 'false', 'true', 'variant'),
|
|
|
|
* convertHiraganaToKatakana: (enum: 'false', 'true', 'variant'),
|
|
|
|
* convertKatakanaToHiragana: (enum: 'false', 'true', 'variant'),
|
|
|
|
* collapseEmphaticSequences: (enum: 'false', 'true', 'full'),
|
2021-01-03 17:12:55 +00:00
|
|
|
* textReplacements: [
|
|
|
|
* (null or [
|
|
|
|
* {pattern: (RegExp), replacement: (string)}
|
|
|
|
* ...
|
|
|
|
* ])
|
|
|
|
* ...
|
|
|
|
* ],
|
2020-10-04 16:54:55 +00:00
|
|
|
* enabledDictionaryMap: (Map of [
|
|
|
|
* (string),
|
|
|
|
* {
|
2021-03-06 18:04:50 +00:00
|
|
|
* index: (number),
|
|
|
|
* priority: (number),
|
2020-10-04 16:54:55 +00:00
|
|
|
* allowSecondarySearches: (boolean)
|
|
|
|
* }
|
2021-06-05 17:35:23 +00:00
|
|
|
* ]),
|
|
|
|
* excludeDictionaryDefinitions: (Set of (string) or null)
|
2020-10-04 16:54:55 +00:00
|
|
|
* }
|
2021-03-25 23:55:31 +00:00
|
|
|
* ```
|
|
|
|
* @returns An object of the structure `{dictionaryEntries, originalTextLength}`.
|
2020-10-04 16:54:55 +00:00
|
|
|
*/
|
|
|
|
async findTerms(mode, text, options) {
|
2021-09-26 15:08:16 +00:00
|
|
|
const {enabledDictionaryMap, excludeDictionaryDefinitions, sortFrequencyDictionary, sortFrequencyDictionaryOrder} = options;
|
2021-03-25 23:55:31 +00:00
|
|
|
let {dictionaryEntries, originalTextLength} = await this._findTermsInternal(text, enabledDictionaryMap, options);
|
|
|
|
|
2020-08-09 17:21:14 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 'group':
|
2021-03-25 23:55:31 +00:00
|
|
|
dictionaryEntries = this._groupDictionaryEntriesByHeadword(dictionaryEntries);
|
|
|
|
break;
|
2020-08-09 17:21:14 +00:00
|
|
|
case 'merge':
|
2021-03-25 23:55:31 +00:00
|
|
|
dictionaryEntries = await this._getRelatedDictionaryEntries(dictionaryEntries, options.mainDictionary, enabledDictionaryMap);
|
|
|
|
break;
|
2020-08-09 17:21:14 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
|
2021-06-05 17:35:23 +00:00
|
|
|
if (excludeDictionaryDefinitions !== null) {
|
|
|
|
this._removeExcludedDefinitions(dictionaryEntries, excludeDictionaryDefinitions);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
if (mode === 'simple') {
|
|
|
|
this._clearTermTags(dictionaryEntries);
|
|
|
|
} else {
|
|
|
|
await this._addTermMeta(dictionaryEntries, enabledDictionaryMap);
|
|
|
|
await this._expandTermTags(dictionaryEntries);
|
2021-06-26 16:37:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 15:08:16 +00:00
|
|
|
if (sortFrequencyDictionary !== null) {
|
|
|
|
this._updateSortFrequencies(dictionaryEntries, sortFrequencyDictionary, sortFrequencyDictionaryOrder === 'ascending');
|
|
|
|
}
|
2021-06-26 16:37:38 +00:00
|
|
|
if (dictionaryEntries.length > 1) {
|
|
|
|
this._sortTermDictionaryEntries(dictionaryEntries);
|
|
|
|
}
|
|
|
|
for (const {definitions, frequencies, pronunciations} of dictionaryEntries) {
|
|
|
|
this._flagRedundantDefinitionTags(definitions);
|
|
|
|
if (definitions.length > 1) { this._sortTermDictionaryEntryDefinitions(definitions); }
|
|
|
|
if (frequencies.length > 1) { this._sortTermDictionaryEntrySimpleData(frequencies); }
|
|
|
|
if (pronunciations.length > 1) { this._sortTermDictionaryEntrySimpleData(pronunciations); }
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {dictionaryEntries, originalTextLength};
|
2020-08-09 17:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 16:54:55 +00:00
|
|
|
/**
|
|
|
|
* Finds kanji definitions for the given text.
|
|
|
|
* @param text The text to find kanji definitions for. This string can be of any length,
|
|
|
|
* but is typically just one character, which is a single kanji. If the string is multiple
|
|
|
|
* characters long, each character will be searched in the database.
|
|
|
|
* @param options An object using the following structure:
|
|
|
|
* {
|
|
|
|
* enabledDictionaryMap: (Map of [
|
|
|
|
* (string),
|
|
|
|
* {
|
2021-03-06 18:04:50 +00:00
|
|
|
* index: (number),
|
|
|
|
* priority: (number)
|
2020-10-04 16:54:55 +00:00
|
|
|
* }
|
|
|
|
* ])
|
|
|
|
* }
|
|
|
|
* @returns An array of definitions. See the _createKanjiDefinition() function for structure details.
|
|
|
|
*/
|
2020-08-09 17:21:14 +00:00
|
|
|
async findKanji(text, options) {
|
2020-10-04 16:54:55 +00:00
|
|
|
const {enabledDictionaryMap} = options;
|
2020-08-09 17:21:14 +00:00
|
|
|
const kanjiUnique = new Set();
|
|
|
|
for (const c of text) {
|
|
|
|
kanjiUnique.add(c);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const databaseEntries = await this._database.findKanjiBulk([...kanjiUnique], enabledDictionaryMap);
|
|
|
|
if (databaseEntries.length === 0) { return []; }
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
this._sortDatabaseEntriesByIndex(databaseEntries);
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const dictionaryEntries = [];
|
2021-04-08 23:59:55 +00:00
|
|
|
for (const {character, onyomi, kunyomi, tags, definitions, stats, dictionary} of databaseEntries) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const expandedStats = await this._expandKanjiStats(stats, dictionary);
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const tagGroups = [];
|
|
|
|
if (tags.length > 0) { tagGroups.push(this._createTagGroup(dictionary, tags)); }
|
2021-03-02 03:17:23 +00:00
|
|
|
|
2021-04-08 23:59:55 +00:00
|
|
|
const dictionaryEntry = this._createKanjiDictionaryEntry(character, dictionary, onyomi, kunyomi, tagGroups, expandedStats, definitions);
|
2021-03-25 23:55:31 +00:00
|
|
|
dictionaryEntries.push(dictionaryEntry);
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
await this._addKanjiMeta(dictionaryEntries, enabledDictionaryMap);
|
|
|
|
await this._expandKanjiTags(dictionaryEntries);
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
this._sortKanjiDictionaryEntryData(dictionaryEntries);
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
return dictionaryEntries;
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 15:08:16 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of frequency information for a given list of term-reading pairs
|
|
|
|
* and a list of dictionaries.
|
|
|
|
* @param termReadingList An array of `{term, reading}` pairs. If reading is null,
|
|
|
|
* the reading won't be compared.
|
|
|
|
* @param dictionaries An array of dictionary names.
|
|
|
|
* @returns An array of objects with the format
|
|
|
|
* `{term, reading, dictionary, hasReading, frequency}`.
|
|
|
|
*/
|
|
|
|
async getTermFrequencies(termReadingList, dictionaries) {
|
|
|
|
const dictionarySet = new Set();
|
|
|
|
for (const dictionary of dictionaries) {
|
|
|
|
dictionarySet.add(dictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
const termList = termReadingList.map(({term}) => term);
|
|
|
|
const metas = await this._database.findTermMetaBulk(termList, dictionarySet);
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
for (const {mode, data, dictionary, index} of metas) {
|
|
|
|
if (mode !== 'freq') { continue; }
|
|
|
|
let {term, reading} = termReadingList[index];
|
|
|
|
let frequency = data;
|
|
|
|
const hasReading = (data !== null && typeof data === 'object');
|
|
|
|
if (hasReading) {
|
|
|
|
if (data.reading !== reading) {
|
|
|
|
if (reading !== null) { continue; }
|
|
|
|
reading = data.reading;
|
|
|
|
}
|
|
|
|
frequency = data.frequency;
|
|
|
|
}
|
|
|
|
results.push({
|
|
|
|
term,
|
|
|
|
reading,
|
|
|
|
dictionary,
|
|
|
|
hasReading,
|
|
|
|
frequency
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-10-04 23:33:22 +00:00
|
|
|
// Find terms internal implementation
|
|
|
|
|
|
|
|
async _findTermsInternal(text, enabledDictionaryMap, options) {
|
2021-04-29 01:17:05 +00:00
|
|
|
const {wildcard} = options;
|
|
|
|
if (options.removeNonJapaneseCharacters) {
|
|
|
|
text = this._getJapaneseOnlyText(text);
|
|
|
|
}
|
2020-10-04 23:33:22 +00:00
|
|
|
if (text.length === 0) {
|
2021-04-21 22:11:43 +00:00
|
|
|
return {dictionaryEntries: [], originalTextLength: 0};
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const deinflections = await (
|
2020-10-04 23:33:22 +00:00
|
|
|
wildcard ?
|
2021-03-25 23:55:31 +00:00
|
|
|
this._findTermsWildcard(text, enabledDictionaryMap, wildcard) :
|
|
|
|
this._findTermDeinflections(text, enabledDictionaryMap, options)
|
2020-10-04 23:33:22 +00:00
|
|
|
);
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
let originalTextLength = 0;
|
|
|
|
const dictionaryEntries = [];
|
|
|
|
const ids = new Set();
|
|
|
|
for (const {databaseEntries, originalText, transformedText, deinflectedText, reasons} of deinflections) {
|
|
|
|
if (databaseEntries.length === 0) { continue; }
|
|
|
|
originalTextLength = Math.max(originalTextLength, originalText.length);
|
|
|
|
for (const databaseEntry of databaseEntries) {
|
|
|
|
const {id} = databaseEntry;
|
|
|
|
if (ids.has(id)) { continue; }
|
|
|
|
const dictionaryEntry = this._createTermDictionaryEntryFromDatabaseEntry(databaseEntry, originalText, transformedText, deinflectedText, reasons, true, enabledDictionaryMap);
|
|
|
|
dictionaryEntries.push(dictionaryEntry);
|
|
|
|
ids.add(id);
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
return {dictionaryEntries, originalTextLength};
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
async _findTermsWildcard(text, enabledDictionaryMap, wildcard) {
|
|
|
|
const databaseEntries = await this._database.findTermsBulk([text], enabledDictionaryMap, wildcard);
|
|
|
|
return databaseEntries.length > 0 ? [this._createDeinflection(text, text, text, 0, [], databaseEntries)] : [];
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _findTermDeinflections(text, enabledDictionaryMap, options) {
|
|
|
|
const deinflections = this._getAllDeinflections(text, options);
|
|
|
|
|
|
|
|
if (deinflections.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const uniqueDeinflectionTerms = [];
|
|
|
|
const uniqueDeinflectionArrays = [];
|
|
|
|
const uniqueDeinflectionsMap = new Map();
|
|
|
|
for (const deinflection of deinflections) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const term = deinflection.deinflectedText;
|
2020-10-04 23:33:22 +00:00
|
|
|
let deinflectionArray = uniqueDeinflectionsMap.get(term);
|
|
|
|
if (typeof deinflectionArray === 'undefined') {
|
|
|
|
deinflectionArray = [];
|
|
|
|
uniqueDeinflectionTerms.push(term);
|
|
|
|
uniqueDeinflectionArrays.push(deinflectionArray);
|
|
|
|
uniqueDeinflectionsMap.set(term, deinflectionArray);
|
|
|
|
}
|
|
|
|
deinflectionArray.push(deinflection);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const databaseEntries = await this._database.findTermsBulk(uniqueDeinflectionTerms, enabledDictionaryMap, null);
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const databaseEntry of databaseEntries) {
|
|
|
|
const definitionRules = Deinflector.rulesToRuleFlags(databaseEntry.rules);
|
|
|
|
for (const deinflection of uniqueDeinflectionArrays[databaseEntry.index]) {
|
2020-10-04 23:33:22 +00:00
|
|
|
const deinflectionRules = deinflection.rules;
|
|
|
|
if (deinflectionRules === 0 || (definitionRules & deinflectionRules) !== 0) {
|
2021-03-25 23:55:31 +00:00
|
|
|
deinflection.databaseEntries.push(databaseEntry);
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return deinflections;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Deinflections and text transformations
|
|
|
|
|
2020-10-04 23:33:22 +00:00
|
|
|
_getAllDeinflections(text, options) {
|
|
|
|
const textOptionVariantArray = [
|
2021-01-03 17:12:55 +00:00
|
|
|
this._getTextReplacementsVariants(options),
|
2020-10-04 23:33:22 +00:00
|
|
|
this._getTextOptionEntryVariants(options.convertHalfWidthCharacters),
|
|
|
|
this._getTextOptionEntryVariants(options.convertNumericCharacters),
|
|
|
|
this._getTextOptionEntryVariants(options.convertAlphabeticCharacters),
|
|
|
|
this._getTextOptionEntryVariants(options.convertHiraganaToKatakana),
|
|
|
|
this._getTextOptionEntryVariants(options.convertKatakanaToHiragana),
|
2021-01-02 04:16:44 +00:00
|
|
|
this._getCollapseEmphaticOptions(options)
|
2020-10-04 23:33:22 +00:00
|
|
|
];
|
|
|
|
|
2020-11-29 18:09:02 +00:00
|
|
|
const jp = this._japaneseUtil;
|
2020-10-04 23:33:22 +00:00
|
|
|
const deinflections = [];
|
|
|
|
const used = new Set();
|
2021-01-03 17:12:55 +00:00
|
|
|
for (const [textReplacements, halfWidth, numeric, alphabetic, katakana, hiragana, [collapseEmphatic, collapseEmphaticFull]] of this._getArrayVariants(textOptionVariantArray)) {
|
2020-10-04 23:33:22 +00:00
|
|
|
let text2 = text;
|
|
|
|
const sourceMap = new TextSourceMap(text2);
|
2021-01-03 17:12:55 +00:00
|
|
|
if (textReplacements !== null) {
|
|
|
|
text2 = this._applyTextReplacements(text2, sourceMap, textReplacements);
|
|
|
|
}
|
2020-10-04 23:33:22 +00:00
|
|
|
if (halfWidth) {
|
|
|
|
text2 = jp.convertHalfWidthKanaToFullWidth(text2, sourceMap);
|
|
|
|
}
|
|
|
|
if (numeric) {
|
|
|
|
text2 = jp.convertNumericToFullWidth(text2);
|
|
|
|
}
|
|
|
|
if (alphabetic) {
|
|
|
|
text2 = jp.convertAlphabeticToKana(text2, sourceMap);
|
|
|
|
}
|
|
|
|
if (katakana) {
|
|
|
|
text2 = jp.convertHiraganaToKatakana(text2);
|
|
|
|
}
|
|
|
|
if (hiragana) {
|
|
|
|
text2 = jp.convertKatakanaToHiragana(text2);
|
|
|
|
}
|
|
|
|
if (collapseEmphatic) {
|
|
|
|
text2 = jp.collapseEmphaticSequences(text2, collapseEmphaticFull, sourceMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = text2.length; i > 0; --i) {
|
2021-03-07 19:07:26 +00:00
|
|
|
const source = text2.substring(0, i);
|
|
|
|
if (used.has(source)) { break; }
|
|
|
|
used.add(source);
|
2020-10-04 23:33:22 +00:00
|
|
|
const rawSource = sourceMap.source.substring(0, sourceMap.getSourceLength(i));
|
2021-03-07 19:07:26 +00:00
|
|
|
for (const {term, rules, reasons} of this._deinflector.deinflect(source)) {
|
2021-03-25 23:55:31 +00:00
|
|
|
deinflections.push(this._createDeinflection(rawSource, source, term, rules, reasons, []));
|
2020-10-04 23:33:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deinflections;
|
|
|
|
}
|
2020-08-09 17:21:14 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_applyTextReplacements(text, sourceMap, replacements) {
|
|
|
|
for (const {pattern, replacement} of replacements) {
|
|
|
|
text = RegexUtil.applyTextReplacement(text, sourceMap, pattern, replacement);
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2021-04-29 01:17:05 +00:00
|
|
|
_getJapaneseOnlyText(text) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const jp = this._japaneseUtil;
|
|
|
|
let length = 0;
|
|
|
|
for (const c of text) {
|
2021-04-29 01:17:05 +00:00
|
|
|
if (!jp.isCodePointJapanese(c.codePointAt(0))) {
|
|
|
|
return text.substring(0, length);
|
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
length += c.length;
|
|
|
|
}
|
2021-04-29 01:17:05 +00:00
|
|
|
return text;
|
2021-03-07 19:07:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_getTextOptionEntryVariants(value) {
|
|
|
|
switch (value) {
|
|
|
|
case 'true': return [true];
|
|
|
|
case 'variant': return [false, true];
|
|
|
|
default: return [false];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_getCollapseEmphaticOptions(options) {
|
|
|
|
const collapseEmphaticOptions = [[false, false]];
|
|
|
|
switch (options.collapseEmphaticSequences) {
|
|
|
|
case 'true':
|
|
|
|
collapseEmphaticOptions.push([true, false]);
|
|
|
|
break;
|
|
|
|
case 'full':
|
|
|
|
collapseEmphaticOptions.push([true, false], [true, true]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return collapseEmphaticOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTextReplacementsVariants(options) {
|
|
|
|
return options.textReplacements;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createDeinflection(originalText, transformedText, deinflectedText, rules, reasons, databaseEntries) {
|
|
|
|
return {originalText, transformedText, deinflectedText, rules, reasons, databaseEntries};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Term dictionary entry grouping
|
|
|
|
|
|
|
|
async _getRelatedDictionaryEntries(dictionaryEntries, mainDictionary, enabledDictionaryMap) {
|
2020-01-28 23:58:14 +00:00
|
|
|
const sequenceList = [];
|
2021-03-25 23:55:31 +00:00
|
|
|
const groupedDictionaryEntries = [];
|
|
|
|
const groupedDictionaryEntriesMap = new Map();
|
|
|
|
const ungroupedDictionaryEntriesMap = new Map();
|
|
|
|
for (const dictionaryEntry of dictionaryEntries) {
|
2021-06-26 15:57:09 +00:00
|
|
|
const {definitions: [{id, dictionary, sequences: [sequence]}]} = dictionaryEntry;
|
2020-10-02 21:59:14 +00:00
|
|
|
if (mainDictionary === dictionary && sequence >= 0) {
|
2021-03-25 23:55:31 +00:00
|
|
|
let group = groupedDictionaryEntriesMap.get(sequence);
|
|
|
|
if (typeof group === 'undefined') {
|
2021-04-03 19:41:44 +00:00
|
|
|
group = {
|
|
|
|
ids: new Set(),
|
|
|
|
dictionaryEntries: []
|
|
|
|
};
|
2021-03-25 23:55:31 +00:00
|
|
|
sequenceList.push({query: sequence, dictionary});
|
|
|
|
groupedDictionaryEntries.push(group);
|
|
|
|
groupedDictionaryEntriesMap.set(sequence, group);
|
2020-10-02 21:59:14 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
group.dictionaryEntries.push(dictionaryEntry);
|
|
|
|
group.ids.add(id);
|
2020-10-02 21:59:14 +00:00
|
|
|
} else {
|
2021-03-25 23:55:31 +00:00
|
|
|
ungroupedDictionaryEntriesMap.set(id, dictionaryEntry);
|
2020-10-02 21:59:14 +00:00
|
|
|
}
|
2020-01-28 23:58:14 +00:00
|
|
|
}
|
2019-10-19 15:34:12 +00:00
|
|
|
|
2020-10-04 15:12:15 +00:00
|
|
|
if (sequenceList.length > 0) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const secondarySearchDictionaryMap = this._getSecondarySearchDictionaryMap(enabledDictionaryMap);
|
2021-04-18 22:15:58 +00:00
|
|
|
await this._addRelatedDictionaryEntries(groupedDictionaryEntries, ungroupedDictionaryEntriesMap, sequenceList, enabledDictionaryMap);
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const group of groupedDictionaryEntries) {
|
|
|
|
this._sortTermDictionaryEntriesById(group.dictionaryEntries);
|
|
|
|
}
|
|
|
|
if (ungroupedDictionaryEntriesMap.size !== 0 || secondarySearchDictionaryMap.size !== 0) {
|
|
|
|
await this._addSecondaryRelatedDictionaryEntries(groupedDictionaryEntries, ungroupedDictionaryEntriesMap, enabledDictionaryMap, secondarySearchDictionaryMap);
|
|
|
|
}
|
2019-10-19 15:34:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const newDictionaryEntries = [];
|
|
|
|
for (const group of groupedDictionaryEntries) {
|
2021-04-18 22:11:09 +00:00
|
|
|
newDictionaryEntries.push(this._createGroupedDictionaryEntry(group.dictionaryEntries, true));
|
2020-10-05 02:04:44 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
newDictionaryEntries.push(...this._groupDictionaryEntriesByHeadword(ungroupedDictionaryEntriesMap.values()));
|
|
|
|
return newDictionaryEntries;
|
|
|
|
}
|
|
|
|
|
2021-04-18 22:15:58 +00:00
|
|
|
async _addRelatedDictionaryEntries(groupedDictionaryEntries, ungroupedDictionaryEntriesMap, sequenceList, enabledDictionaryMap) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const databaseEntries = await this._database.findTermsBySequenceBulk(sequenceList);
|
|
|
|
for (const databaseEntry of databaseEntries) {
|
|
|
|
const {dictionaryEntries, ids} = groupedDictionaryEntries[databaseEntry.index];
|
|
|
|
const {id} = databaseEntry;
|
|
|
|
if (ids.has(id)) { continue; }
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
const {term} = databaseEntry;
|
|
|
|
const dictionaryEntry = this._createTermDictionaryEntryFromDatabaseEntry(databaseEntry, term, term, term, [], false, enabledDictionaryMap);
|
2021-03-25 23:55:31 +00:00
|
|
|
dictionaryEntries.push(dictionaryEntry);
|
|
|
|
ids.add(id);
|
|
|
|
ungroupedDictionaryEntriesMap.delete(id);
|
2019-10-19 16:24:38 +00:00
|
|
|
}
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2019-10-19 16:24:38 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
async _addSecondaryRelatedDictionaryEntries(groupedDictionaryEntries, ungroupedDictionaryEntriesMap, enabledDictionaryMap, secondarySearchDictionaryMap) {
|
2021-03-02 03:17:23 +00:00
|
|
|
// Prepare grouping info
|
2021-03-15 02:51:20 +00:00
|
|
|
const termList = [];
|
2021-03-02 03:17:23 +00:00
|
|
|
const targetList = [];
|
|
|
|
const targetMap = new Map();
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const group of groupedDictionaryEntries) {
|
|
|
|
const {dictionaryEntries} = group;
|
|
|
|
for (const dictionaryEntry of dictionaryEntries) {
|
|
|
|
const {term, reading} = dictionaryEntry.headwords[0];
|
|
|
|
const key = this._createMapKey([term, reading]);
|
2021-03-02 03:17:23 +00:00
|
|
|
let target = targetMap.get(key);
|
|
|
|
if (typeof target === 'undefined') {
|
|
|
|
target = {
|
2021-06-06 03:17:13 +00:00
|
|
|
groups: []
|
2021-03-02 03:17:23 +00:00
|
|
|
};
|
|
|
|
targetMap.set(key, target);
|
2021-09-10 03:29:41 +00:00
|
|
|
termList.push({term, reading});
|
|
|
|
targetList.push(target);
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
target.groups.push(group);
|
2019-10-19 16:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
// Group unsequenced dictionary entries with sequenced entries that have a matching [term, reading].
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const [id, dictionaryEntry] of ungroupedDictionaryEntriesMap.entries()) {
|
|
|
|
const {term, reading} = dictionaryEntry.headwords[0];
|
|
|
|
const key = this._createMapKey([term, reading]);
|
2021-03-02 03:17:23 +00:00
|
|
|
const target = targetMap.get(key);
|
|
|
|
if (typeof target === 'undefined') { continue; }
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {ids, dictionaryEntries} of target.groups) {
|
|
|
|
if (ids.has(id)) { continue; }
|
|
|
|
dictionaryEntries.push(dictionaryEntry);
|
|
|
|
ids.add(id);
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2021-06-06 02:27:58 +00:00
|
|
|
ungroupedDictionaryEntriesMap.delete(id);
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search database for additional secondary terms
|
2021-03-15 02:51:20 +00:00
|
|
|
if (termList.length === 0 || secondarySearchDictionaryMap.size === 0) { return; }
|
2021-03-02 03:17:23 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const databaseEntries = await this._database.findTermsExactBulk(termList, secondarySearchDictionaryMap);
|
|
|
|
this._sortDatabaseEntriesByIndex(databaseEntries);
|
2019-10-19 16:42:26 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const databaseEntry of databaseEntries) {
|
|
|
|
const {index, id} = databaseEntry;
|
2021-04-04 20:22:35 +00:00
|
|
|
const sourceText = termList[index].term;
|
2021-03-02 03:17:23 +00:00
|
|
|
const target = targetList[index];
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {ids, dictionaryEntries} of target.groups) {
|
|
|
|
if (ids.has(id)) { continue; }
|
2021-03-02 03:17:23 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const dictionaryEntry = this._createTermDictionaryEntryFromDatabaseEntry(databaseEntry, sourceText, sourceText, sourceText, [], false, enabledDictionaryMap);
|
|
|
|
dictionaryEntries.push(dictionaryEntry);
|
|
|
|
ids.add(id);
|
|
|
|
ungroupedDictionaryEntriesMap.delete(id);
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2019-10-19 16:42:26 +00:00
|
|
|
}
|
2019-10-19 16:24:38 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_groupDictionaryEntriesByHeadword(dictionaryEntries) {
|
|
|
|
const groups = new Map();
|
|
|
|
for (const dictionaryEntry of dictionaryEntries) {
|
|
|
|
const {inflections, headwords: [{term, reading}]} = dictionaryEntry;
|
|
|
|
const key = this._createMapKey([term, reading, ...inflections]);
|
2021-04-18 22:15:58 +00:00
|
|
|
let groupDictionaryEntries = groups.get(key);
|
|
|
|
if (typeof groupDictionaryEntries === 'undefined') {
|
|
|
|
groupDictionaryEntries = [];
|
|
|
|
groups.set(key, groupDictionaryEntries);
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2021-04-18 22:15:58 +00:00
|
|
|
groupDictionaryEntries.push(dictionaryEntry);
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2017-10-03 04:20:02 +00:00
|
|
|
|
2021-04-18 22:15:58 +00:00
|
|
|
const newDictionaryEntries = [];
|
|
|
|
for (const groupDictionaryEntries of groups.values()) {
|
|
|
|
newDictionaryEntries.push(this._createGroupedDictionaryEntry(groupDictionaryEntries, false));
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2021-04-18 22:15:58 +00:00
|
|
|
return newDictionaryEntries;
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2021-03-02 03:17:23 +00:00
|
|
|
|
2021-06-06 03:17:13 +00:00
|
|
|
// Removing data
|
|
|
|
|
2021-06-05 17:35:23 +00:00
|
|
|
_removeExcludedDefinitions(dictionaryEntries, excludeDictionaryDefinitions) {
|
|
|
|
for (let i = dictionaryEntries.length - 1; i >= 0; --i) {
|
|
|
|
const dictionaryEntry = dictionaryEntries[i];
|
|
|
|
const {definitions, pronunciations, frequencies, headwords} = dictionaryEntry;
|
|
|
|
const definitionsChanged = this._removeArrayItemsWithDictionary(definitions, excludeDictionaryDefinitions);
|
|
|
|
this._removeArrayItemsWithDictionary(pronunciations, excludeDictionaryDefinitions);
|
|
|
|
this._removeArrayItemsWithDictionary(frequencies, excludeDictionaryDefinitions);
|
|
|
|
this._removeTagGroupsWithDictionary(definitions, excludeDictionaryDefinitions);
|
|
|
|
this._removeTagGroupsWithDictionary(headwords, excludeDictionaryDefinitions);
|
|
|
|
|
|
|
|
if (!definitionsChanged) { continue; }
|
|
|
|
|
|
|
|
if (definitions.length === 0) {
|
|
|
|
dictionaryEntries.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
this._removeUnusedHeadwords(dictionaryEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeUnusedHeadwords(dictionaryEntry) {
|
|
|
|
const {definitions, pronunciations, frequencies, headwords} = dictionaryEntry;
|
|
|
|
const removeHeadwordIndices = new Set();
|
|
|
|
for (let i = 0, ii = headwords.length; i < ii; ++i) {
|
|
|
|
removeHeadwordIndices.add(i);
|
|
|
|
}
|
|
|
|
for (const {headwordIndices} of definitions) {
|
|
|
|
for (const headwordIndex of headwordIndices) {
|
|
|
|
removeHeadwordIndices.delete(headwordIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removeHeadwordIndices.size === 0) { return; }
|
|
|
|
|
|
|
|
const indexRemap = new Map();
|
|
|
|
let oldIndex = 0;
|
|
|
|
for (let i = 0, ii = headwords.length; i < ii; ++i) {
|
2021-09-06 01:07:23 +00:00
|
|
|
if (removeHeadwordIndices.has(oldIndex)) {
|
2021-06-05 17:35:23 +00:00
|
|
|
headwords.splice(i, 1);
|
|
|
|
--i;
|
|
|
|
--ii;
|
|
|
|
} else {
|
|
|
|
indexRemap.set(oldIndex, indexRemap.size);
|
|
|
|
}
|
|
|
|
++oldIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateDefinitionHeadwordIndices(definitions, indexRemap);
|
|
|
|
this._updateArrayItemsHeadwordIndex(pronunciations, indexRemap);
|
|
|
|
this._updateArrayItemsHeadwordIndex(frequencies, indexRemap);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateDefinitionHeadwordIndices(definitions, indexRemap) {
|
|
|
|
for (const {headwordIndices} of definitions) {
|
|
|
|
for (let i = headwordIndices.length - 1; i >= 0; --i) {
|
|
|
|
const newHeadwordIndex = indexRemap.get(headwordIndices[i]);
|
|
|
|
if (typeof newHeadwordIndex === 'undefined') {
|
|
|
|
headwordIndices.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
headwordIndices[i] = newHeadwordIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateArrayItemsHeadwordIndex(array, indexRemap) {
|
|
|
|
for (let i = array.length - 1; i >= 0; --i) {
|
|
|
|
const item = array[i];
|
|
|
|
const {headwordIndex} = item;
|
|
|
|
const newHeadwordIndex = indexRemap.get(headwordIndex);
|
|
|
|
if (typeof newHeadwordIndex === 'undefined') {
|
|
|
|
array.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
item.headwordIndex = newHeadwordIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeArrayItemsWithDictionary(array, excludeDictionaryDefinitions) {
|
|
|
|
let changed = false;
|
|
|
|
for (let j = array.length - 1; j >= 0; --j) {
|
|
|
|
const {dictionary} = array[j];
|
|
|
|
if (!excludeDictionaryDefinitions.has(dictionary)) { continue; }
|
|
|
|
array.splice(j, 1);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeTagGroupsWithDictionary(array, excludeDictionaryDefinitions) {
|
|
|
|
for (const {tags} of array) {
|
|
|
|
this._removeArrayItemsWithDictionary(tags, excludeDictionaryDefinitions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Tags
|
|
|
|
|
|
|
|
_getTermTagTargets(dictionaryEntries) {
|
|
|
|
const tagTargets = [];
|
|
|
|
for (const {headwords, definitions, pronunciations} of dictionaryEntries) {
|
|
|
|
this._addTagExpansionTargets(tagTargets, headwords);
|
|
|
|
this._addTagExpansionTargets(tagTargets, definitions);
|
|
|
|
for (const {pitches} of pronunciations) {
|
|
|
|
this._addTagExpansionTargets(tagTargets, pitches);
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
return tagTargets;
|
|
|
|
}
|
|
|
|
|
|
|
|
_clearTermTags(dictionaryEntries) {
|
|
|
|
this._getTermTagTargets(dictionaryEntries);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _expandTermTags(dictionaryEntries) {
|
|
|
|
const tagTargets = this._getTermTagTargets(dictionaryEntries);
|
|
|
|
await this._expandTagGroups(tagTargets);
|
|
|
|
this._groupTags(tagTargets);
|
|
|
|
}
|
2021-03-02 03:17:23 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
async _expandKanjiTags(dictionaryEntries) {
|
|
|
|
const tagTargets = [];
|
|
|
|
this._addTagExpansionTargets(tagTargets, dictionaryEntries);
|
|
|
|
await this._expandTagGroups(tagTargets);
|
|
|
|
this._groupTags(tagTargets);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _expandTagGroups(tagTargets) {
|
|
|
|
const allItems = [];
|
|
|
|
const targetMap = new Map();
|
|
|
|
for (const {tagGroups, tags} of tagTargets) {
|
|
|
|
for (const {dictionary, tagNames} of tagGroups) {
|
|
|
|
let dictionaryItems = targetMap.get(dictionary);
|
|
|
|
if (typeof dictionaryItems === 'undefined') {
|
|
|
|
dictionaryItems = new Map();
|
|
|
|
targetMap.set(dictionary, dictionaryItems);
|
|
|
|
}
|
|
|
|
for (const tagName of tagNames) {
|
|
|
|
let item = dictionaryItems.get(tagName);
|
|
|
|
if (typeof item === 'undefined') {
|
|
|
|
const query = this._getNameBase(tagName);
|
|
|
|
item = {query, dictionary, tagName, cache: null, databaseTag: null, targets: []};
|
|
|
|
dictionaryItems.set(tagName, item);
|
|
|
|
allItems.push(item);
|
|
|
|
}
|
|
|
|
item.targets.push(tags);
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 16:16:38 +00:00
|
|
|
}
|
2017-10-03 04:20:02 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const nonCachedItems = [];
|
|
|
|
const tagCache = this._tagCache;
|
|
|
|
for (const [dictionary, dictionaryItems] of targetMap.entries()) {
|
|
|
|
let cache = tagCache.get(dictionary);
|
|
|
|
if (typeof cache === 'undefined') {
|
|
|
|
cache = new Map();
|
|
|
|
tagCache.set(dictionary, cache);
|
|
|
|
}
|
|
|
|
for (const item of dictionaryItems.values()) {
|
|
|
|
const databaseTag = cache.get(item.query);
|
|
|
|
if (typeof databaseTag !== 'undefined') {
|
|
|
|
item.databaseTag = databaseTag;
|
|
|
|
} else {
|
|
|
|
item.cache = cache;
|
|
|
|
nonCachedItems.push(item);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const nonCachedItemCount = nonCachedItems.length;
|
|
|
|
if (nonCachedItemCount > 0) {
|
|
|
|
const databaseTags = await this._database.findTagMetaBulk(nonCachedItems);
|
|
|
|
for (let i = 0; i < nonCachedItemCount; ++i) {
|
|
|
|
const item = nonCachedItems[i];
|
|
|
|
let databaseTag = databaseTags[i];
|
|
|
|
if (typeof databaseTag === 'undefined') { databaseTag = null; }
|
|
|
|
item.databaseTag = databaseTag;
|
|
|
|
item.cache.set(item.query, databaseTag);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 15:12:15 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {dictionary, tagName, databaseTag, targets} of allItems) {
|
|
|
|
for (const tags of targets) {
|
|
|
|
tags.push(this._createTag(databaseTag, tagName, dictionary));
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_groupTags(tagTargets) {
|
|
|
|
const stringComparer = this._stringComparer;
|
|
|
|
const compare = (v1, v2) => {
|
|
|
|
const i = v1.order - v2.order;
|
|
|
|
return i !== 0 ? i : stringComparer.compare(v1.name, v2.name);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const {tags} of tagTargets) {
|
|
|
|
if (tags.length <= 1) { continue; }
|
|
|
|
this._mergeSimilarTags(tags);
|
|
|
|
tags.sort(compare);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_addTagExpansionTargets(tagTargets, objects) {
|
|
|
|
for (const value of objects) {
|
|
|
|
const tagGroups = value.tags;
|
|
|
|
if (tagGroups.length === 0) { continue; }
|
|
|
|
const tags = [];
|
|
|
|
value.tags = tags;
|
|
|
|
tagTargets.push({tagGroups, tags});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_mergeSimilarTags(tags) {
|
|
|
|
let tagCount = tags.length;
|
|
|
|
for (let i = 0; i < tagCount; ++i) {
|
|
|
|
const tag1 = tags[i];
|
|
|
|
const {category, name} = tag1;
|
|
|
|
for (let j = i + 1; j < tagCount; ++j) {
|
|
|
|
const tag2 = tags[j];
|
|
|
|
if (tag2.name !== name || tag2.category !== category) { continue; }
|
|
|
|
// Merge tag
|
|
|
|
tag1.order = Math.min(tag1.order, tag2.order);
|
|
|
|
tag1.score = Math.max(tag1.score, tag2.score);
|
|
|
|
tag1.dictionaries.push(...tag2.dictionaries);
|
2021-05-08 17:16:56 +00:00
|
|
|
this._addUniqueSimple(tag1.content, tag2.content);
|
2021-03-25 23:55:31 +00:00
|
|
|
tags.splice(j, 1);
|
|
|
|
--tagCount;
|
|
|
|
--j;
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_getTagNamesWithCategory(tags, category) {
|
|
|
|
const results = [];
|
|
|
|
for (const tag of tags) {
|
|
|
|
if (tag.category !== category) { continue; }
|
|
|
|
results.push(tag.name);
|
|
|
|
}
|
|
|
|
results.sort();
|
|
|
|
return results;
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
2017-10-01 01:17:02 +00:00
|
|
|
|
2020-11-13 01:34:11 +00:00
|
|
|
_flagRedundantDefinitionTags(definitions) {
|
2021-03-25 23:55:31 +00:00
|
|
|
if (definitions.length === 0) { return; }
|
|
|
|
|
2021-01-29 02:33:30 +00:00
|
|
|
let lastDictionary = null;
|
2020-10-04 23:33:22 +00:00
|
|
|
let lastPartOfSpeech = '';
|
|
|
|
const removeCategoriesSet = new Set();
|
2017-04-22 20:02:06 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {dictionary, tags} of definitions) {
|
|
|
|
const partOfSpeech = this._createMapKey(this._getTagNamesWithCategory(tags, 'partOfSpeech'));
|
2016-03-21 03:34:50 +00:00
|
|
|
|
2021-01-29 02:33:30 +00:00
|
|
|
if (lastDictionary !== dictionary) {
|
2020-10-04 23:33:22 +00:00
|
|
|
lastDictionary = dictionary;
|
|
|
|
lastPartOfSpeech = '';
|
2017-01-08 19:18:55 +00:00
|
|
|
}
|
2017-07-10 21:10:58 +00:00
|
|
|
|
2020-10-04 23:33:22 +00:00
|
|
|
if (lastPartOfSpeech === partOfSpeech) {
|
|
|
|
removeCategoriesSet.add('partOfSpeech');
|
|
|
|
} else {
|
|
|
|
lastPartOfSpeech = partOfSpeech;
|
|
|
|
}
|
2016-03-21 01:45:37 +00:00
|
|
|
|
2020-10-04 23:33:22 +00:00
|
|
|
if (removeCategoriesSet.size > 0) {
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const tag of tags) {
|
|
|
|
if (removeCategoriesSet.has(tag.category)) {
|
|
|
|
tag.redundant = true;
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 23:33:22 +00:00
|
|
|
removeCategoriesSet.clear();
|
|
|
|
}
|
2019-11-05 01:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Metadata
|
2019-12-22 19:07:30 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
async _addTermMeta(dictionaryEntries, enabledDictionaryMap) {
|
|
|
|
const headwordMap = new Map();
|
|
|
|
const headwordMapKeys = [];
|
|
|
|
const headwordReadingMaps = [];
|
2021-02-28 03:27:00 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {headwords, pronunciations, frequencies} of dictionaryEntries) {
|
|
|
|
for (let i = 0, ii = headwords.length; i < ii; ++i) {
|
|
|
|
const {term, reading} = headwords[i];
|
|
|
|
let readingMap = headwordMap.get(term);
|
2021-02-28 03:27:00 +00:00
|
|
|
if (typeof readingMap === 'undefined') {
|
|
|
|
readingMap = new Map();
|
2021-03-25 23:55:31 +00:00
|
|
|
headwordMap.set(term, readingMap);
|
|
|
|
headwordMapKeys.push(term);
|
|
|
|
headwordReadingMaps.push(readingMap);
|
2020-11-02 02:24:35 +00:00
|
|
|
}
|
2021-02-28 03:27:00 +00:00
|
|
|
let targets = readingMap.get(reading);
|
|
|
|
if (typeof targets === 'undefined') {
|
|
|
|
targets = [];
|
|
|
|
readingMap.set(reading, targets);
|
2020-11-02 02:24:35 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
targets.push({headwordIndex: i, pronunciations, frequencies});
|
2019-08-31 01:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const metas = await this._database.findTermMetaBulk(headwordMapKeys, enabledDictionaryMap);
|
|
|
|
for (const {mode, data, dictionary, index} of metas) {
|
|
|
|
const {index: dictionaryIndex, priority: dictionaryPriority} = this._getDictionaryOrder(dictionary, enabledDictionaryMap);
|
|
|
|
const map2 = headwordReadingMaps[index];
|
2021-02-28 03:27:00 +00:00
|
|
|
for (const [reading, targets] of map2.entries()) {
|
2020-11-02 02:24:35 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 'freq':
|
|
|
|
{
|
2021-02-28 03:27:00 +00:00
|
|
|
let frequency = data;
|
2021-10-15 01:26:53 +00:00
|
|
|
const hasReading = (data !== null && typeof data === 'object' && typeof data.reading === 'string');
|
2021-02-28 03:27:00 +00:00
|
|
|
if (hasReading) {
|
|
|
|
if (data.reading !== reading) { continue; }
|
|
|
|
frequency = data.frequency;
|
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {frequencies, headwordIndex} of targets) {
|
2021-10-15 01:26:53 +00:00
|
|
|
let displayValue;
|
|
|
|
let displayValueParsed;
|
|
|
|
({frequency, displayValue, displayValueParsed} = this._getFrequencyInfo(frequency));
|
2021-03-25 23:55:31 +00:00
|
|
|
frequencies.push(this._createTermFrequency(
|
|
|
|
frequencies.length,
|
|
|
|
headwordIndex,
|
|
|
|
dictionary,
|
|
|
|
dictionaryIndex,
|
|
|
|
dictionaryPriority,
|
|
|
|
hasReading,
|
2021-10-15 01:26:53 +00:00
|
|
|
frequency,
|
|
|
|
displayValue,
|
|
|
|
displayValueParsed
|
2021-03-25 23:55:31 +00:00
|
|
|
));
|
2021-02-28 03:27:00 +00:00
|
|
|
}
|
2020-11-02 02:24:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'pitch':
|
|
|
|
{
|
2021-02-28 03:27:00 +00:00
|
|
|
if (data.reading !== reading) { continue; }
|
2021-03-25 23:55:31 +00:00
|
|
|
const pitches = [];
|
2021-07-16 02:39:33 +00:00
|
|
|
for (const {position, tags, nasal, devoice} of data.pitches) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const tags2 = [];
|
|
|
|
if (Array.isArray(tags) && tags.length > 0) {
|
|
|
|
tags2.push(this._createTagGroup(dictionary, tags));
|
|
|
|
}
|
2021-07-16 02:39:33 +00:00
|
|
|
const nasalPositions = this._toNumberArray(nasal);
|
|
|
|
const devoicePositions = this._toNumberArray(devoice);
|
|
|
|
pitches.push({position, nasalPositions, devoicePositions, tags: tags2});
|
2021-02-28 03:27:00 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {pronunciations, headwordIndex} of targets) {
|
|
|
|
pronunciations.push(this._createTermPronunciation(
|
|
|
|
pronunciations.length,
|
|
|
|
headwordIndex,
|
|
|
|
dictionary,
|
|
|
|
dictionaryIndex,
|
|
|
|
dictionaryPriority,
|
|
|
|
pitches
|
|
|
|
));
|
2021-02-28 03:27:00 +00:00
|
|
|
}
|
2020-11-02 02:24:35 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-09-23 15:46:34 +00:00
|
|
|
}
|
2017-09-14 06:22:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
async _addKanjiMeta(dictionaryEntries, enabledDictionaryMap) {
|
2020-01-25 03:27:25 +00:00
|
|
|
const kanjiList = [];
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {character} of dictionaryEntries) {
|
2020-10-02 21:59:14 +00:00
|
|
|
kanjiList.push(character);
|
2020-01-25 03:27:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 15:12:15 +00:00
|
|
|
const metas = await this._database.findKanjiMetaBulk(kanjiList, enabledDictionaryMap);
|
2020-01-25 03:27:25 +00:00
|
|
|
for (const {character, mode, data, dictionary, index} of metas) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const {index: dictionaryIndex, priority: dictionaryPriority} = this._getDictionaryOrder(dictionary, enabledDictionaryMap);
|
2020-01-25 03:27:25 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 'freq':
|
2020-12-30 20:12:08 +00:00
|
|
|
{
|
2021-03-25 23:55:31 +00:00
|
|
|
const {frequencies} = dictionaryEntries[index];
|
2021-10-15 01:26:53 +00:00
|
|
|
const {frequency, displayValue, displayValueParsed} = this._getFrequencyInfo(data);
|
2021-03-25 23:55:31 +00:00
|
|
|
frequencies.push(this._createKanjiFrequency(
|
|
|
|
frequencies.length,
|
|
|
|
dictionary,
|
|
|
|
dictionaryIndex,
|
|
|
|
dictionaryPriority,
|
|
|
|
character,
|
2021-10-15 01:26:53 +00:00
|
|
|
frequency,
|
|
|
|
displayValue,
|
|
|
|
displayValueParsed
|
2021-03-25 23:55:31 +00:00
|
|
|
));
|
2020-12-30 20:12:08 +00:00
|
|
|
}
|
2020-01-25 03:27:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
async _expandKanjiStats(stats, dictionary) {
|
|
|
|
const statsEntries = Object.entries(stats);
|
|
|
|
const items = [];
|
|
|
|
for (const [name] of statsEntries) {
|
|
|
|
const query = this._getNameBase(name);
|
|
|
|
items.push({query, dictionary});
|
2020-10-02 21:59:14 +00:00
|
|
|
}
|
2017-09-18 02:16:08 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const databaseInfos = await this._database.findTagMetaBulk(items);
|
2017-09-18 02:16:08 +00:00
|
|
|
|
2020-02-15 18:12:03 +00:00
|
|
|
const statsGroups = new Map();
|
2021-03-25 23:55:31 +00:00
|
|
|
for (let i = 0, ii = statsEntries.length; i < ii; ++i) {
|
|
|
|
const databaseInfo = databaseInfos[i];
|
|
|
|
if (databaseInfo === null) { continue; }
|
2019-10-19 17:32:05 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const [name, value] = statsEntries[i];
|
|
|
|
const {category} = databaseInfo;
|
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
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
group.push(this._createKanjiStat(name, value, databaseInfo, dictionary));
|
2017-09-18 02:16:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const groupedStats = {};
|
2020-02-15 18:12:03 +00:00
|
|
|
for (const [category, group] of statsGroups.entries()) {
|
2020-10-02 21:59:14 +00:00
|
|
|
this._sortKanjiStats(group);
|
2021-03-25 23:55:31 +00:00
|
|
|
groupedStats[category] = group;
|
2017-09-22 06:20:51 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
return groupedStats;
|
2017-09-18 02:16:08 +00:00
|
|
|
}
|
2019-08-11 18:12:01 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_sortKanjiStats(stats) {
|
|
|
|
if (stats.length <= 1) { return; }
|
|
|
|
const stringComparer = this._stringComparer;
|
|
|
|
stats.sort((v1, v2) => {
|
|
|
|
const i = v1.order - v2.order;
|
|
|
|
return (i !== 0) ? i : stringComparer.compare(v1.content, v2.content);
|
|
|
|
});
|
2019-10-19 17:32:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 01:26:53 +00:00
|
|
|
_convertStringToNumber(value) {
|
|
|
|
const match = this._numberRegex.exec(value);
|
|
|
|
if (match === null) { return 0; }
|
|
|
|
value = Number.parseFloat(match[0]);
|
|
|
|
return Number.isFinite(value) ? value : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getFrequencyInfo(frequency) {
|
|
|
|
let displayValue = null;
|
|
|
|
let displayValueParsed = false;
|
|
|
|
if (typeof frequency === 'object' && frequency !== null) {
|
|
|
|
({value: frequency, displayValue} = frequency);
|
|
|
|
if (typeof frequency !== 'number') { frequency = 0; }
|
|
|
|
if (typeof displayValue !== 'string') { displayValue = null; }
|
|
|
|
} else {
|
|
|
|
switch (typeof frequency) {
|
|
|
|
case 'number':
|
|
|
|
// No change
|
|
|
|
break;
|
|
|
|
case 'string':
|
|
|
|
displayValue = frequency;
|
|
|
|
displayValueParsed = true;
|
|
|
|
frequency = this._convertStringToNumber(frequency);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
frequency = 0;
|
|
|
|
break;
|
|
|
|
}
|
2021-09-26 15:08:21 +00:00
|
|
|
}
|
2021-10-15 01:26:53 +00:00
|
|
|
return {frequency, displayValue, displayValueParsed};
|
2021-09-26 15:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Helpers
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2020-08-09 17:21:14 +00:00
|
|
|
_getNameBase(name) {
|
2019-08-11 18:12:01 +00:00
|
|
|
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
|
|
|
|
2020-10-04 15:12:15 +00:00
|
|
|
_getSecondarySearchDictionaryMap(enabledDictionaryMap) {
|
|
|
|
const secondarySearchDictionaryMap = new Map();
|
2020-10-07 00:28:49 +00:00
|
|
|
for (const [dictionary, details] of enabledDictionaryMap.entries()) {
|
|
|
|
if (!details.allowSecondarySearches) { continue; }
|
|
|
|
secondarySearchDictionaryMap.set(dictionary, details);
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
return secondarySearchDictionaryMap;
|
|
|
|
}
|
|
|
|
|
2021-02-28 04:11:41 +00:00
|
|
|
_getDictionaryOrder(dictionary, enabledDictionaryMap) {
|
2020-10-04 15:12:15 +00:00
|
|
|
const info = enabledDictionaryMap.get(dictionary);
|
2021-03-06 18:04:50 +00:00
|
|
|
const {index, priority} = typeof info !== 'undefined' ? info : {index: enabledDictionaryMap.size, priority: 0};
|
|
|
|
return {index, priority};
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 23:33:22 +00:00
|
|
|
*_getArrayVariants(arrayVariants) {
|
|
|
|
const ii = arrayVariants.length;
|
2020-08-10 01:04:09 +00:00
|
|
|
|
2020-10-04 23:33:22 +00:00
|
|
|
let total = 1;
|
|
|
|
for (let i = 0; i < ii; ++i) {
|
|
|
|
total *= arrayVariants[i].length;
|
2020-08-10 01:04:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 23:33:22 +00:00
|
|
|
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;
|
2020-08-10 01:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 15:12:15 +00:00
|
|
|
_createMapKey(array) {
|
|
|
|
return JSON.stringify(array);
|
2020-08-10 01:04:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 02:39:33 +00:00
|
|
|
_toNumberArray(value) {
|
|
|
|
return Array.isArray(value) ? value : (typeof value === 'number' ? [value] : []);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Kanji data
|
2020-08-10 01:04:09 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_createKanjiStat(name, value, databaseInfo, dictionary) {
|
|
|
|
const {category, notes, order, score} = databaseInfo;
|
2020-10-02 21:59:14 +00:00
|
|
|
return {
|
|
|
|
name,
|
|
|
|
category: (typeof category === 'string' && category.length > 0 ? category : 'default'),
|
2021-03-25 23:55:31 +00:00
|
|
|
content: (typeof notes === 'string' ? notes : ''),
|
2020-10-02 21:59:14 +00:00
|
|
|
order: (typeof order === 'number' ? order : 0),
|
|
|
|
score: (typeof score === 'number' ? score : 0),
|
|
|
|
dictionary: (typeof dictionary === 'string' ? dictionary : null),
|
|
|
|
value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-15 01:26:53 +00:00
|
|
|
_createKanjiFrequency(index, dictionary, dictionaryIndex, dictionaryPriority, character, frequency, displayValue, displayValueParsed) {
|
|
|
|
return {index, dictionary, dictionaryIndex, dictionaryPriority, character, frequency, displayValue, displayValueParsed};
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_createKanjiDictionaryEntry(character, dictionary, onyomi, kunyomi, tags, stats, definitions) {
|
2020-10-04 15:12:15 +00:00
|
|
|
return {
|
|
|
|
type: 'kanji',
|
|
|
|
character,
|
|
|
|
dictionary,
|
|
|
|
onyomi,
|
|
|
|
kunyomi,
|
|
|
|
tags,
|
|
|
|
stats,
|
2021-03-25 23:55:31 +00:00
|
|
|
definitions,
|
2020-10-04 15:12:15 +00:00
|
|
|
frequencies: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Term data
|
2020-10-02 21:59:14 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_createTag(databaseTag, name, dictionary) {
|
|
|
|
const {category, notes, order, score} = (databaseTag !== null ? databaseTag : {});
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
category: (typeof category === 'string' && category.length > 0 ? category : 'default'),
|
|
|
|
order: (typeof order === 'number' ? order : 0),
|
|
|
|
score: (typeof score === 'number' ? score : 0),
|
|
|
|
content: (typeof notes === 'string' && notes.length > 0 ? [notes] : []),
|
|
|
|
dictionaries: [dictionary],
|
|
|
|
redundant: false
|
|
|
|
};
|
|
|
|
}
|
2020-10-02 21:59:14 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_createTagGroup(dictionary, tagNames) {
|
|
|
|
return {dictionary, tagNames};
|
|
|
|
}
|
|
|
|
|
|
|
|
_createSource(originalText, transformedText, deinflectedText, isPrimary) {
|
|
|
|
return {originalText, transformedText, deinflectedText, isPrimary};
|
|
|
|
}
|
|
|
|
|
2021-03-26 23:50:54 +00:00
|
|
|
_createTermHeadword(index, term, reading, sources, tags, wordClasses) {
|
|
|
|
return {index, term, reading, sources, tags, wordClasses};
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 15:57:09 +00:00
|
|
|
_createTermDefinition(index, headwordIndices, dictionary, dictionaryIndex, dictionaryPriority, id, score, sequences, isPrimary, tags, entries) {
|
2021-09-26 15:08:16 +00:00
|
|
|
return {
|
|
|
|
index,
|
|
|
|
headwordIndices,
|
|
|
|
dictionary,
|
|
|
|
dictionaryIndex,
|
|
|
|
dictionaryPriority,
|
|
|
|
id,
|
|
|
|
score,
|
|
|
|
frequencyOrder: 0,
|
|
|
|
sequences,
|
|
|
|
isPrimary,
|
|
|
|
tags,
|
|
|
|
entries
|
|
|
|
};
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2020-10-02 21:59:14 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_createTermPronunciation(index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches) {
|
|
|
|
return {index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, pitches};
|
|
|
|
}
|
|
|
|
|
2021-10-15 01:26:53 +00:00
|
|
|
_createTermFrequency(index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency, displayValue, displayValueParsed) {
|
|
|
|
return {index, headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency, displayValue, displayValueParsed};
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 15:57:09 +00:00
|
|
|
_createTermDictionaryEntry(isPrimary, inflections, score, dictionaryIndex, dictionaryPriority, sourceTermExactMatchCount, maxTransformedTextLength, headwords, definitions) {
|
2020-10-02 21:59:14 +00:00
|
|
|
return {
|
2020-10-04 15:12:15 +00:00
|
|
|
type: 'term',
|
2021-03-02 00:01:30 +00:00
|
|
|
isPrimary,
|
2021-03-25 23:55:31 +00:00
|
|
|
inflections,
|
2020-10-04 15:12:15 +00:00
|
|
|
score,
|
2021-09-26 15:08:16 +00:00
|
|
|
frequencyOrder: 0,
|
2021-03-25 23:55:31 +00:00
|
|
|
dictionaryIndex,
|
|
|
|
dictionaryPriority,
|
|
|
|
sourceTermExactMatchCount,
|
2021-03-26 01:40:27 +00:00
|
|
|
maxTransformedTextLength,
|
2021-03-25 23:55:31 +00:00
|
|
|
headwords,
|
|
|
|
definitions,
|
|
|
|
pronunciations: [],
|
|
|
|
frequencies: []
|
2020-10-04 15:12:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_createTermDictionaryEntryFromDatabaseEntry(databaseEntry, originalText, transformedText, deinflectedText, reasons, isPrimary, enabledDictionaryMap) {
|
2021-04-18 22:08:02 +00:00
|
|
|
const {term, reading: rawReading, definitionTags, termTags, definitions, score, dictionary, id, sequence: rawSequence, rules} = databaseEntry;
|
2021-04-04 20:22:35 +00:00
|
|
|
const reading = (rawReading.length > 0 ? rawReading : term);
|
2021-03-25 23:55:31 +00:00
|
|
|
const {index: dictionaryIndex, priority: dictionaryPriority} = this._getDictionaryOrder(dictionary, enabledDictionaryMap);
|
2021-04-04 20:22:35 +00:00
|
|
|
const sourceTermExactMatchCount = (isPrimary && deinflectedText === term ? 1 : 0);
|
2021-03-25 23:55:31 +00:00
|
|
|
const source = this._createSource(originalText, transformedText, deinflectedText, isPrimary);
|
2021-03-26 01:40:27 +00:00
|
|
|
const maxTransformedTextLength = transformedText.length;
|
2021-04-18 22:08:02 +00:00
|
|
|
const hasSequence = (rawSequence >= 0);
|
|
|
|
const sequence = hasSequence ? rawSequence : -1;
|
2021-03-25 23:55:31 +00:00
|
|
|
|
|
|
|
const headwordTagGroups = [];
|
|
|
|
const definitionTagGroups = [];
|
|
|
|
if (termTags.length > 0) { headwordTagGroups.push(this._createTagGroup(dictionary, termTags)); }
|
|
|
|
if (definitionTags.length > 0) { definitionTagGroups.push(this._createTagGroup(dictionary, definitionTags)); }
|
|
|
|
|
|
|
|
return this._createTermDictionaryEntry(
|
|
|
|
isPrimary,
|
2020-10-04 15:12:15 +00:00
|
|
|
reasons,
|
|
|
|
score,
|
2021-03-25 23:55:31 +00:00
|
|
|
dictionaryIndex,
|
|
|
|
dictionaryPriority,
|
|
|
|
sourceTermExactMatchCount,
|
2021-03-26 01:40:27 +00:00
|
|
|
maxTransformedTextLength,
|
2021-04-04 20:22:35 +00:00
|
|
|
[this._createTermHeadword(0, term, reading, [source], headwordTagGroups, rules)],
|
2021-06-26 15:57:09 +00:00
|
|
|
[this._createTermDefinition(0, [0], dictionary, dictionaryIndex, dictionaryPriority, id, score, [sequence], isPrimary, definitionTagGroups, definitions)]
|
2021-03-25 23:55:31 +00:00
|
|
|
);
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 22:11:09 +00:00
|
|
|
_createGroupedDictionaryEntry(dictionaryEntries, checkDuplicateDefinitions) {
|
2021-03-25 23:55:31 +00:00
|
|
|
// Headwords are generated before sorting, so that the order of dictionaryEntries can be maintained
|
|
|
|
const definitionEntries = [];
|
|
|
|
const headwords = new Map();
|
|
|
|
for (const dictionaryEntry of dictionaryEntries) {
|
|
|
|
const headwordIndexMap = this._addTermHeadwords(headwords, dictionaryEntry.headwords);
|
|
|
|
definitionEntries.push({index: definitionEntries.length, dictionaryEntry, headwordIndexMap});
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
|
|
|
|
// Sort
|
2021-06-26 16:37:38 +00:00
|
|
|
if (definitionEntries.length <= 1) {
|
2021-03-25 23:55:31 +00:00
|
|
|
checkDuplicateDefinitions = false;
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Merge dictionary entry data
|
|
|
|
let score = Number.MIN_SAFE_INTEGER;
|
|
|
|
let dictionaryIndex = Number.MAX_SAFE_INTEGER;
|
|
|
|
let dictionaryPriority = Number.MIN_SAFE_INTEGER;
|
2021-03-26 01:40:27 +00:00
|
|
|
let maxTransformedTextLength = 0;
|
2021-03-25 23:55:31 +00:00
|
|
|
let isPrimary = false;
|
|
|
|
const definitions = [];
|
|
|
|
const definitionsMap = checkDuplicateDefinitions ? new Map() : null;
|
|
|
|
let inflections = null;
|
|
|
|
|
|
|
|
for (const {dictionaryEntry, headwordIndexMap} of definitionEntries) {
|
|
|
|
score = Math.max(score, dictionaryEntry.score);
|
|
|
|
dictionaryIndex = Math.min(dictionaryIndex, dictionaryEntry.dictionaryIndex);
|
|
|
|
dictionaryPriority = Math.max(dictionaryPriority, dictionaryEntry.dictionaryPriority);
|
|
|
|
if (dictionaryEntry.isPrimary) {
|
|
|
|
isPrimary = true;
|
2021-03-26 01:40:27 +00:00
|
|
|
maxTransformedTextLength = Math.max(maxTransformedTextLength, dictionaryEntry.maxTransformedTextLength);
|
2021-03-25 23:55:31 +00:00
|
|
|
const dictionaryEntryInflections = dictionaryEntry.inflections;
|
|
|
|
if (inflections === null || dictionaryEntryInflections.length < inflections.length) {
|
|
|
|
inflections = dictionaryEntryInflections;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (checkDuplicateDefinitions) {
|
2021-05-08 17:16:56 +00:00
|
|
|
this._addTermDefinitions(definitions, definitionsMap, dictionaryEntry.definitions, headwordIndexMap);
|
2021-03-25 23:55:31 +00:00
|
|
|
} else {
|
2021-05-08 17:16:56 +00:00
|
|
|
this._addTermDefinitionsFast(definitions, dictionaryEntry.definitions, headwordIndexMap);
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-04 22:54:03 +00:00
|
|
|
|
2021-05-14 23:26:27 +00:00
|
|
|
const headwordsArray = [...headwords.values()];
|
|
|
|
|
|
|
|
let sourceTermExactMatchCount = 0;
|
|
|
|
for (const {term, sources} of headwordsArray) {
|
|
|
|
for (const {deinflectedText, isPrimary: isPrimary2} of sources) {
|
|
|
|
if (isPrimary2 && deinflectedText === term) {
|
|
|
|
++sourceTermExactMatchCount;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
return this._createTermDictionaryEntry(
|
|
|
|
isPrimary,
|
|
|
|
inflections !== null ? inflections : [],
|
|
|
|
score,
|
|
|
|
dictionaryIndex,
|
|
|
|
dictionaryPriority,
|
|
|
|
sourceTermExactMatchCount,
|
2021-03-26 01:40:27 +00:00
|
|
|
maxTransformedTextLength,
|
2021-05-14 23:26:27 +00:00
|
|
|
headwordsArray,
|
2021-03-25 23:55:31 +00:00
|
|
|
definitions
|
|
|
|
);
|
|
|
|
}
|
2020-10-04 22:10:10 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Data collection addition functions
|
2020-10-04 15:12:15 +00:00
|
|
|
|
2021-05-08 17:16:56 +00:00
|
|
|
_addUniqueSimple(list, newItems) {
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const item of newItems) {
|
|
|
|
if (!list.includes(item)) {
|
|
|
|
list.push(item);
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 15:12:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_addUniqueSources(sources, newSources) {
|
|
|
|
if (newSources.length === 0) { return; }
|
|
|
|
if (sources.length === 0) {
|
|
|
|
sources.push(...newSources);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const newSource of newSources) {
|
|
|
|
const {originalText, transformedText, deinflectedText, isPrimary} = newSource;
|
|
|
|
let has = false;
|
|
|
|
for (const source of sources) {
|
|
|
|
if (
|
|
|
|
source.deinflectedText === deinflectedText &&
|
|
|
|
source.transformedText === transformedText &&
|
|
|
|
source.originalText === originalText
|
|
|
|
) {
|
|
|
|
if (isPrimary) { source.isPrimary = true; }
|
|
|
|
has = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
if (!has) {
|
|
|
|
sources.push(newSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 03:17:23 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_addUniqueTagGroups(tagGroups, newTagGroups) {
|
|
|
|
if (newTagGroups.length === 0) { return; }
|
|
|
|
for (const newTagGroup of newTagGroups) {
|
|
|
|
const {dictionary} = newTagGroup;
|
|
|
|
const ii = tagGroups.length;
|
|
|
|
if (ii > 0) {
|
|
|
|
let i = 0;
|
|
|
|
for (; i < ii; ++i) {
|
|
|
|
const tagGroup = tagGroups[i];
|
|
|
|
if (tagGroup.dictionary === dictionary) {
|
2021-05-08 17:16:56 +00:00
|
|
|
this._addUniqueSimple(tagGroup.tagNames, newTagGroup.tagNames);
|
2021-03-25 23:55:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < ii) { continue; }
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
tagGroups.push(newTagGroup);
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 03:17:23 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_addTermHeadwords(headwordsMap, headwords) {
|
|
|
|
const headwordIndexMap = [];
|
2021-03-26 23:50:54 +00:00
|
|
|
for (const {term, reading, sources, tags, wordClasses} of headwords) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const key = this._createMapKey([term, reading]);
|
|
|
|
let headword = headwordsMap.get(key);
|
|
|
|
if (typeof headword === 'undefined') {
|
2021-03-26 23:50:54 +00:00
|
|
|
headword = this._createTermHeadword(headwordsMap.size, term, reading, [], [], []);
|
2021-03-25 23:55:31 +00:00
|
|
|
headwordsMap.set(key, headword);
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
this._addUniqueSources(headword.sources, sources);
|
|
|
|
this._addUniqueTagGroups(headword.tags, tags);
|
2021-05-08 17:16:56 +00:00
|
|
|
this._addUniqueSimple(headword.wordClasses, wordClasses);
|
2021-03-25 23:55:31 +00:00
|
|
|
headwordIndexMap.push(headword.index);
|
|
|
|
}
|
|
|
|
return headwordIndexMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
_addUniqueTermHeadwordIndex(headwordIndices, headwordIndex) {
|
|
|
|
let end = headwordIndices.length;
|
|
|
|
if (end === 0) {
|
|
|
|
headwordIndices.push(headwordIndex);
|
|
|
|
return;
|
2021-03-02 03:17:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
let start = 0;
|
|
|
|
while (start < end) {
|
|
|
|
const mid = Math.floor((start + end) / 2);
|
|
|
|
const value = headwordIndices[mid];
|
|
|
|
if (headwordIndex === value) { return; }
|
|
|
|
if (headwordIndex > value) {
|
|
|
|
start = mid + 1;
|
|
|
|
} else {
|
|
|
|
end = mid;
|
2020-10-04 22:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
|
|
|
|
if (headwordIndex === headwordIndices[start]) { return; }
|
|
|
|
headwordIndices.splice(start, 0, headwordIndex);
|
2020-10-04 22:10:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 17:16:56 +00:00
|
|
|
_addTermDefinitionsFast(definitions, newDefinitions, headwordIndexMap) {
|
2021-06-26 15:57:09 +00:00
|
|
|
for (const {headwordIndices, dictionary, dictionaryIndex, dictionaryPriority, sequences, id, score, isPrimary, tags, entries} of newDefinitions) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const headwordIndicesNew = [];
|
|
|
|
for (const headwordIndex of headwordIndices) {
|
|
|
|
headwordIndicesNew.push(headwordIndexMap[headwordIndex]);
|
|
|
|
}
|
2021-06-26 15:57:09 +00:00
|
|
|
definitions.push(this._createTermDefinition(definitions.length, headwordIndicesNew, dictionary, dictionaryIndex, dictionaryPriority, id, score, sequences, isPrimary, tags, entries));
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2020-08-10 01:04:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 17:16:56 +00:00
|
|
|
_addTermDefinitions(definitions, definitionsMap, newDefinitions, headwordIndexMap) {
|
2021-06-26 15:57:09 +00:00
|
|
|
for (const {headwordIndices, dictionary, dictionaryIndex, dictionaryPriority, sequences, id, score, isPrimary, tags, entries} of newDefinitions) {
|
2021-05-08 17:16:56 +00:00
|
|
|
const key = this._createMapKey([dictionary, ...entries]);
|
2021-03-25 23:55:31 +00:00
|
|
|
let definition = definitionsMap.get(key);
|
|
|
|
if (typeof definition === 'undefined') {
|
2021-06-26 15:57:09 +00:00
|
|
|
definition = this._createTermDefinition(definitions.length, [], dictionary, dictionaryIndex, dictionaryPriority, id, score, [...sequences], isPrimary, [], [...entries]);
|
2021-03-25 23:55:31 +00:00
|
|
|
definitions.push(definition);
|
|
|
|
definitionsMap.set(key, definition);
|
2021-04-18 22:11:09 +00:00
|
|
|
} else {
|
|
|
|
if (isPrimary) {
|
|
|
|
definition.isPrimary = true;
|
|
|
|
}
|
2021-05-08 17:16:56 +00:00
|
|
|
this._addUniqueSimple(definition.sequences, sequences);
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2020-10-04 23:33:22 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const newHeadwordIndices = definition.headwordIndices;
|
|
|
|
for (const headwordIndex of headwordIndices) {
|
|
|
|
this._addUniqueTermHeadwordIndex(newHeadwordIndices, headwordIndexMap[headwordIndex]);
|
|
|
|
}
|
|
|
|
this._addUniqueTagGroups(definition.tags, tags);
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 01:04:09 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Sorting functions
|
|
|
|
|
|
|
|
_sortDatabaseEntriesByIndex(databaseEntries) {
|
|
|
|
if (databaseEntries.length <= 1) { return; }
|
|
|
|
databaseEntries.sort((a, b) => a.index - b.index);
|
2020-10-02 21:59:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_sortTermDictionaryEntries(dictionaryEntries) {
|
2020-10-02 21:59:14 +00:00
|
|
|
const stringComparer = this._stringComparer;
|
2021-02-27 04:55:32 +00:00
|
|
|
const compareFunction = (v1, v2) => {
|
2021-03-25 23:55:31 +00:00
|
|
|
// Sort by length of source term
|
2021-03-26 01:40:27 +00:00
|
|
|
let i = v2.maxTransformedTextLength - v1.maxTransformedTextLength;
|
2021-03-25 23:55:31 +00:00
|
|
|
if (i !== 0) { return i; }
|
2021-03-11 01:27:10 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Sort by the number of inflection reasons
|
|
|
|
i = v1.inflections.length - v2.inflections.length;
|
|
|
|
if (i !== 0) { return i; }
|
2021-03-11 01:27:10 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Sort by how many terms exactly match the source (e.g. for exact kana prioritization)
|
|
|
|
i = v2.sourceTermExactMatchCount - v1.sourceTermExactMatchCount;
|
|
|
|
if (i !== 0) { return i; }
|
2020-10-04 22:54:03 +00:00
|
|
|
|
2021-03-06 18:27:20 +00:00
|
|
|
// Sort by dictionary priority
|
2021-03-25 23:55:31 +00:00
|
|
|
i = v2.dictionaryPriority - v1.dictionaryPriority;
|
2021-03-06 18:27:20 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-09-26 15:08:16 +00:00
|
|
|
// Sort by frequency order
|
|
|
|
i = v1.frequencyOrder - v2.frequencyOrder;
|
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-03-06 18:04:50 +00:00
|
|
|
// Sort by term score
|
2020-10-02 21:59:14 +00:00
|
|
|
i = v2.score - v1.score;
|
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
// Sort by headword term text
|
2021-03-25 23:55:31 +00:00
|
|
|
const headwords1 = v1.headwords;
|
|
|
|
const headwords2 = v2.headwords;
|
|
|
|
for (let j = 0, jj = Math.min(headwords1.length, headwords2.length); j < jj; ++j) {
|
|
|
|
const term1 = headwords1[j].term;
|
|
|
|
const term2 = headwords2[j].term;
|
|
|
|
|
|
|
|
i = term2.length - term1.length;
|
2021-03-06 18:04:50 +00:00
|
|
|
if (i !== 0) { return i; }
|
2020-10-04 23:04:21 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
i = stringComparer.compare(term1, term2);
|
2021-03-06 18:04:50 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
}
|
2020-10-02 21:59:14 +00:00
|
|
|
|
2021-03-06 18:04:50 +00:00
|
|
|
// Sort by dictionary order
|
2021-03-25 23:55:31 +00:00
|
|
|
i = v1.dictionaryIndex - v2.dictionaryIndex;
|
2021-03-06 18:04:50 +00:00
|
|
|
return i;
|
2020-10-04 15:12:15 +00:00
|
|
|
};
|
2021-03-25 23:55:31 +00:00
|
|
|
dictionaryEntries.sort(compareFunction);
|
2020-10-02 21:59:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 16:37:38 +00:00
|
|
|
_sortTermDictionaryEntryDefinitions(definitions) {
|
|
|
|
const compareFunction = (v1, v2) => {
|
2021-03-25 23:55:31 +00:00
|
|
|
// Sort by dictionary priority
|
|
|
|
let i = v2.dictionaryPriority - v1.dictionaryPriority;
|
|
|
|
if (i !== 0) { return i; }
|
2020-10-05 02:04:44 +00:00
|
|
|
|
2021-09-26 15:08:16 +00:00
|
|
|
// Sort by frequency order
|
|
|
|
i = v1.frequencyOrder - v2.frequencyOrder;
|
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Sort by term score
|
|
|
|
i = v2.score - v1.score;
|
2020-10-02 21:59:14 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
// Sort by definition headword index
|
2021-06-26 16:37:38 +00:00
|
|
|
const headwordIndices1 = v1.headwordIndices;
|
|
|
|
const headwordIndices2 = v2.headwordIndices;
|
|
|
|
const jj = headwordIndices1.length;
|
|
|
|
i = headwordIndices2.length - jj;
|
|
|
|
if (i !== 0) { return i; }
|
|
|
|
for (let j = 0; j < jj; ++j) {
|
|
|
|
i = headwordIndices1[j] - headwordIndices2[j];
|
2021-03-25 23:55:31 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by dictionary order
|
|
|
|
i = v1.dictionaryIndex - v2.dictionaryIndex;
|
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
|
|
|
// Sort by original order
|
2021-06-26 16:37:38 +00:00
|
|
|
i = v1.index - v2.index;
|
2021-03-25 23:55:31 +00:00
|
|
|
return i;
|
|
|
|
};
|
2021-06-26 16:37:38 +00:00
|
|
|
definitions.sort(compareFunction);
|
2020-08-10 01:04:09 +00:00
|
|
|
}
|
2021-01-03 17:12:55 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_sortTermDictionaryEntriesById(dictionaryEntries) {
|
|
|
|
if (dictionaryEntries.length <= 1) { return; }
|
2021-06-26 15:57:09 +00:00
|
|
|
dictionaryEntries.sort((a, b) => a.definitions[0].id - b.definitions[0].id);
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 16:37:38 +00:00
|
|
|
_sortTermDictionaryEntrySimpleData(dataList) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const compare = (v1, v2) => {
|
2021-03-06 18:04:50 +00:00
|
|
|
// Sort by dictionary priority
|
2021-03-25 23:55:31 +00:00
|
|
|
let i = v2.dictionaryPriority - v1.dictionaryPriority;
|
2021-02-28 03:27:00 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
// Sory by headword order
|
2021-03-25 23:55:31 +00:00
|
|
|
i = v1.headwordIndex - v2.headwordIndex;
|
2021-02-28 03:27:00 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-03-06 18:04:50 +00:00
|
|
|
// Sort by dictionary order
|
2021-03-25 23:55:31 +00:00
|
|
|
i = v1.dictionaryIndex - v2.dictionaryIndex;
|
2021-03-06 18:04:50 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
2021-02-28 03:27:00 +00:00
|
|
|
// Default order
|
|
|
|
i = v1.index - v2.index;
|
|
|
|
return i;
|
|
|
|
};
|
2021-06-26 16:37:38 +00:00
|
|
|
dataList.sort(compare);
|
2021-02-28 03:27:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
_sortKanjiDictionaryEntryData(dictionaryEntries) {
|
|
|
|
const compare = (v1, v2) => {
|
2021-03-06 18:04:50 +00:00
|
|
|
// Sort by dictionary priority
|
2021-03-25 23:55:31 +00:00
|
|
|
let i = v2.dictionaryPriority - v1.dictionaryPriority;
|
2021-03-06 18:04:50 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
|
|
|
// Sort by dictionary order
|
2021-03-25 23:55:31 +00:00
|
|
|
i = v1.dictionaryIndex - v2.dictionaryIndex;
|
2021-02-28 03:27:00 +00:00
|
|
|
if (i !== 0) { return i; }
|
|
|
|
|
|
|
|
// Default order
|
|
|
|
i = v1.index - v2.index;
|
|
|
|
return i;
|
|
|
|
};
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {frequencies} of dictionaryEntries) {
|
|
|
|
frequencies.sort(compare);
|
2021-01-03 17:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 15:08:16 +00:00
|
|
|
|
|
|
|
_updateSortFrequencies(dictionaryEntries, dictionary, ascending) {
|
|
|
|
const frequencyMap = new Map();
|
|
|
|
for (const dictionaryEntry of dictionaryEntries) {
|
|
|
|
const {definitions, frequencies} = dictionaryEntry;
|
|
|
|
let frequencyMin = Number.MAX_SAFE_INTEGER;
|
|
|
|
let frequencyMax = Number.MIN_SAFE_INTEGER;
|
|
|
|
for (const item of frequencies) {
|
|
|
|
if (item.dictionary !== dictionary) { continue; }
|
|
|
|
const {headwordIndex, frequency} = item;
|
|
|
|
if (typeof frequency !== 'number') { continue; }
|
|
|
|
frequencyMap.set(headwordIndex, frequency);
|
|
|
|
frequencyMin = Math.min(frequencyMin, frequency);
|
|
|
|
frequencyMax = Math.max(frequencyMax, frequency);
|
|
|
|
}
|
|
|
|
dictionaryEntry.frequencyOrder = (
|
|
|
|
frequencyMin <= frequencyMax ?
|
|
|
|
(ascending ? frequencyMin : -frequencyMax) :
|
|
|
|
(ascending ? Number.MAX_SAFE_INTEGER : 0)
|
|
|
|
);
|
|
|
|
for (const definition of definitions) {
|
|
|
|
frequencyMin = Number.MAX_SAFE_INTEGER;
|
|
|
|
frequencyMax = Number.MIN_SAFE_INTEGER;
|
|
|
|
const {headwordIndices} = definition;
|
|
|
|
for (const headwordIndex of headwordIndices) {
|
|
|
|
const frequency = frequencyMap.get(headwordIndex);
|
|
|
|
if (typeof frequency !== 'number') { continue; }
|
|
|
|
frequencyMin = Math.min(frequencyMin, frequency);
|
|
|
|
frequencyMax = Math.max(frequencyMax, frequency);
|
|
|
|
}
|
|
|
|
definition.frequencyOrder = (
|
|
|
|
frequencyMin <= frequencyMax ?
|
|
|
|
(ascending ? frequencyMin : -frequencyMax) :
|
|
|
|
(ascending ? Number.MAX_SAFE_INTEGER : 0)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
frequencyMap.clear();
|
|
|
|
}
|
|
|
|
}
|
2016-03-20 17:52:27 +00:00
|
|
|
}
|