1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-24 04:31:31 +03:00

refactor(server): move some NER methods from NLU to NER

This commit is contained in:
louistiti 2023-03-20 23:15:26 +08:00
parent 7d6698df17
commit eb026cb74a
No known key found for this signature in database
GPG Key ID: 0A1C3B043E70C77D
3 changed files with 28 additions and 27 deletions

View File

@ -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<void> {
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<NERSpacyEntity[]> {
private getSpacyEntities(utterance: NLPUtterance): Promise<NERSpacyEntity[]> {
return new Promise((resolve) => {
const spacyEntitiesReceivedHandler =
async ({ spacyEntities }: { spacyEntities: NERSpacyEntity[] }): Promise<void> => {

View File

@ -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<void> => {
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<void> {
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()) {

View File

@ -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)