Reduce number of redundant Map.get calls in dictTermsMergeByGloss

This commit is contained in:
toasted-nutbread 2020-02-15 17:15:24 -05:00
parent 45c685d00a
commit 69b5007842
2 changed files with 26 additions and 8 deletions

View File

@ -222,17 +222,34 @@ function dictTermsMergeByGloss(result, definitions, appendTo=null, mergedIndices
}
if (appendTo === null) {
// result->expressions[ Expression1[ Reading1[ Tag1, Tag2 ] ], Expression2, ... ]
if (!result.expressions.has(expression)) {
result.expressions.set(expression, new Map());
/*
Data layout:
result.expressions = new Map([
[expression, new Map([
[reading, new Map([
[tagName, tagInfo],
...
])],
...
])],
...
]);
*/
let readingMap = result.expressions.get(expression);
if (typeof readingMap === 'undefined') {
readingMap = new Map();
result.expressions.set(expression, readingMap);
}
if (!result.expressions.get(expression).has(reading)) {
result.expressions.get(expression).set(reading, []);
let termTagsMap = readingMap.get(reading);
if (typeof termTagsMap === 'undefined') {
termTagsMap = new Map();
readingMap.set(reading, termTagsMap);
}
for (const tag of definition.termTags) {
if (!result.expressions.get(expression).get(reading).find((existingTag) => existingTag.name === tag.name)) {
result.expressions.get(expression).get(reading).push(tag);
if (!termTagsMap.has(tag.name)) {
termTagsMap.set(tag.name, tag);
}
}
}

View File

@ -127,7 +127,8 @@ class Translator {
const expressions = [];
for (const [expression, readingMap] of result.expressions.entries()) {
for (const [reading, termTags] of readingMap.entries()) {
for (const [reading, termTagsMap] of readingMap.entries()) {
const termTags = [...termTagsMap.values()];
const score = termTags.map((tag) => tag.score).reduce((p, v) => p + v, 0);
expressions.push(Translator.createExpression(expression, reading, dictTagsSort(termTags), Translator.scoreToTermFrequency(score)));
}