1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-27 08:06:03 +03:00

refactor(server): spaCy entities extracting

This commit is contained in:
louistiti 2022-03-22 23:40:35 +08:00
parent 1d9b18093b
commit 368c57928a
No known key found for this signature in database
GPG Key ID: 7ECA3DD523793FE6
2 changed files with 21 additions and 25 deletions

View File

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

View File

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