1
1
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:
louistiti 2022-03-02 00:16:03 +08:00
parent 8280c65897
commit 0367b44f21
No known key found for this signature in database
GPG Key ID: 7ECA3DD523793FE6
2 changed files with 28 additions and 3 deletions

View File

@ -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..."
]
}
}
}

View File

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