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