From 3fda3526c7425bdea4b669474fa77efd61c06a8e Mon Sep 17 00:00:00 2001 From: Louistiti Date: Sat, 11 May 2019 18:09:05 +0800 Subject: [PATCH] feat(server): add regex entity type --- packages/calendar/data/expressions/en.json | 4 ++-- server/src/core/ner.js | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/calendar/data/expressions/en.json b/packages/calendar/data/expressions/en.json index d59c7da0e..e5776a61b 100644 --- a/packages/calendar/data/expressions/en.json +++ b/packages/calendar/data/expressions/en.json @@ -8,8 +8,8 @@ "entities": [ { "type": "regex", - "name": "", - "regex": "" + "name": "color", + "regex": "red|blue|green" }, { "type": "trim", diff --git a/server/src/core/ner.js b/server/src/core/ner.js index 2c4ef0e67..a257eb77c 100644 --- a/server/src/core/ner.js +++ b/server/src/core/ner.js @@ -53,7 +53,7 @@ class Ner { if (!this.supportedEntityTypes.includes(entity.type)) { reject({ type: 'warning', obj: new Error(`"${entity.type}" action entity type not supported`), code: 'random_ner_type_not_supported', data: { '%entity_type%': entity.type } }) } else if (entity.type === 'regex') { - // TODO: regex case + promises.push(this.injectRegexEntity(lang, entity)) } else if (entity.type === 'trim') { promises.push(this.injectTrimEntity(lang, entity)) } @@ -83,7 +83,6 @@ class Ner { */ injectTrimEntity (lang, entity) { return new Promise((resolve) => { - console.log('addnamedentity', entity.name, entity.type) const e = this.nerManager.addNamedEntity(entity.name, entity.type) for (let j = 0; j < entity.conditions.length; j += 1) { @@ -103,6 +102,19 @@ class Ner { resolve() }) } + + /** + * Inject regex type entities + */ + injectRegexEntity (lang, entity) { + return new Promise((resolve) => { + const e = this.nerManager.addNamedEntity(entity.name, entity.type) + + e.addRegex(lang, new RegExp(entity.regex, 'g')) + + resolve() + }) + } } export default Ner