From efc2a3a7c0c4992d901716c805f59a3f50868037 Mon Sep 17 00:00:00 2001 From: louistiti Date: Tue, 5 Apr 2022 10:41:18 +0800 Subject: [PATCH] refactor: normalize entities --- server/src/core/conversation.js | 5 ++++- server/src/core/ner.js | 16 +++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/server/src/core/conversation.js b/server/src/core/conversation.js index 70b8fc5c..e6278b0a 100644 --- a/server/src/core/conversation.js +++ b/server/src/core/conversation.js @@ -56,7 +56,7 @@ class Conversation { entities } = contextObj const slotKeys = Object.keys(slots) - + // If slots are required to trigger next actions, then go through the context activation if (slotKeys.length > 0) { const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8')) @@ -83,6 +83,9 @@ class Conversation { originalUtterance: contextObj.originalUtterance, activatedAt: Date.now() } + + log.title('Conversation') + log.info(`New active context: ${outputContext}`) } this.setSlots(lang, entities, slots) diff --git a/server/src/core/ner.js b/server/src/core/ner.js index 62fcfb4a..a5784932 100644 --- a/server/src/core/ner.js +++ b/server/src/core/ner.js @@ -56,12 +56,18 @@ class Ner { const { entities } = await this.ner.process({ locale: lang, text: utterance }) - // Trim whitespace at the beginning and the end of the entity value - entities.map((e) => { - e.sourceText = e.sourceText.trim() - e.utteranceText = e.utteranceText.trim() + // Normalize entities + entities.map((entity) => { + // Trim whitespace at the beginning and the end of the entity value + entity.sourceText = entity.sourceText.trim() + entity.utteranceText = entity.utteranceText.trim() - return e + // Add resolution property to stay consistent with all entities + if (!entity.resolution) { + entity.resolution = { value: entity.sourceText } + } + + return entity }) if (entities.length > 0) {