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

feat: add Cartesian sample training on resolvers + enum entities

This commit is contained in:
louistiti 2022-07-02 01:42:23 +08:00
parent eb5ade7684
commit 6ed88a5946
No known key found for this signature in database
GPG Key ID: 0A1C3B043E70C77D
7 changed files with 88 additions and 14 deletions

View File

@ -7,7 +7,7 @@
"Yep",
"Yup",
"Yeah",
"Let's do it",
"Do [it|this|that]",
"For sure",
"Sure thing",
"Of course!",
@ -30,6 +30,7 @@
"utterance_samples": [
"No",
"No no don't",
"Stop it",
"Nope",
"Naa",
"No thanks",

View File

@ -1,5 +1,6 @@
import path from 'path'
import fs from 'fs'
import { composeFromPattern } from '@nlpjs/utils'
import log from '@/helpers/log'
@ -28,7 +29,13 @@ export default (lang, nlp) => new Promise((resolve) => {
nlp.assignDomain(lang, intent, 'system')
for (let k = 0; k < intentObj.utterance_samples.length; k += 1) {
nlp.addDocument(lang, intentObj.utterance_samples[k], intent)
const utteranceSample = intentObj.utterance_samples[k]
// Achieve Cartesian training
const utteranceAlternatives = composeFromPattern(utteranceSample)
utteranceAlternatives.forEach((utteranceAlternative) => {
nlp.addDocument(lang, utteranceAlternative, intent)
})
}
}

View File

@ -1,5 +1,6 @@
import path from 'path'
import fs from 'fs'
import { composeFromPattern } from '@nlpjs/utils'
import log from '@/helpers/log'
import domain from '@/helpers/domain'
@ -40,7 +41,12 @@ export default (lang, nlp) => new Promise(async (resolve) => {
nlp.assignDomain(lang, intent, currentDomain.name)
intentObj.utterance_samples.forEach((utteranceSample) => {
nlp.addDocument(lang, utteranceSample, intent)
// Achieve Cartesian training
const utteranceAlternatives = composeFromPattern(utteranceSample)
utteranceAlternatives.forEach((utteranceAlternative) => {
nlp.addDocument(lang, utteranceAlternative, intent)
})
})
})

View File

@ -49,6 +49,8 @@ class Ner {
promises.push(this.injectRegexEntity(lang, entity))
} else if (entity.type === 'trim') {
promises.push(this.injectTrimEntity(lang, entity))
} else if (entity.type === 'enum') {
promises.push(this.injectEnumEntity(lang, entity))
}
}
@ -140,6 +142,24 @@ class Ner {
})
}
/**
* Inject enum type entities
*/
injectEnumEntity (lang, entity) {
return new Promise((resolve) => {
const { name: entityName, options } = entity
const optionKeys = Object.keys(options)
optionKeys.forEach((optionName) => {
const { synonyms } = options[optionName]
this.ner.addRuleOptionTexts(lang, entityName, optionName, synonyms)
})
resolve()
})
}
/**
* Get Microsoft builtin entities
* https://github.com/axa-group/nlp.js/blob/master/packages/builtin-microsoft/src/builtin-microsoft.js

View File

@ -3,10 +3,11 @@
"setup": {
"type": "dialog",
"utterance_samples": [
"I want to know my MBTI personality type"
"I want to know my MBTI personality type",
"Start a personality type [quiz|questionnaire|test]"
],
"answers": [
"Alright, let's go!"
"Alright, let's go!<br><br>1/20<br>At a party do you:<ul><li>a. Interact with many, including strangers</li><li>b. Interact with a few, known to you</li></ul>"
],
"next_action": "quiz"
},
@ -34,24 +35,20 @@
"1_b": {
"utterance_samples": [
"Interact with a few",
"Know to [me|you]"
"Known to [me|you]"
],
"value": "1_b"
},
"2_a": {
"utterance_samples": [
"Realistic than speculative",
"Not a dreamer",
"Believe in science"
"Head in the clouds",
"My head in the clouds"
],
"value": "2_a"
},
"2_b": {
"utterance_samples": [
"Speculative than realistic",
"Speculative",
"[like|love] to dream",
"A dreamer"
"In a rut"
],
"value": "2_b"
}
@ -59,5 +56,7 @@
}
},
"answers": {
"2": ["%question%/20<br>Is it worse to:<ul><li>a. Have your \"head in the clouds\"</li><li>b. Be \"in a rut\"</li></ul>"],
"3": ["%question%/20<br>Is it worse to:<ul><li>a. Have your \"head in the clouds\"</li><li>b. Be \"in a rut\"</li></ul>"]
}
}

View File

@ -2,6 +2,7 @@
# -*- coding:utf-8 -*-
import utils
from ..lib import db
def quiz(params):
"""TODO"""
@ -12,4 +13,18 @@ def quiz(params):
if resolver['name'] == 'mbti_quiz':
answer = resolver['value']
print('answer', answer)
session = db.get_session()
current_question = 1
if session != None:
current_question = session['current_question']
db.upsert_session(current_question)
current_question += 1
if current_question == 20:
# TODO
return utils.output('end', 'Your personality type is...', { 'isInActionLoop': False })
return utils.output('end', { 'key': current_question, 'data': { 'question': current_question }})

View File

@ -0,0 +1,26 @@
from time import time
import utils
# Skill database
db = utils.db()['db']
table = utils.db()['table']
# Session table
session_table = db.table('session')
# Time stamp
timestamp = int(time())
def upsert_session(current_question):
"""Save current question number"""
session_table.upsert(table.Document({
'current_question': current_question
}, doc_id=0))
def get_session():
"""TODO"""
return session_table.get(doc_id=0)