From eb026cb74a6db89509930211b8f7ad642d90c54a Mon Sep 17 00:00:00 2001 From: louistiti Date: Mon, 20 Mar 2023 23:15:26 +0800 Subject: [PATCH] refactor(server): move some NER methods from NLU to NER --- server/src/core/nlp/nlu/ner.ts | 25 +++++++++++++++++++++++-- server/src/core/nlp/nlu/nlu.ts | 26 +++----------------------- server/src/pre-check.ts | 4 ++-- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/server/src/core/nlp/nlu/ner.ts b/server/src/core/nlp/nlu/ner.ts index 56f50dce..0b5efa73 100644 --- a/server/src/core/nlp/nlu/ner.ts +++ b/server/src/core/nlp/nlu/ner.ts @@ -8,7 +8,7 @@ import type { SkillCustomRegexEntityTypeSchema, SkillCustomTrimEntityTypeSchema } from '@/schemas/skill-schemas' -import { TCP_CLIENT } from '@/core' +import { BRAIN, MODEL_LOADER, TCP_CLIENT } from '@/core' import { LogHelper } from '@/helpers/log-helper' import { StringHelper } from '@/helpers/string-helper' @@ -126,10 +126,31 @@ export default class NER { }) } + /** + * Merge spaCy entities with the NER instance + */ + public async mergeSpacyEntities(utterance: NLPUtterance): Promise { + const spacyEntities = await this.getSpacyEntities(utterance) + + if (spacyEntities.length > 0) { + spacyEntities.forEach(({ entity, resolution }) => { + const spacyEntity = { + [entity]: { + options: { + [resolution.value]: [StringHelper.ucFirst(resolution.value)] + } + } + } + + MODEL_LOADER.mainNLPContainer.addEntities(spacyEntity, BRAIN.lang) + }) + } + } + /** * Get spaCy entities from the TCP server */ - public getSpacyEntities(utterance: NLPUtterance): Promise { + private getSpacyEntities(utterance: NLPUtterance): Promise { return new Promise((resolve) => { const spacyEntitiesReceivedHandler = async ({ spacyEntities }: { spacyEntities: NERSpacyEntity[] }): Promise => { diff --git a/server/src/core/nlp/nlu/nlu.ts b/server/src/core/nlp/nlu/nlu.ts index 14d2b898..f3d82261 100644 --- a/server/src/core/nlp/nlu/nlu.ts +++ b/server/src/core/nlp/nlu/nlu.ts @@ -5,6 +5,7 @@ import { spawn } from 'node:child_process' import axios from 'axios' import kill from 'tree-kill' +import type { ShortLanguageCode } from '@/types' import type { NLPUtterance, NLUResult } from '@/core/nlp/types' import { langs } from '@@/core/langs.json' import { version } from '@@/package.json' @@ -49,7 +50,7 @@ export default class NLU { /** * Set new language; recreate a new TCP server with new language; and reprocess understanding */ - private switchLanguage(utterance, locale) { + private switchLanguage(utterance: NLPUtterance, locale: ShortLanguageCode) { const connectedHandler = async (): Promise => { await this.process(utterance) } @@ -90,27 +91,6 @@ export default class NLU { } } - /** - * Merge spaCy entities with the current NER instance - */ - private async mergeSpacyEntities(utterance: NLPUtterance): Promise { - const spacyEntities = await NER.getSpacyEntities(utterance) - - if (spacyEntities.length > 0) { - spacyEntities.forEach(({ entity, resolution }) => { - const spacyEntity = { - [entity]: { - options: { - [resolution.value]: [StringHelper.ucFirst(resolution.value)] - } - } - } - - MODEL_LOADER.mainNLPContainer.addEntities(spacyEntity, BRAIN.lang) - }) - } - } - /** * Handle in action loop logic before NLU processing */ @@ -307,7 +287,7 @@ export default class NLU { } // Add spaCy entities - await this.mergeSpacyEntities(utterance) + await NER.mergeSpacyEntities(utterance) // Pre NLU processing according to the active context if there is one if (this.conversation.hasActiveContext()) { diff --git a/server/src/pre-check.ts b/server/src/pre-check.ts index 32d0bfb8..ddd4f92a 100644 --- a/server/src/pre-check.ts +++ b/server/src/pre-check.ts @@ -39,12 +39,12 @@ interface ObjectUnknown { const validateSchema = ( schema: ObjectUnknown, contentToValidate: ObjectUnknown, - customErrorMesage: string + customErrorMessage: string ): void => { const validate = ajv.compile(schema) const isValid = validate(contentToValidate) if (!isValid) { - LogHelper.error(customErrorMesage) + LogHelper.error(customErrorMessage) const errors = new AggregateAjvError(validate.errors ?? []) for (const error of errors) { LogHelper.error(error.message)