Fixes to make deinflection work

This commit is contained in:
Alex Yatskov 2016-03-23 18:12:33 -07:00
parent a9c92bdfbf
commit c308370658
3 changed files with 41 additions and 23 deletions

View File

@ -47,7 +47,8 @@ class Deinflection {
this.children.push(child); this.children.push(child);
} }
for (const [rule, variants] of rules) { for (const rule in rules) {
const variants = rules[rule];
for (const v of variants) { for (const v of variants) {
let allowed = this.tags.length === 0; let allowed = this.tags.length === 0;
for (const tag in this.tags) { for (const tag in this.tags) {
@ -61,10 +62,10 @@ class Deinflection {
continue; continue;
} }
const term = self.term.slice(0, -v.kanaIn.length) + v.kanaOut; const term = this.term.slice(0, -v.kanaIn.length) + v.kanaOut;
const child = new Deinflection(term, v.tagsOut, rule); const child = new Deinflection(term, v.tagsOut, rule);
if (child.deinflect(validator, rules)) { if (child.deinflect(validator, rules)) {
this.children.append(child); this.children.push(child);
} }
} }
} }
@ -89,9 +90,9 @@ class Deinflection {
const paths = []; const paths = [];
for (const child of this.children) { for (const child of this.children) {
for (const path in child.gather()) { for (const path of child.gather()) {
if (this.rule.length > 0) { if (this.rule.length > 0) {
path.rules.append(this.rule); path.rules.push(this.rule);
} }
path.source = this.term; path.source = this.term;

View File

@ -56,7 +56,13 @@ class Dictionary {
findTermInDict(term, dict) { findTermInDict(term, dict) {
return (dict.indices[term] || []).map(index => { return (dict.indices[term] || []).map(index => {
const [e, r, g, t] = dict.defs[index]; const [e, r, g, t] = dict.defs[index];
return {id: index, expression: e, reading: r, glossary: g, tags: t}; return {
id: index,
expression: e,
reading: r,
glossary: g,
tags: t
};
}); });
} }
@ -67,6 +73,12 @@ class Dictionary {
} }
const [c, k, o, g] = def; const [c, k, o, g] = def;
return {id: kanji.charCodeAt(0), character: c, kunyomi: k, onyomi: o, glossary: g}; return {
id: kanji.charCodeAt(0),
character: c,
kunyomi: k,
onyomi: o,
glossary: g
};
} }
} }

View File

@ -36,7 +36,7 @@ class Translator {
} }
$.when.apply($, loaders).done((rules, edict, enamdict, kanjidic) => { $.when.apply($, loaders).done((rules, edict, enamdict, kanjidic) => {
this.deinflector.setRules(rules); this.deinflector.setRules(rules[0]);
this.dictionary.addTermDict(edict[0]); this.dictionary.addTermDict(edict[0]);
this.dictionary.addTermDict(enamdict[0]); this.dictionary.addTermDict(enamdict[0]);
@ -71,8 +71,14 @@ class Translator {
this.processTerm(groups, df.source, df.rules, df.root); this.processTerm(groups, df.source, df.rules, df.root);
} }
} }
}
const results = groups.sort(resultSorter); let results = [];
for (const key in groups) {
results.push(groups[key]);
}
results = results.sort(this.resultSorter);
let length = 0; let length = 0;
for (const result of results) { for (const result of results) {
@ -81,7 +87,6 @@ class Translator {
return {results: results, length: length}; return {results: results, length: length};
} }
}
findKanji(text) { findKanji(text) {
let results = []; let results = [];
@ -115,27 +120,27 @@ class Translator {
const sl2 = v2.source.length; const sl2 = v2.source.length;
if (sl1 > sl2) { if (sl1 > sl2) {
return -1;
} else if (sl1 > sl2) {
return 1; return 1;
} else if (sl1 > sl2) {
return -1;
} }
const p1 = v1.tags.indexOf('P') >= 0; const p1 = v1.tags.indexOf('P') >= 0;
const p2 = v2.tags.indexOf('P') >= 0; const p2 = v2.tags.indexOf('P') >= 0;
if (p1 && !p2) { if (p1 && !p2) {
return -1;
} else if (!p1 && p2) {
return 1; return 1;
} else if (!p1 && p2) {
return -1;
} }
const rl1 = v1.rules.length; const rl1 = v1.rules.length;
const rl2 = v2.rules.length; const rl2 = v2.rules.length;
if (rl1 < rl2) { if (rl1 < rl2) {
return -1;
} else if (rl2 > rl1) {
return 1; return 1;
} else if (rl2 > rl1) {
return -1;
} }
return 0; return 0;