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

View File

@ -151,49 +151,84 @@ 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] = {
expression: entry.expression,
reading: entry.reading,
glossary: entry.glossary,
tags: Translator.sortTags(tags),
source: dfSource,
rules: dfRules,
popular: popular
};
}
}
processKanji(entries) {
const processed = [];
for (let entry of entries) {
const tags = [];
for (let t of entry.tags) {
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t};
this.applyTagMeta(tag);
tags.push(tag);
}
processed.push({
character: entry.character,
kunyomi: entry.kunyomi,
onyomi: entry.onyomi,
tags: Translator.sortTags(tags),
glossary: entry.glossary
});
}
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 order1 = v1.order;
const order2 = v2.order; const order2 = v2.order;
if (order1 < order2) { if (order1 < order2) {
@ -212,52 +247,6 @@ class Translator {
return 0; 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) {
const results = [];
for (let entry of entries) {
const tagItems = [];
for (let tag of entry.tags) {
const tagItem = {
class: 'default',
order: Number.MAX_SAFE_INTEGER,
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({
character: entry.character,
kunyomi: entry.kunyomi,
onyomi: entry.onyomi,
tags: tagItems,
glossary: entry.glossary
});
}
return results;
} }
static isKanji(c) { static isKanji(c) {