Skip to content

Commit

Permalink
feat: add Cartesian sample training on resolvers + enum entities
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Jul 1, 2022
1 parent eb5ade7 commit 6ed88a5
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 14 deletions.
3 changes: 2 additions & 1 deletion core/data/en/global-resolvers/affirmation_denial.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Yep",
"Yup",
"Yeah",
"Let's do it",
"Do [it|this|that]",
"For sure",
"Sure thing",
"Of course!",
Expand All @@ -30,6 +30,7 @@
"utterance_samples": [
"No",
"No no don't",
"Stop it",
"Nope",
"Naa",
"No thanks",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import fs from 'fs'
import { composeFromPattern } from '@nlpjs/utils'

import log from '@/helpers/log'

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

Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
})
})
})

Expand Down
20 changes: 20 additions & 0 deletions server/src/core/ner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions skills/social_communication/mbti/nlu/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -34,30 +35,28 @@
"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"
}
}
}
},
"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>"]
}
}
17 changes: 16 additions & 1 deletion skills/social_communication/mbti/src/actions/quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding:utf-8 -*-

import utils
from ..lib import db

def quiz(params):
"""TODO"""
Expand All @@ -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 }})
26 changes: 26 additions & 0 deletions skills/social_communication/mbti/src/lib/db.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6ed88a5

Please sign in to comment.