summaryrefslogtreecommitdiff
path: root/ext/bg/js/util.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-01-16 20:54:01 -0800
committerAlex Yatskov <alex@foosoft.net>2017-01-16 20:54:01 -0800
commit268e00435f081b81ac0426bd3e316acb1bb33915 (patch)
tree1b93e3110058a56b14e0cbf5fe52d89916af744e /ext/bg/js/util.js
parente8840465f07f09fbb9acb6071b94489f02ecd87f (diff)
support for dictionary priority sorting
Diffstat (limited to 'ext/bg/js/util.js')
-rw-r--r--ext/bg/js/util.js17
1 files changed, 14 insertions, 3 deletions
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js
index 900a7a2c..bba4d797 100644
--- a/ext/bg/js/util.js
+++ b/ext/bg/js/util.js
@@ -50,7 +50,7 @@ function promiseCallback(promise, callback) {
});
}
-function sortTermDefs(definitions) {
+function sortTermDefs(definitions, dictionaries=null) {
return definitions.sort((v1, v2) => {
const sl1 = v1.source.length;
const sl2 = v2.source.length;
@@ -60,6 +60,16 @@ function sortTermDefs(definitions) {
return 1;
}
+ if (dictionaries !== null) {
+ const p1 = (dictionaries[v1.dictionary] || {}).priority || 0;
+ const p2 = (dictionaries[v2.dictionary] || {}).priority || 0;
+ if (p1 > p2) {
+ return -1;
+ } else if (p1 < p2) {
+ return 1;
+ }
+ }
+
const s1 = v1.score;
const s2 = v2.score;
if (s1 > s2) {
@@ -97,7 +107,7 @@ function undupeTermDefs(definitions) {
return definitionsUnique;
}
-function groupTermDefs(definitions) {
+function groupTermDefs(definitions, dictionaries) {
const groups = {};
for (const definition of definitions) {
const key = [definition.source, definition.expression].concat(definition.reasons);
@@ -117,12 +127,13 @@ function groupTermDefs(definitions) {
for (const key in groups) {
const groupDefs = groups[key];
const firstDef = groupDefs[0];
+ sortTermDefs(groupDefs, dictionaries);
results.push({
definitions: groupDefs,
expression: firstDef.expression,
reading: firstDef.reading,
reasons: firstDef.reasons,
- score: groupDefs.reduce((x, y) => x > y ? x : y, Number.MIN_SAFE_INTEGER),
+ score: groupDefs.reduce((x, y) => x.score > y.score ? x.score : y.score, Number.MIN_SAFE_INTEGER),
source: firstDef.source
});
}