This commit is contained in:
Alex Yatskov 2016-09-10 19:36:28 -07:00
parent 05ac931285
commit fa391bd5d3

View File

@ -54,28 +54,27 @@ class Translator {
}); });
} }
findTerm(text) { findTermGroups(text) {
const groups = {}; const groups = {};
const promises = [];
for (let i = text.length; i > 0; --i) { for (let i = text.length; i > 0; --i) {
const term = text.slice(0, i); promises.append(
const dfs = this.deinflector.deinflect(term, t => { this.deinflector.deinflect(text.slice(0, i), term => {
const tagSets = []; return this.dictionary.findTerm(term).then(definitions => definitions.map(def => def.tags));
for (const d of this.dictionary.findTerm(t)) { }).then(inflects => {
tagSets.push(d.tags); for (const inflect of inflects || []) {
}
return tagSets;
});
if (dfs === null) {
continue;
}
for (const df of dfs) {
this.processTerm(groups, df.source, df.tags, df.rules, df.root); this.processTerm(groups, df.source, df.tags, df.rules, df.root);
} }
})
);
} }
return Promise.all(promises).then(Promise.resolve(groups));
}
findTerm(text) {
return this.findTermGroups(text).then(groups => {
let definitions = []; let definitions = [];
for (const key in groups) { for (const key in groups) {
definitions.push(groups[key]); definitions.push(groups[key]);
@ -115,6 +114,7 @@ class Translator {
} }
return {definitions, length}; return {definitions, length};
});
} }
findKanji(text) { findKanji(text) {
@ -132,14 +132,15 @@ class Translator {
} }
processTerm(groups, source, tags, rules, root) { processTerm(groups, source, tags, rules, root) {
for (const entry of this.dictionary.findTerm(root)) { return this.dictionary.findTerm(root).then(definitions => {
if (entry.id in groups) { for (const definition of definitions) {
if (definition.id in groups) {
continue; continue;
} }
let matched = tags.length === 0; let matched = tags.length === 0;
for (const tag of tags) { for (const tag of tags) {
if (entry.tags.indexOf(tag) !== -1) { if (definition.tags.indexOf(tag) !== -1) {
matched = true; matched = true;
break; break;
} }
@ -150,13 +151,13 @@ class Translator {
} }
const tagItems = []; const tagItems = [];
for (const tag of entry.tags) { for (const tag of definition.tags) {
const tagItem = { const tagItem = {
name: tag, name: tag,
class: 'default', class: 'default',
order: Number.MAX_SAFE_INTEGER, order: Number.MAX_SAFE_INTEGER,
score: 0, score: 0,
desc: entry.entities[tag] || '', desc: definition.entities[tag] || '',
}; };
this.applyTagMeta(tagItem); this.applyTagMeta(tagItem);
@ -168,16 +169,17 @@ class Translator {
score += tagItem.score; score += tagItem.score;
} }
groups[entry.id] = { groups[definition.id] = {
score, score,
source, source,
rules, rules,
expression: entry.expression, expression: definition.expression,
reading: entry.reading, reading: definition.reading,
glossary: entry.glossary, glossary: definition.glossary,
tags: Translator.sortTags(tagItems) tags: Translator.sortTags(tagItems)
}; };
} }
});
} }
processKanji(entries) { processKanji(entries) {