Better control over result ordering. This fixes #12

This commit is contained in:
Alex Yatskov 2016-08-20 13:04:59 -07:00
parent 28e217b75b
commit 9649a83181
2 changed files with 49 additions and 34 deletions

View File

@ -26,12 +26,12 @@ class Deinflection {
} }
validate(validator) { validate(validator) {
for (let tags of validator(this.term)) { for (const tags of validator(this.term)) {
if (this.tags.length === 0) { if (this.tags.length === 0) {
return true; return true;
} }
for (let tag of this.tags) { for (const tag of this.tags) {
if (tags.indexOf(tag) !== -1) { if (tags.indexOf(tag) !== -1) {
return true; return true;
} }
@ -47,11 +47,11 @@ class Deinflection {
this.children.push(child); this.children.push(child);
} }
for (let rule in rules) { for (const rule in rules) {
const variants = rules[rule]; const variants = rules[rule];
for (let v of variants) { for (const v of variants) {
let allowed = this.tags.length === 0; let allowed = this.tags.length === 0;
for (let tag of this.tags) { for (const tag of this.tags) {
if (v.ti.indexOf(tag) !== -1) { if (v.ti.indexOf(tag) !== -1) {
allowed = true; allowed = true;
break; break;
@ -79,8 +79,8 @@ class Deinflection {
} }
const paths = []; const paths = [];
for (let child of this.children) { for (const child of this.children) {
for (let path of child.gather()) { for (const path of child.gather()) {
if (this.rule.length > 0) { if (this.rule.length > 0) {
path.rules.push(this.rule); path.rules.push(this.rule);
} }

View File

@ -95,11 +95,11 @@ class Translator {
return 1; return 1;
} }
const p1 = v1.popular; const s1 = v1.score;
const p2 = v2.popular; const s2 = v2.score;
if (p1 && !p2) { if (s1 > s2) {
return -1; return -1;
} else if (!p1 && p2) { } else if (s1 < s2) {
return 1; return 1;
} }
@ -154,31 +154,33 @@ class Translator {
continue; 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); this.applyTagMeta(tag);
tags.push(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] = { groups[entry.id] = {
score,
expression: entry.expression, expression: entry.expression,
reading: entry.reading, reading: entry.reading,
glossary: entry.glossary, glossary: entry.glossary,
tags: Translator.sortTags(tags), tags: Translator.sortTags(tags),
source: dfSource, source: dfSource,
rules: dfRules, rules: dfRules,
popular: popular
}; };
} }
} }
@ -188,13 +190,26 @@ class Translator {
for (const entry of entries) { for (const entry of entries) {
const tags = []; const tags = [];
for (const t of entry.tags) { for (const name of entry.tags) {
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t}; const tag = {
name,
class: 'default',
order: Number.MAX_SAFE_INTEGER,
score: 0,
desc: '',
};
this.applyTagMeta(tag); this.applyTagMeta(tag);
tags.push(tag); tags.push(tag);
} }
let score = 0;
for (const tag of tags) {
score += tag.score;
}
processed.push({ processed.push({
score,
character: entry.character, character: entry.character,
kunyomi: entry.kunyomi, kunyomi: entry.kunyomi,
onyomi: entry.onyomi, onyomi: entry.onyomi,