From 9649a831814816adfed54f45cfceb338993bb2fb Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 20 Aug 2016 13:04:59 -0700 Subject: [PATCH] Better control over result ordering. This fixes #12 --- ext/bg/js/deinflector.js | 14 ++++---- ext/bg/js/translator.js | 69 ++++++++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/ext/bg/js/deinflector.js b/ext/bg/js/deinflector.js index bbb228b5..24289b0c 100644 --- a/ext/bg/js/deinflector.js +++ b/ext/bg/js/deinflector.js @@ -26,12 +26,12 @@ class Deinflection { } validate(validator) { - for (let tags of validator(this.term)) { + for (const tags of validator(this.term)) { if (this.tags.length === 0) { return true; } - for (let tag of this.tags) { + for (const tag of this.tags) { if (tags.indexOf(tag) !== -1) { return true; } @@ -47,11 +47,11 @@ class Deinflection { this.children.push(child); } - for (let rule in rules) { + for (const rule in rules) { const variants = rules[rule]; - for (let v of variants) { + for (const v of variants) { let allowed = this.tags.length === 0; - for (let tag of this.tags) { + for (const tag of this.tags) { if (v.ti.indexOf(tag) !== -1) { allowed = true; break; @@ -79,8 +79,8 @@ class Deinflection { } const paths = []; - for (let child of this.children) { - for (let path of child.gather()) { + for (const child of this.children) { + for (const path of child.gather()) { if (this.rule.length > 0) { path.rules.push(this.rule); } 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 }); }