diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-09-19 20:17:58 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-09-19 20:17:58 -0700 |
commit | 7666d6a3e6b43f102f79d9f684f4cb5195790c67 (patch) | |
tree | 622bd6c710dc1be5eae2f8f777ea295fd56c17e7 | |
parent | 396ff944a48ff0bef72f4a2fa99d686768497e98 (diff) |
Cleanup
-rw-r--r-- | ext/bg/js/translator.js | 72 | ||||
-rw-r--r-- | ext/bg/js/util.js | 60 |
2 files changed, 69 insertions, 63 deletions
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index e534e0cb..9711cf8d 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -107,45 +107,20 @@ class Translator { findTerm(text) { return this.findTermGroups(text).then(deinflectGroups => { - let definitions = []; + const definitions = []; for (const key in deinflectGroups) { definitions.push(deinflectGroups[key]); } - definitions = definitions.sort((v1, v2) => { - const sl1 = v1.source.length; - const sl2 = v2.source.length; - if (sl1 > sl2) { - return -1; - } else if (sl1 < sl2) { - return 1; - } - - const s1 = v1.score; - const s2 = v2.score; - if (s1 > s2) { - return -1; - } else if (s1 < s2) { - return 1; - } - - const rl1 = v1.rules.length; - const rl2 = v2.rules.length; - if (rl1 < rl2) { - return -1; - } else if (rl1 > rl2) { - return 1; - } - - return v2.expression.localeCompare(v1.expression); - }); - let length = 0; for (const result of definitions) { length = Math.max(length, result.source.length); } - return {definitions, length}; + return { + length, + definitions: sortTermDefs(definitions) + }; }); } @@ -192,7 +167,7 @@ class Translator { desc: definition.entities[tag] || '', }; - this.applyTagMeta(tagItem); + applyTagMeta(tagItem, this.tagMeta); tagItems.push(tagItem); } @@ -208,7 +183,7 @@ class Translator { expression: definition.expression, reading: definition.reading, glossary: definition.glossary, - tags: Translator.sortTags(tagItems) + tags: sortTags(tagItems) }; } }); @@ -225,42 +200,13 @@ class Translator { desc: '', }; - this.applyTagMeta(tagItem); + applyTagMeta(tagItem, this.tagMeta); tagItems.push(tagItem); } - definition.tags = Translator.sortTags(tagItems); + definition.tags = sortTags(tagItems); } return definitions; } - - applyTagMeta(tag) { - const symbol = tag.name.split(':')[0]; - for (const prop in this.tagMeta[symbol] || {}) { - tag[prop] = this.tagMeta[symbol][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; - }); - } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 5583502d..95d1b43e 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -44,3 +44,63 @@ function isKanji(c) { return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; } +function 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; + }); +} + +function sortTermDefs(definitions) { + return definitions.sort((v1, v2) => { + const sl1 = v1.source.length; + const sl2 = v2.source.length; + if (sl1 > sl2) { + return -1; + } else if (sl1 < sl2) { + return 1; + } + + const s1 = v1.score; + const s2 = v2.score; + if (s1 > s2) { + return -1; + } else if (s1 < s2) { + return 1; + } + + const rl1 = v1.rules.length; + const rl2 = v2.rules.length; + if (rl1 < rl2) { + return -1; + } else if (rl1 > rl2) { + return 1; + } + + return v2.expression.localeCompare(v1.expression); + }); +} + +function applyTagMeta(tag, meta) { + const symbol = tag.name.split(':')[0]; + for (const prop in meta[symbol] || {}) { + tag[prop] = meta[symbol][prop]; + } + + return tag; +} |