diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-08-20 13:04:59 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-08-20 13:04:59 -0700 |
commit | 9649a831814816adfed54f45cfceb338993bb2fb (patch) | |
tree | f8e6e483c80a1fbbd6351d2d80aa4fea2ec100b5 /ext/bg/js/translator.js | |
parent | 28e217b75be75fc52dde9aabdf9b52d413497533 (diff) |
Better control over result ordering. This fixes #12
Diffstat (limited to 'ext/bg/js/translator.js')
-rw-r--r-- | ext/bg/js/translator.js | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 2d15f05b..a49021c0 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -95,11 +95,11 @@ class Translator { return 1; } - const p1 = v1.popular; - const p2 = v2.popular; - if (p1 && !p2) { + const s1 = v1.score; + const s2 = v2.score; + if (s1 > s2) { return -1; - } else if (!p1 && p2) { + } else if (s1 < s2) { return 1; } @@ -154,31 +154,33 @@ class Translator { continue; } - let popular = false; + const tags = []; + for (const name of entry.tags) { + const tag = { + name, + class: 'default', + order: Number.MAX_SAFE_INTEGER, + score: 0, + desc: entry.entities[name] || '', + }; - let tags = []; - for (const t of entry.tags) { - const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: entry.entities[t] || '', name: t}; this.applyTagMeta(tag); tags.push(tag); + } - // - // TODO: Handle tagging as popular through data. - // - - if (t === 'P') { - popular = true; - } + let score = 0; + for (const tag of tags) { + score += tag.score; } groups[entry.id] = { + score, expression: entry.expression, - reading: entry.reading, - glossary: entry.glossary, - tags: Translator.sortTags(tags), - source: dfSource, - rules: dfRules, - popular: popular + reading: entry.reading, + glossary: entry.glossary, + tags: Translator.sortTags(tags), + source: dfSource, + rules: dfRules, }; } } @@ -188,18 +190,31 @@ class Translator { for (const entry of entries) { const tags = []; - for (const t of entry.tags) { - const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t}; + for (const name of entry.tags) { + const tag = { + name, + class: 'default', + order: Number.MAX_SAFE_INTEGER, + score: 0, + desc: '', + }; + this.applyTagMeta(tag); tags.push(tag); } + let score = 0; + for (const tag of tags) { + score += tag.score; + } + processed.push({ + score, character: entry.character, - kunyomi: entry.kunyomi, - onyomi: entry.onyomi, - tags: Translator.sortTags(tags), - glossary: entry.glossary + kunyomi: entry.kunyomi, + onyomi: entry.onyomi, + tags: Translator.sortTags(tags), + glossary: entry.glossary }); } |