This commit is contained in:
Alex Yatskov 2016-03-20 20:34:50 -07:00
parent 1ed2e70c6f
commit f784462c4d

View File

@ -51,63 +51,52 @@ class Translator {
} }
findTerm(text) { findTerm(text) {
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 deinflections = this.deinflector.deinflect(term, this.validator); const dfs = this.deinflector.deinflect(term, this.validator);
if (deinflections === null) { if (dfs === null) {
this.processTerm(groups, term); this.processTerm(groups, term);
} else { } else {
for (const deinflection of deinflections) { for (const df of dfs) {
//fix this.processTerm(groups, df.source, df.rules, df.root);
//this.processTerm(groups, **deinflection);
} }
} }
const results = const results = formatResults(groups).sort(resultSorter);
let length = 0;
for (const result of results) {
length = Math.max(length, result.source.length);
}
return {results: results, length: length};
} }
// text = util.sanitize(text, wildcards=wildcards)
// groups = dict()
// for i in xrange(len(text), 0, -1):
// term = text[:i]
// deinflections = self.deinflector.deinflect(term, self.validator)
// if deinflections is None:
// self.processTerm(groups, term, wildcards=wildcards)
// else:
// for deinflection in deinflections:
// self.processTerm(groups, **deinflection)
// results = map(self.formatResult, groups.items())
// results = filter(operator.truth, results)
// results = sorted(results, key=lambda d: (len(d['source']), 'P' in d['tags'], -len(d['rules'])), reverse=True)
// length = 0
// for result in results:
// length = max(length, len(result['source']))
// return results, length
} }
findKanji(text) { findKanji(text) {
// text = util.sanitize(text, kana=False) let results = [];
// results = list()
// processed = dict() const processed = {};
// for c in text: for (const c of text) {
// if c not in processed: if (!processed.has(c)) {
// match = self.dictionary.findCharacter(c) results = results.concat(this.dictionary.findKanji(c));
// if match is not None: processed[c] = true;
// results.append(match) }
// processed[c] = match }
// return results return results;
} }
processTerm(groups, source, rules=[], root='') { processTerm(groups, source, rules=[], root='') {
root = root || source;
// for (const entry of this.dictionary.findTerm(root)) {
// const key =
// }
// root = root or source // root = root or source
// for entry in self.dictionary.findTerm(root, wildcards): // for entry in self.dictionary.findTerm(root, wildcards):
@ -117,12 +106,53 @@ class Translator {
} }
formatResult(group) { formatResult(group) {
// root = root or source const results = [];
for (const [key, value] of groups) {
[expression, reading, glossary] = key;
[tags, source, rules] = group;
// for entry in self.dictionary.findTerm(root, wildcards): results.push({
// key = entry['expression'], entry['reading'], entry['glossary'] expression: expression,
// if key not in groups: reading: reading,
// groups[key] = entry['tags'], source, rules glossary: glossary,
rules: rules,
source: source,
tags: tags
});
}
return results;
}
resultSorter(v1, v2) {
const sl1 = v1.source.length;
const sl2 = v2.source.length;
if (sl1 > sl2) {
return -1;
} else if (sl1 > sl2) {
return 1;
}
const p1 = v1.tags.indexOf('P') >= 0;
const p2 = v2.tags.indexOf('P') >= 0;
if (p1 && !p2) {
return -1;
} else if (!p1 && p2) {
return 1;
}
const rl1 = v1.rules.length;
const rl2 = v2.rules.length;
if (rl1 < rl2) {
return -1;
} else if (rl2 > rl1) {
return 1;
}
return 0;
} }
validator(term) { validator(term) {