This commit is contained in:
Alex Yatskov 2016-08-07 22:42:56 -07:00
parent 29135ff84f
commit 6c838b7fb4

View File

@ -151,113 +151,102 @@ class Translator {
return this.processKanji(definitions); return this.processKanji(definitions);
} }
processTerm(groups, source, tags, rules=[], root='') { processTerm(groups, dfSource, dfTags, dfRules=[], dfRoot='') {
for (let entry of this.dictionary.findTerm(root)) { for (let entry of this.dictionary.findTerm(dfRoot)) {
if (entry.id in groups) { if (entry.id in groups) {
continue; continue;
} }
let matched = tags.length == 0; let matched = dfTags.length === 0;
for (let tag of tags) { for (let t of dfTags) {
if (entry.tags.indexOf(tag) !== -1) { if (entry.tags.indexOf(t) !== -1) {
matched = true; matched = true;
break; break;
} }
} }
if (!matched) {
continue;
}
let popular = false; let popular = false;
let tagItems = [];
for (let tag of entry.tags) {
const tagItem = {
class: 'default',
order: Number.MAX_SAFE_INTEGER,
desc: entry.entities[tag] || '',
name: tag
};
const tagMeta = this.tagMeta[tag]; let tags = [];
if (tagMeta) { for (let t of entry.tags) {
for (const tagProp in tagMeta) { const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: entry.entities[t] || '', name: t};
tagItem[tagProp] = tagMeta[tagProp] || tagItem[tagProp]; this.applyTagMeta(tag);
} tags.push(tag);
}
tagItems.push(tagItem);
// //
// TODO: Handle tagging as popular through data. // TODO: Handle tagging as popular through data.
// //
if (tag === 'P') { if (t === 'P') {
popular = true; popular = true;
} }
} }
tagItems = tagItems.sort((v1, v2) => { groups[entry.id] = {
const order1 = v1.order; expression: entry.expression,
const order2 = v2.order; reading: entry.reading,
if (order1 < order2) { glossary: entry.glossary,
return -1; tags: Translator.sortTags(tags),
} else if (order1 > order2) { source: dfSource,
return 1; rules: dfRules,
} popular: popular
};
const name1 = v1.name;
const name2 = v2.name;
if (name1 < name2) {
return -1;
} else if (name1 > name2) {
return 1;
}
return 0;
});
if (matched) {
groups[entry.id] = {
expression: entry.expression,
reading: entry.reading,
glossary: entry.glossary,
tags: tagItems,
source: source,
rules: rules,
popular: popular
};
}
} }
} }
processKanji(entries) { processKanji(entries) {
const results = []; const processed = [];
for (let entry of entries) { for (let entry of entries) {
const tagItems = []; const tags = [];
for (let tag of entry.tags) { for (let t of entry.tags) {
const tagItem = { const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t};
class: 'default', this.applyTagMeta(tag);
order: Number.MAX_SAFE_INTEGER, tags.push(tag);
name: tag
};
const tagMeta = this.tagMeta[tag];
if (tagMeta) {
for (const tagProp in tagMeta) {
tagItem[tagProp] = tagMeta[tagProp] || tagItem[tagProp];
}
}
tagItems.push(tagItem);
} }
results.push({ processed.push({
character: entry.character, character: entry.character,
kunyomi: entry.kunyomi, kunyomi: entry.kunyomi,
onyomi: entry.onyomi, onyomi: entry.onyomi,
tags: tagItems, tags: Translator.sortTags(tags),
glossary: entry.glossary glossary: entry.glossary
}); });
} }
return results; return processed;
}
applyTagMeta(tag) {
for (const prop in this.tagMeta[tag.name] || {}) {
tag[prop] = this.tagMeta[tag.name][prop] || tag[prop];
}
}
static sortTags(tags) {
return tags.sort((v1, v2) => {
const order1 = v1.order;
const order2 = v2.order;
if (order1 < order2) {
return -1;
} else if (order1 > order2) {
return 1;
}
const name1 = v1.name;
const name2 = v2.name;
if (name1 < name2) {
return -1;
} else if (name1 > name2) {
return 1;
}
return 0;
});
} }
static isKanji(c) { static isKanji(c) {