mirror of
https://github.com/leon-ai/leon.git
synced 2024-11-28 04:04:58 +03:00
refactor(server): move some NER methods from NLU to NER
This commit is contained in:
parent
7d6698df17
commit
eb026cb74a
@ -8,7 +8,7 @@ import type {
|
|||||||
SkillCustomRegexEntityTypeSchema,
|
SkillCustomRegexEntityTypeSchema,
|
||||||
SkillCustomTrimEntityTypeSchema
|
SkillCustomTrimEntityTypeSchema
|
||||||
} from '@/schemas/skill-schemas'
|
} from '@/schemas/skill-schemas'
|
||||||
import { TCP_CLIENT } from '@/core'
|
import { BRAIN, MODEL_LOADER, TCP_CLIENT } from '@/core'
|
||||||
import { LogHelper } from '@/helpers/log-helper'
|
import { LogHelper } from '@/helpers/log-helper'
|
||||||
import { StringHelper } from '@/helpers/string-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
|
* Get spaCy entities from the TCP server
|
||||||
*/
|
*/
|
||||||
public getSpacyEntities(utterance: NLPUtterance): Promise<NERSpacyEntity[]> {
|
private getSpacyEntities(utterance: NLPUtterance): Promise<NERSpacyEntity[]> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const spacyEntitiesReceivedHandler =
|
const spacyEntitiesReceivedHandler =
|
||||||
async ({ spacyEntities }: { spacyEntities: NERSpacyEntity[] }): Promise<void> => {
|
async ({ spacyEntities }: { spacyEntities: NERSpacyEntity[] }): Promise<void> => {
|
||||||
|
@ -5,6 +5,7 @@ import { spawn } from 'node:child_process'
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import kill from 'tree-kill'
|
import kill from 'tree-kill'
|
||||||
|
|
||||||
|
import type { ShortLanguageCode } from '@/types'
|
||||||
import type { NLPUtterance, NLUResult } from '@/core/nlp/types'
|
import type { NLPUtterance, NLUResult } from '@/core/nlp/types'
|
||||||
import { langs } from '@@/core/langs.json'
|
import { langs } from '@@/core/langs.json'
|
||||||
import { version } from '@@/package.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
|
* 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> => {
|
const connectedHandler = async (): Promise<void> => {
|
||||||
await this.process(utterance)
|
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
|
* Handle in action loop logic before NLU processing
|
||||||
*/
|
*/
|
||||||
@ -307,7 +287,7 @@ export default class NLU {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add spaCy entities
|
// Add spaCy entities
|
||||||
await this.mergeSpacyEntities(utterance)
|
await NER.mergeSpacyEntities(utterance)
|
||||||
|
|
||||||
// Pre NLU processing according to the active context if there is one
|
// Pre NLU processing according to the active context if there is one
|
||||||
if (this.conversation.hasActiveContext()) {
|
if (this.conversation.hasActiveContext()) {
|
||||||
|
@ -39,12 +39,12 @@ interface ObjectUnknown {
|
|||||||
const validateSchema = (
|
const validateSchema = (
|
||||||
schema: ObjectUnknown,
|
schema: ObjectUnknown,
|
||||||
contentToValidate: ObjectUnknown,
|
contentToValidate: ObjectUnknown,
|
||||||
customErrorMesage: string
|
customErrorMessage: string
|
||||||
): void => {
|
): void => {
|
||||||
const validate = ajv.compile(schema)
|
const validate = ajv.compile(schema)
|
||||||
const isValid = validate(contentToValidate)
|
const isValid = validate(contentToValidate)
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
LogHelper.error(customErrorMesage)
|
LogHelper.error(customErrorMessage)
|
||||||
const errors = new AggregateAjvError(validate.errors ?? [])
|
const errors = new AggregateAjvError(validate.errors ?? [])
|
||||||
for (const error of errors) {
|
for (const error of errors) {
|
||||||
LogHelper.error(error.message)
|
LogHelper.error(error.message)
|
||||||
|
Loading…
Reference in New Issue
Block a user