Translator refactor (#1473)

* Refactor _groupTerms and add doc comment

* Update where expression/reading is acquired from

* Add doc comment

* Add isPrimary field

* Update test data

* Add definition which has "isPrimary": false definitions
This commit is contained in:
toasted-nutbread 2021-03-01 19:01:30 -05:00 committed by GitHub
parent 488dc486f1
commit b477da97d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3578 additions and 5 deletions

View File

@ -244,7 +244,7 @@ class Translator {
if (databaseDefinitions.length === 0) { continue; } if (databaseDefinitions.length === 0) { continue; }
maxLength = Math.max(maxLength, rawSource.length); maxLength = Math.max(maxLength, rawSource.length);
for (const databaseDefinition of databaseDefinitions) { for (const databaseDefinition of databaseDefinitions) {
const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, rawSource, term, reasons, enabledDictionaryMap); const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, rawSource, term, reasons, true, enabledDictionaryMap);
definitions.push(definition); definitions.push(definition);
} }
} }
@ -358,6 +358,11 @@ class Translator {
return deinflections; return deinflections;
} }
/**
* @param definitions An array of 'term' definitions.
* @param mainDictionary The name of the main dictionary.
* @param enabledDictionaryMap The map of enabled dictionaries and their settings.
*/
async _getSequencedDefinitions(definitions, mainDictionary, enabledDictionaryMap) { async _getSequencedDefinitions(definitions, mainDictionary, enabledDictionaryMap) {
const sequenceList = []; const sequenceList = [];
const sequencedDefinitionMap = new Map(); const sequencedDefinitionMap = new Map();
@ -393,7 +398,7 @@ class Translator {
if (relatedDefinitionIds.has(id)) { continue; } if (relatedDefinitionIds.has(id)) { continue; }
const {source, rawSource, sourceTerm} = relatedDefinitions[0]; const {source, rawSource, sourceTerm} = relatedDefinitions[0];
const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, rawSource, sourceTerm, [], enabledDictionaryMap); const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, rawSource, sourceTerm, [], false, enabledDictionaryMap);
relatedDefinitions.push(definition); relatedDefinitions.push(definition);
} }
} }
@ -425,7 +430,7 @@ class Translator {
const definitions = []; const definitions = [];
for (const databaseDefinition of databaseDefinitions) { for (const databaseDefinition of databaseDefinitions) {
const source = expressionList[databaseDefinition.index]; const source = expressionList[databaseDefinition.index];
const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, source, source, [], secondarySearchDictionaryMap); const definition = await this._createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, source, source, [], false, secondarySearchDictionaryMap);
definitions.push(definition); definitions.push(definition);
} }
@ -565,10 +570,16 @@ class Translator {
} }
} }
/**
* Groups definitions with the same [source, expression, reading, reasons].
* @param definitions An array of 'term' definitions.
* @returns An array of 'termGrouped' definitions.
*/
_groupTerms(definitions) { _groupTerms(definitions) {
const groups = new Map(); const groups = new Map();
for (const definition of definitions) { for (const definition of definitions) {
const key = this._createMapKey([definition.source, definition.expression, definition.reading, ...definition.reasons]); const {source, reasons, expressions: [{expression, reading}]} = definition;
const key = this._createMapKey([source, expression, reading, ...reasons]);
let groupDefinitions = groups.get(key); let groupDefinitions = groups.get(key);
if (typeof groupDefinitions === 'undefined') { if (typeof groupDefinitions === 'undefined') {
groupDefinitions = []; groupDefinitions = [];
@ -1080,7 +1091,7 @@ class Translator {
}; };
} }
async _createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, rawSource, sourceTerm, reasons, enabledDictionaryMap) { async _createTermDefinitionFromDatabaseDefinition(databaseDefinition, source, rawSource, sourceTerm, reasons, isPrimary, enabledDictionaryMap) {
const {expression, reading, definitionTags, termTags, glossary, score, dictionary, id, sequence} = databaseDefinition; const {expression, reading, definitionTags, termTags, glossary, score, dictionary, id, sequence} = databaseDefinition;
const dictionaryOrder = this._getDictionaryOrder(dictionary, enabledDictionaryMap); const dictionaryOrder = this._getDictionaryOrder(dictionary, enabledDictionaryMap);
const termTagsExpanded = await this._expandTags(termTags, dictionary); const termTagsExpanded = await this._expandTags(termTags, dictionary);
@ -1101,6 +1112,7 @@ class Translator {
sourceTerm, sourceTerm,
reasons, reasons,
score, score,
isPrimary,
sequence, sequence,
dictionary, dictionary,
dictionaryOrder, dictionaryOrder,
@ -1136,6 +1148,7 @@ class Translator {
sourceTerm, sourceTerm,
reasons: [...reasons], reasons: [...reasons],
score, score,
// isPrimary
// sequence // sequence
dictionary: dictionaryNames[0], dictionary: dictionaryNames[0],
dictionaryOrder, dictionaryOrder,
@ -1167,6 +1180,7 @@ class Translator {
// sourceTerm // sourceTerm
reasons, reasons,
score, score,
// isPrimary
// sequence // sequence
dictionary: dictionaryNames[0], dictionary: dictionaryNames[0],
dictionaryOrder, dictionaryOrder,
@ -1216,6 +1230,7 @@ class Translator {
// sourceTerm // sourceTerm
reasons: [], reasons: [],
score, score,
// isPrimary
// sequence // sequence
dictionary: dictionaryNames[0], dictionary: dictionaryNames[0],
dictionaryOrder, dictionaryOrder,

File diff suppressed because it is too large Load Diff