summaryrefslogtreecommitdiff
path: root/ext/bg/js/util.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-01-08 15:08:18 -0800
committerAlex Yatskov <alex@foosoft.net>2017-01-08 15:08:18 -0800
commit0498ea5d361867f34b6b813178a39709e8bea68e (patch)
tree45c9e3b890471e7c6ab6eee5a566d35a604fd20a /ext/bg/js/util.js
parent984b5326a9753c47ef43defa88d664f9a371729a (diff)
initial pass at term grouping function
Diffstat (limited to 'ext/bg/js/util.js')
-rw-r--r--ext/bg/js/util.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js
index 52a52cf8..3fa01a10 100644
--- a/ext/bg/js/util.js
+++ b/ext/bg/js/util.js
@@ -91,6 +91,71 @@ function undupeTermDefs(definitions) {
return definitionsUnique;
}
+function groupTermDefs(definitions) {
+ const groups = {};
+ let groupCount = 0;
+
+ for (const definition of definitions) {
+ const key = [definition.source, definition.expression].concat(definition.reasons);
+ if (definition.reading) {
+ key.push(definition.reading);
+ }
+
+ const group = groups[key];
+ if (group) {
+ group.push(definition);
+ } else {
+ groups[key] = [definition];
+ ++groupCount;
+ }
+ }
+
+ const results = [];
+ for (const key in groups) {
+ const groupDefs = sortTermDefs(groups[key]);
+
+ const tagCounts = {};
+ for (const tag of groupDefs.map(def => def.tags)) {
+ const count = tagsGlobal[tag.name] || 0;
+ tagCounts[tag.name] = count + 1;
+ }
+
+ const tagsGlobal = [];
+ const tagsGlobalAdded = {};
+ let maxScore = Number.MIN_SAFE_INTEGER;
+
+ for (const definition of groupDefs) {
+ const tagsLocal = [];
+ for (const tag of definition.tags) {
+ if (tagCounts[tag.name] === groupCount) {
+ if (!tagsGlobalAdded[tag.name]) {
+ tagsGlobalAdded[tag.name] = true;
+ tagsGlobal.push(tag);
+ }
+ } else {
+ tagsLocal.push(tag);
+ }
+ }
+
+ definition.tags = tagsLocal;
+ maxScore = Math.max(maxScore, definition.score);
+ }
+
+ const firstDef = groupDefs[0];
+ results.push({
+ definitions: groupDefs,
+ expression: firstDef.expression,
+ reading: firstDef.reading,
+ reasons: firstDef.reasons,
+ score: maxScore,
+ source: firstDef.source,
+ tags: sortTags(tagsGlobal),
+ });
+ }
+
+ return sortTermDefs(results);
+}
+
function buildDictTag(name) {
return sanitizeTag({name, category: 'dictionary', order: 100});
}