From 368c57928a59230b2329dd62e88a0f8a7f4ef845 Mon Sep 17 00:00:00 2001 From: louistiti Date: Tue, 22 Mar 2022 23:40:35 +0800 Subject: [PATCH] refactor(server): spaCy entities extracting --- server/src/core/ner.js | 16 ++++++---------- server/src/core/nlu.js | 30 +++++++++++++++--------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/server/src/core/ner.js b/server/src/core/ner.js index 53642b54..8f57e224 100644 --- a/server/src/core/ner.js +++ b/server/src/core/ner.js @@ -11,7 +11,6 @@ import string from '@/helpers/string' class Ner { constructor (ner) { this.ner = ner - this._spacyEntities = [] log.title('NER') log.success('New instance') @@ -55,7 +54,7 @@ class Ner { await Promise.all(promises) - let { entities } = await this.ner.process({ locale: lang, text: utterance }) + 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) => { @@ -65,9 +64,6 @@ class Ner { return e }) - // Merge with spaCy entities - entities = entities.concat(this._spacyEntities) - if (entities.length > 0) { Ner.logExtraction(entities) return resolve(entities) @@ -79,15 +75,15 @@ class Ner { }) } - getSpacyEntities (utterance) { + /** + * Get spaCy entities from the TCP server + */ + static getSpacyEntities (utterance) { return new Promise((resolve) => { const spacyEntitiesReceivedHandler = async ({ spacyEntities }) => { - this._spacyEntities = spacyEntities - - resolve() + resolve(spacyEntities) } - this._spacyEntities = [] global.tcpClient.ee.removeAllListeners() global.tcpClient.ee.on('spacy-entities-received', spacyEntitiesReceivedHandler) diff --git a/server/src/core/nlu.js b/server/src/core/nlu.js index e71452b4..a2d63636 100644 --- a/server/src/core/nlu.js +++ b/server/src/core/nlu.js @@ -95,7 +95,22 @@ class Nlu { return reject(msg) } + // Add spaCy entities + const spacyEntities = await Ner.getSpacyEntities(utterance) + if (spacyEntities.length > 0) { + const [{ entity, resolution }] = spacyEntities + const spacyEntity = { + [entity]: { + options: { + [resolution.value]: [resolution.value] + } + } + } + this.nlp.addEntities(spacyEntity, this.brain.lang) + } + const result = await this.nlp.process(utterance) + const { locale, domain, intent, score, answers } = result @@ -192,7 +207,6 @@ class Nlu { obj.skillType = skillType try { - await this.ner.getSpacyEntities(utterance) obj.entities = await this.ner.extractEntities( this.brain.lang, join(process.cwd(), 'skills', obj.classification.domain, obj.classification.skill, `nlu/${this.brain.lang}.json`), @@ -209,20 +223,6 @@ class Nlu { } try { - const [{ entity, resolution }] = obj.entities - const testoEntity = { - [entity]: { - options: { - [resolution.value]: [resolution.value] - } - } - } - this.nlp.addEntities(testoEntity, this.brain.lang) - - const result2 = await this.nlp.process(utterance) - - console.log('result2', result2) - // Inject action entities with the others if there is const data = await this.brain.execute(obj, { mute: opts.mute }) const processingTimeEnd = Date.now()