mirror of
https://github.com/leon-ai/leon.git
synced 2024-11-23 20:12:08 +03:00
feat: dynamic variable binding on NLG
This commit is contained in:
parent
8280c65897
commit
0367b44f21
@ -3,13 +3,18 @@
|
||||
"red": {
|
||||
"synonyms": ["red"],
|
||||
"data": {
|
||||
"usage": "The red color is often used for danger"
|
||||
"usage": [
|
||||
"The red color is often used for danger"
|
||||
]
|
||||
}
|
||||
},
|
||||
"blue": {
|
||||
"synonyms": ["blue"],
|
||||
"data": {
|
||||
"usage": "The blue color is often used for that..."
|
||||
"usage": [
|
||||
"The blue color is often used for that...",
|
||||
"222 The blue color is often used for that..."
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import path from 'path'
|
||||
import log from '@/helpers/log'
|
||||
import lang from '@/helpers/lang'
|
||||
import domain from '@/helpers/domain'
|
||||
import string from '@/helpers/string'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
@ -58,7 +59,8 @@ export default () => new Promise(async (resolve, reject) => {
|
||||
const nluFilePath = path.join(currentSkill.path, 'nlu', `${lang}.json`)
|
||||
|
||||
if (fs.existsSync(nluFilePath)) {
|
||||
const { actions, entities } = JSON.parse(fs.readFileSync(nluFilePath, 'utf8'))
|
||||
const { actions, entities, variables } = JSON.parse(fs.readFileSync(nluFilePath, 'utf8'))
|
||||
// const { actions, entities } = loadNluFile(nluFilePath)
|
||||
const actionsKeys = Object.keys(actions)
|
||||
|
||||
for (let k = 0; k < actionsKeys.length; k += 1) {
|
||||
@ -74,7 +76,25 @@ export default () => new Promise(async (resolve, reject) => {
|
||||
|
||||
// Train NLG if the skill has a dialog type
|
||||
if (currentSkill.type === 'dialog') {
|
||||
const variablesObj = { }
|
||||
|
||||
// Dynamic variables binding if any variable is declared
|
||||
if (variables) {
|
||||
const variableKeys = Object.keys(variables)
|
||||
|
||||
for (let l = 0; l < variableKeys.length; l += 1) {
|
||||
const key = variableKeys[l]
|
||||
|
||||
variablesObj[`%${key}%`] = variables[variableKeys[l]]
|
||||
}
|
||||
}
|
||||
|
||||
for (let l = 0; l < answers?.length; l += 1) {
|
||||
const variableKeys = Object.keys(variablesObj)
|
||||
if (variableKeys.length > 0) {
|
||||
answers[l] = string.pnr(answers[l], variablesObj)
|
||||
}
|
||||
|
||||
nlp.addAnswer(lang, `${skillName}.${actionName}`, answers[l])
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user