Skip to content

Commit

Permalink
feat: dynamic variable binding on NLG
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Mar 1, 2022
1 parent 8280c65 commit 0367b44
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 7 additions & 2 deletions core/data/en/colors.json
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
]
}
}
}
Expand Down
22 changes: 21 additions & 1 deletion scripts/train.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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) {
Expand All @@ -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])
}
}
Expand Down

0 comments on commit 0367b44

Please sign in to comment.