1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-01 11:25:01 +03:00
leon/scripts/train/train-resolvers-model/train-skills-resolvers.js

68 lines
2.0 KiB
JavaScript
Raw Normal View History

import { composeFromPattern } from '@nlpjs/utils'
import { LOG } from '@/helpers/log'
2022-09-20 22:43:34 +03:00
import { getSkillDomains, getSkillConfig } from '@/helpers/skill-domain'
/**
* Train skills resolvers
*/
2022-09-03 14:12:41 +03:00
export default (lang, nlp) =>
new Promise(async (resolve) => {
LOG.title('Skills resolvers training')
const skillDomains = await getSkillDomains()
skillDomains.forEach((currentDomain) => {
2022-09-03 14:12:41 +03:00
const skillKeys = Object.keys(currentDomain.skills)
2022-09-03 14:12:41 +03:00
skillKeys.forEach(async (skillName) => {
const currentSkill = currentDomain.skills[skillName]
2022-09-20 22:43:34 +03:00
const skillConfigData = await getSkillConfig({
domain: currentDomain.name,
skill: skillName,
lang
})
2022-09-20 22:43:34 +03:00
if (skillConfigData != null) {
const { resolvers } = skillConfigData
2022-09-20 22:43:34 +03:00
if (resolvers != null) {
2022-09-03 14:12:41 +03:00
const resolversKeys = Object.keys(resolvers)
2022-09-03 14:12:41 +03:00
resolversKeys.forEach((resolverName) => {
const resolver = resolvers[resolverName]
const intentKeys = Object.keys(resolver.intents)
LOG.info(
2022-09-03 14:12:41 +03:00
`[${lang}] Training ${skillName} "${resolverName}" resolver...`
)
2022-09-03 14:12:41 +03:00
intentKeys.forEach((intentName) => {
const intent = `resolver.${currentSkill.name}.${resolverName}.${intentName}`
const intentObj = resolver.intents[intentName]
2022-09-03 14:12:41 +03:00
nlp.assignDomain(lang, intent, currentDomain.name)
2022-09-03 14:12:41 +03:00
intentObj.utterance_samples.forEach((utteranceSample) => {
// Achieve Cartesian training
const utteranceAlternatives =
composeFromPattern(utteranceSample)
2022-09-03 14:12:41 +03:00
utteranceAlternatives.forEach((utteranceAlternative) => {
nlp.addDocument(lang, utteranceAlternative, intent)
})
})
})
LOG.success(
2022-09-03 14:12:41 +03:00
`[${lang}] ${skillName} "${resolverName}" resolver trained`
)
})
}
}
2022-09-03 14:12:41 +03:00
})
})
2022-09-03 14:12:41 +03:00
resolve()
})