This commit is contained in:
Alex Yatskov 2016-08-20 13:42:12 -07:00
parent 9649a83181
commit 015c262bbf

View File

@ -62,7 +62,6 @@ class Translator {
const groups = {}; const groups = {};
for (let i = text.length; i > 0; --i) { for (let i = text.length; i > 0; --i) {
const term = text.slice(0, i); const term = text.slice(0, i);
const dfs = this.deinflector.deinflect(term, t => { const dfs = this.deinflector.deinflect(term, t => {
const tags = []; const tags = [];
for (const d of this.dictionary.findTerm(t)) { for (const d of this.dictionary.findTerm(t)) {
@ -136,15 +135,15 @@ class Translator {
return this.processKanji(definitions); return this.processKanji(definitions);
} }
processTerm(groups, dfSource, dfTags, dfRules=[], dfRoot='') { processTerm(groups, source, tags, rules, root) {
for (const entry of this.dictionary.findTerm(dfRoot)) { for (const entry of this.dictionary.findTerm(root)) {
if (entry.id in groups) { if (entry.id in groups) {
continue; continue;
} }
let matched = dfTags.length === 0; let matched = tags.length === 0;
for (const t of dfTags) { for (const tag of tags) {
if (entry.tags.indexOf(t) !== -1) { if (entry.tags.indexOf(tag) !== -1) {
matched = true; matched = true;
break; break;
} }
@ -154,71 +153,64 @@ class Translator {
continue; continue;
} }
const tags = []; const tagItems = [];
for (const name of entry.tags) { for (const tag of entry.tags) {
const tag = { const tagItem = {
name, name: tag,
class: 'default', class: 'default',
order: Number.MAX_SAFE_INTEGER, order: Number.MAX_SAFE_INTEGER,
score: 0, score: 0,
desc: entry.entities[name] || '', desc: entry.entities[tag] || '',
}; };
this.applyTagMeta(tag); this.applyTagMeta(tagItem);
tags.push(tag); tagItems.push(tagItem);
} }
let score = 0; let score = 0;
for (const tag of tags) { for (const tagItem of tagItems) {
score += tag.score; score += tagItem.score;
} }
groups[entry.id] = { groups[entry.id] = {
score, score,
source,
rules,
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(tagItems)
source: dfSource,
rules: dfRules,
}; };
} }
} }
processKanji(entries) { processKanji(entries) {
const processed = []; const results = [];
for (const entry of entries) { for (const entry of entries) {
const tags = []; const tagItems = [];
for (const name of entry.tags) { for (const tag of entry.tags) {
const tag = { const tagItem = {
name, name: tag,
class: 'default', class: 'default',
order: Number.MAX_SAFE_INTEGER, order: Number.MAX_SAFE_INTEGER,
score: 0,
desc: '', desc: '',
}; };
this.applyTagMeta(tag); this.applyTagMeta(tagItem);
tags.push(tag); tagItems.push(tagItem);
} }
let score = 0; results.push({
for (const tag of tags) {
score += tag.score;
}
processed.push({
score,
character: entry.character, character: entry.character,
kunyomi: entry.kunyomi, kunyomi: entry.kunyomi,
onyomi: entry.onyomi, onyomi: entry.onyomi,
tags: Translator.sortTags(tags), glossary: entry.glossary,
glossary: entry.glossary tags: Translator.sortTags(tagItems)
}); });
} }
return processed; return results;
} }
applyTagMeta(tag) { applyTagMeta(tag) {