Use Map for dictEnabledSet

This commit is contained in:
toasted-nutbread 2020-02-15 15:01:21 -05:00
parent b0c566417f
commit cc2e21cd86
3 changed files with 43 additions and 45 deletions

View File

@ -149,14 +149,14 @@ class Database {
await Promise.all(promises); await Promise.all(promises);
} }
async findTermsBulk(termList, titles, wildcard) { async findTermsBulk(termList, dictionaries, wildcard) {
this._validate(); this._validate();
const promises = []; const promises = [];
const visited = new Set(); const visited = new Set();
const results = []; const results = [];
const processRow = (row, index) => { const processRow = (row, index) => {
if (titles.includes(row.dictionary) && !visited.has(row.id)) { if (dictionaries.has(row.dictionary) && !visited.has(row.id)) {
visited.add(row.id); visited.add(row.id);
results.push(Database._createTerm(row, index)); results.push(Database._createTerm(row, index));
} }
@ -184,13 +184,13 @@ class Database {
return results; return results;
} }
async findTermsExactBulk(termList, readingList, titles) { async findTermsExactBulk(termList, readingList, dictionaries) {
this._validate(); this._validate();
const promises = []; const promises = [];
const results = []; const results = [];
const processRow = (row, index) => { const processRow = (row, index) => {
if (row.reading === readingList[index] && titles.includes(row.dictionary)) { if (row.reading === readingList[index] && dictionaries.has(row.dictionary)) {
results.push(Database._createTerm(row, index)); results.push(Database._createTerm(row, index));
} }
}; };
@ -234,16 +234,16 @@ class Database {
return results; return results;
} }
async findTermMetaBulk(termList, titles) { async findTermMetaBulk(termList, dictionaries) {
return this._findGenericBulk('termMeta', 'expression', termList, titles, Database._createTermMeta); return this._findGenericBulk('termMeta', 'expression', termList, dictionaries, Database._createTermMeta);
} }
async findKanjiBulk(kanjiList, titles) { async findKanjiBulk(kanjiList, dictionaries) {
return this._findGenericBulk('kanji', 'character', kanjiList, titles, Database._createKanji); return this._findGenericBulk('kanji', 'character', kanjiList, dictionaries, Database._createKanji);
} }
async findKanjiMetaBulk(kanjiList, titles) { async findKanjiMetaBulk(kanjiList, dictionaries) {
return this._findGenericBulk('kanjiMeta', 'character', kanjiList, titles, Database._createKanjiMeta); return this._findGenericBulk('kanjiMeta', 'character', kanjiList, dictionaries, Database._createKanjiMeta);
} }
async findTagForTitle(name, title) { async findTagForTitle(name, title) {
@ -572,13 +572,13 @@ class Database {
return count > 0; return count > 0;
} }
async _findGenericBulk(tableName, indexName, indexValueList, titles, createResult) { async _findGenericBulk(tableName, indexName, indexValueList, dictionaries, createResult) {
this._validate(); this._validate();
const promises = []; const promises = [];
const results = []; const results = [];
const processRow = (row, index) => { const processRow = (row, index) => {
if (titles.includes(row.dictionary)) { if (dictionaries.has(row.dictionary)) {
results.push(createResult(row, index)); results.push(createResult(row, index));
} }
}; };

View File

@ -19,15 +19,17 @@
/*global utilSetEqual, utilSetIntersection, apiTemplateRender*/ /*global utilSetEqual, utilSetIntersection, apiTemplateRender*/
function dictEnabledSet(options) { function dictEnabledSet(options) {
const dictionaries = {}; const enabledDictionaryMap = new Map();
for (const title in options.dictionaries) { const optionsDictionaries = options.dictionaries;
const dictionary = options.dictionaries[title]; for (const title in optionsDictionaries) {
if (dictionary.enabled) { if (!hasOwn(optionsDictionaries, title)) { continue; }
dictionaries[title] = dictionary; const dictionary = optionsDictionaries[title];
if (!dictionary.enabled) { continue; }
enabledDictionaryMap.set(title, {
priority: dictionary.priority || 0
});
} }
} return enabledDictionaryMap;
return dictionaries;
} }
function dictConfigured(options) { function dictConfigured(options) {
@ -58,10 +60,11 @@ function dictTermsSort(definitions, dictionaries=null) {
return definitions.sort((v1, v2) => { return definitions.sort((v1, v2) => {
let i; let i;
if (dictionaries !== null) { if (dictionaries !== null) {
i = ( const dictionaryInfo1 = dictionaries.get(v1.dictionary);
((dictionaries[v2.dictionary] || {}).priority || 0) - const dictionaryInfo2 = dictionaries.get(v2.dictionary);
((dictionaries[v1.dictionary] || {}).priority || 0) const priority1 = typeof dictionaryInfo1 !== 'undefined' ? dictionaryInfo1.priority || 0 : 0;
); const priority2 = typeof dictionaryInfo2 !== 'undefined' ? dictionaryInfo2.priority || 0 : 0;
i = priority2 - priority1;
if (i !== 0) { return i; } if (i !== 0) { return i; }
} }

View File

@ -85,7 +85,7 @@ class Translator {
} }
} }
const definitions = await this.database.findTermsExactBulk(expressionList, readingList, secondarySearchTitles); const definitions = await this.database.findTermsExactBulk(expressionList, readingList, new Set(secondarySearchTitles));
for (const definition of definitions) { for (const definition of definitions) {
const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary); const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary);
definitionTags.push(dictTagBuildSource(definition.dictionary)); definitionTags.push(dictTagBuildSource(definition.dictionary));
@ -156,11 +156,10 @@ class Translator {
async findTermsGrouped(text, details, options) { async findTermsGrouped(text, details, options) {
const dictionaries = dictEnabledSet(options); const dictionaries = dictEnabledSet(options);
const titles = Object.keys(dictionaries);
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options); const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
const definitionsGrouped = dictTermsGroup(definitions, dictionaries); const definitionsGrouped = dictTermsGroup(definitions, dictionaries);
await this.buildTermMeta(definitionsGrouped, titles); await this.buildTermMeta(definitionsGrouped, dictionaries);
if (options.general.compactTags) { if (options.general.compactTags) {
for (const definition of definitionsGrouped) { for (const definition of definitionsGrouped) {
@ -174,7 +173,6 @@ class Translator {
async findTermsMerged(text, details, options) { async findTermsMerged(text, details, options) {
const dictionaries = dictEnabledSet(options); const dictionaries = dictEnabledSet(options);
const secondarySearchTitles = Object.keys(options.dictionaries).filter((dict) => options.dictionaries[dict].allowSecondarySearches); const secondarySearchTitles = Object.keys(options.dictionaries).filter((dict) => options.dictionaries[dict].allowSecondarySearches);
const titles = Object.keys(dictionaries);
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options); const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
const {sequencedDefinitions, defaultDefinitions} = await this.getSequencedDefinitions(definitions, options.general.mainDictionary); const {sequencedDefinitions, defaultDefinitions} = await this.getSequencedDefinitions(definitions, options.general.mainDictionary);
const definitionsMerged = []; const definitionsMerged = [];
@ -198,7 +196,7 @@ class Translator {
definitionsMerged.push(groupedDefinition); definitionsMerged.push(groupedDefinition);
} }
await this.buildTermMeta(definitionsMerged, titles); await this.buildTermMeta(definitionsMerged, dictionaries);
if (options.general.compactTags) { if (options.general.compactTags) {
for (const definition of definitionsMerged) { for (const definition of definitionsMerged) {
@ -211,10 +209,9 @@ class Translator {
async findTermsSplit(text, details, options) { async findTermsSplit(text, details, options) {
const dictionaries = dictEnabledSet(options); const dictionaries = dictEnabledSet(options);
const titles = Object.keys(dictionaries);
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options); const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
await this.buildTermMeta(definitions, titles); await this.buildTermMeta(definitions, dictionaries);
return [definitions, length]; return [definitions, length];
} }
@ -225,11 +222,10 @@ class Translator {
return [[], 0]; return [[], 0];
} }
const titles = Object.keys(dictionaries);
const deinflections = ( const deinflections = (
details.wildcard ? details.wildcard ?
await this.findTermWildcard(text, titles, details.wildcard) : await this.findTermWildcard(text, dictionaries, details.wildcard) :
await this.findTermDeinflections(text, titles, options) await this.findTermDeinflections(text, dictionaries, options)
); );
let definitions = []; let definitions = [];
@ -271,8 +267,8 @@ class Translator {
return [definitions, length]; return [definitions, length];
} }
async findTermWildcard(text, titles, wildcard) { async findTermWildcard(text, dictionaries, wildcard) {
const definitions = await this.database.findTermsBulk([text], titles, wildcard); const definitions = await this.database.findTermsBulk([text], dictionaries, wildcard);
if (definitions.length === 0) { if (definitions.length === 0) {
return []; return [];
} }
@ -287,7 +283,7 @@ class Translator {
}]; }];
} }
async findTermDeinflections(text, titles, options) { async findTermDeinflections(text, dictionaries, options) {
const deinflections = this.getAllDeinflections(text, options); const deinflections = this.getAllDeinflections(text, options);
if (deinflections.length === 0) { if (deinflections.length === 0) {
@ -309,7 +305,7 @@ class Translator {
deinflectionArray.push(deinflection); deinflectionArray.push(deinflection);
} }
const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles, null); const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, dictionaries, null);
for (const definition of definitions) { for (const definition of definitions) {
const definitionRules = Deinflector.rulesToRuleFlags(definition.rules); const definitionRules = Deinflector.rulesToRuleFlags(definition.rules);
@ -399,13 +395,12 @@ class Translator {
async findKanji(text, options) { async findKanji(text, options) {
const dictionaries = dictEnabledSet(options); const dictionaries = dictEnabledSet(options);
const titles = Object.keys(dictionaries);
const kanjiUnique = new Set(); const kanjiUnique = new Set();
for (const c of text) { for (const c of text) {
kanjiUnique.add(c); kanjiUnique.add(c);
} }
const definitions = await this.database.findKanjiBulk([...kanjiUnique], titles); const definitions = await this.database.findKanjiBulk([...kanjiUnique], dictionaries);
if (definitions.length === 0) { if (definitions.length === 0) {
return definitions; return definitions;
} }
@ -425,12 +420,12 @@ class Translator {
definition.stats = stats; definition.stats = stats;
} }
await this.buildKanjiMeta(definitions, titles); await this.buildKanjiMeta(definitions, dictionaries);
return definitions; return definitions;
} }
async buildTermMeta(definitions, titles) { async buildTermMeta(definitions, dictionaries) {
const terms = []; const terms = [];
for (const definition of definitions) { for (const definition of definitions) {
if (definition.expressions) { if (definition.expressions) {
@ -464,7 +459,7 @@ class Translator {
term.frequencies = []; term.frequencies = [];
} }
const metas = await this.database.findTermMetaBulk(expressionsUnique, titles); const metas = await this.database.findTermMetaBulk(expressionsUnique, dictionaries);
for (const {expression, mode, data, dictionary, index} of metas) { for (const {expression, mode, data, dictionary, index} of metas) {
switch (mode) { switch (mode) {
case 'freq': case 'freq':
@ -476,14 +471,14 @@ class Translator {
} }
} }
async buildKanjiMeta(definitions, titles) { async buildKanjiMeta(definitions, dictionaries) {
const kanjiList = []; const kanjiList = [];
for (const definition of definitions) { for (const definition of definitions) {
kanjiList.push(definition.character); kanjiList.push(definition.character);
definition.frequencies = []; definition.frequencies = [];
} }
const metas = await this.database.findKanjiMetaBulk(kanjiList, titles); const metas = await this.database.findKanjiMetaBulk(kanjiList, dictionaries);
for (const {character, mode, data, dictionary, index} of metas) { for (const {character, mode, data, dictionary, index} of metas) {
switch (mode) { switch (mode) {
case 'freq': case 'freq':