aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-03-01 19:01:30 -0500
committerGitHub <noreply@github.com>2021-03-01 19:01:30 -0500
commitb477da97d43883592d420883f20d137d06e8e2cd (patch)
tree1a3bf4b198d340397710c8d4a7101e0b4e9b0383 /ext
parent488dc486f1a3f435e6bf182295c38cb49fb755b7 (diff)
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
Diffstat (limited to 'ext')
-rw-r--r--ext/js/language/translator.js25
1 files changed, 20 insertions, 5 deletions
diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js
index e530811b..f5885c05 100644
--- a/ext/js/language/translator.js
+++ b/ext/js/language/translator.js
@@ -244,7 +244,7 @@ class Translator {
if (databaseDefinitions.length === 0) { continue; }
maxLength = Math.max(maxLength, rawSource.length);
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);
}
}
@@ -358,6 +358,11 @@ class Translator {
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) {
const sequenceList = [];
const sequencedDefinitionMap = new Map();
@@ -393,7 +398,7 @@ class Translator {
if (relatedDefinitionIds.has(id)) { continue; }
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);
}
}
@@ -425,7 +430,7 @@ class Translator {
const definitions = [];
for (const databaseDefinition of databaseDefinitions) {
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);
}
@@ -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) {
const groups = new Map();
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);
if (typeof groupDefinitions === 'undefined') {
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 dictionaryOrder = this._getDictionaryOrder(dictionary, enabledDictionaryMap);
const termTagsExpanded = await this._expandTags(termTags, dictionary);
@@ -1101,6 +1112,7 @@ class Translator {
sourceTerm,
reasons,
score,
+ isPrimary,
sequence,
dictionary,
dictionaryOrder,
@@ -1136,6 +1148,7 @@ class Translator {
sourceTerm,
reasons: [...reasons],
score,
+ // isPrimary
// sequence
dictionary: dictionaryNames[0],
dictionaryOrder,
@@ -1167,6 +1180,7 @@ class Translator {
// sourceTerm
reasons,
score,
+ // isPrimary
// sequence
dictionary: dictionaryNames[0],
dictionaryOrder,
@@ -1216,6 +1230,7 @@ class Translator {
// sourceTerm
reasons: [],
score,
+ // isPrimary
// sequence
dictionary: dictionaryNames[0],
dictionaryOrder,