Skip to content

Commit

Permalink
feat(server): more accurate NLG
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Feb 28, 2022
1 parent 28efe6e commit d5577b1
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
5 changes: 5 additions & 0 deletions core/skills-endpoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"route": "/api/action/leon/color/color_meaning",
"params": []
},
{
"method": "GET",
"route": "/api/action/leon/color/specific_color_meaning",
"params": []
},
{
"method": "GET",
"route": "/api/action/leon/color/why",
Expand Down
30 changes: 25 additions & 5 deletions server/src/core/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ class Brain {
)
const { entities, actions } = JSON.parse(fs.readFileSync(nluFilePath, 'utf8'))
const utteranceHasEntities = obj.entities.length > 0
let { answers } = obj
const { answers: rawAnswers } = obj
let answers = rawAnswers
let answer = ''

if (!utteranceHasEntities) {
Expand All @@ -333,10 +334,15 @@ class Brain {
answers = answers.filter(({ answer }) => answer.indexOf('{{') !== -1)
}

// No required entity found, hence need to fallback to the "unknown_answers"
// When answers are simple without required entity
if (answers.length === 0) {
answers = actions[obj.classification.action]?.unknown_answers
answer = answers[Math.floor(Math.random() * answers.length)]
answer = rawAnswers[Math.floor(Math.random() * rawAnswers.length)]?.answer

// In case the expected answer requires a known entity
if (answer.indexOf('{{') !== -1) {
answers = actions[obj.classification.action]?.unknown_answers
answer = answers[Math.floor(Math.random() * answers.length)]
}
} else {
answer = answers[Math.floor(Math.random() * answers.length)]?.answer

Expand Down Expand Up @@ -370,7 +376,21 @@ class Brain {
}
}

console.log('answer', answer)
const executionTimeEnd = Date.now()
const executionTime = executionTimeEnd - executionTimeStart

if (!opts.mute) {
this.talk(answer, true)
this._socket.emit('is-typing', false)
}

resolve({
utteranceId,
lang: this._lang,
...obj,
speeches: [answer],
executionTime // In ms, skill execution time only
})
}
}
})
Expand Down
1 change: 0 additions & 1 deletion server/src/core/nlu.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ class Nlu {
processingTime - data?.executionTime // In ms, NLU processing time only
})
} catch (e) /* istanbul ignore next */ {
console.error('eee', e)
log[e.type](e.obj.message)

if (!opts.mute) {
Expand Down
19 changes: 16 additions & 3 deletions skills/leon/color/nlu/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,26 @@
"color_meaning": {
"utterance_samples": [
"Tell me something about colors",
"I want to know about colours",
"Share your knowledge about colors in general",
"Can you speak about colours?"
],
"answers": [
"Well, colors are used to differentiate and bring more aesthetic. Life is colorful!"
]
},
"specific_color_meaning": {
"utterance_samples": [
"Tell me something about the @color color",
"I want to know about the @color colour",
"How about the @color colour?"
"How about the @color colour?",
"Can you tell me more about the @color color?"
],
"answers": [
"Well, colors are used to differentiate and bring more aesthetic. Life is colorful!",
"Alright, here is for the {{ color }} color: {{ color.usage }}"
"Alright, here is for the {{ color }} color: {{ color.usage }}."
],
"unknown_answers": [
"This color looks incredible, but I haven't seen it before."
]
},
"why": {
Expand Down
3 changes: 1 addition & 2 deletions skills/leon/joke/nlu/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@
"Is your name Wi-Fi? Because I'm feeling a connection."
]
}
},
"answers": { }
}
}
3 changes: 1 addition & 2 deletions skills/leon/joke/nlu/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@
"De nos jours, le zip ça devient rar..."
]
}
},
"answers": { }
}
}
4 changes: 2 additions & 2 deletions skills/leon/partner_assistant/nlu/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"{{ partner_assistant.thought }}"
],
"unknown_answers": [
"I do not know this personal assistant",
"I have not met this personal assistant yet"
"I do not know this personal assistant.",
"I have not met this personal assistant yet."
]
}
},
Expand Down
4 changes: 4 additions & 0 deletions skills/leon/partner_assistant/nlu/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
],
"answers": [
"{{ partner_assistant.thought }}"
],
"unknown_answers": [
"Je ne connais pas cet assistant personnel.",
"Je n'ai pas encore rencontré cet assistant personnel."
]
}
},
Expand Down

0 comments on commit d5577b1

Please sign in to comment.