2022-09-21 18:01:16 +03:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2022-07-01 20:42:23 +03:00
|
|
|
import { composeFromPattern } from '@nlpjs/utils'
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
import { LogHelper } from '@/helpers/log-helper'
|
2022-09-21 18:01:16 +03:00
|
|
|
import { SKILL_DOMAIN } from '@/helpers/skill-domain'
|
2022-06-30 19:19:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Train skills resolvers
|
|
|
|
*/
|
2022-09-03 14:12:41 +03:00
|
|
|
export default (lang, nlp) =>
|
|
|
|
new Promise(async (resolve) => {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.title('Skills resolvers training')
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-21 18:01:16 +03:00
|
|
|
const skillDomains = await SKILL_DOMAIN.getSkillDomains()
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-13 18:06:57 +03:00
|
|
|
skillDomains.forEach((currentDomain) => {
|
2022-09-03 14:12:41 +03:00
|
|
|
const skillKeys = Object.keys(currentDomain.skills)
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
skillKeys.forEach(async (skillName) => {
|
|
|
|
const currentSkill = currentDomain.skills[skillName]
|
2022-09-21 18:01:16 +03:00
|
|
|
const configFilePath = path.join(
|
|
|
|
currentSkill.path,
|
|
|
|
'config',
|
|
|
|
`${lang}.json`
|
|
|
|
)
|
|
|
|
|
|
|
|
if (fs.existsSync(configFilePath)) {
|
|
|
|
const { resolvers } = await SKILL_DOMAIN.getSkillConfig(
|
|
|
|
configFilePath,
|
|
|
|
lang
|
|
|
|
)
|
|
|
|
|
|
|
|
if (resolvers) {
|
2022-09-03 14:12:41 +03:00
|
|
|
const resolversKeys = Object.keys(resolvers)
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
resolversKeys.forEach((resolverName) => {
|
|
|
|
const resolver = resolvers[resolverName]
|
|
|
|
const intentKeys = Object.keys(resolver.intents)
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.info(
|
2022-09-03 14:12:41 +03:00
|
|
|
`[${lang}] Training ${skillName} "${resolverName}" resolver...`
|
|
|
|
)
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
intentKeys.forEach((intentName) => {
|
|
|
|
const intent = `resolver.${currentSkill.name}.${resolverName}.${intentName}`
|
|
|
|
const intentObj = resolver.intents[intentName]
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
nlp.assignDomain(lang, intent, currentDomain.name)
|
2022-06-30 19:19:36 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
intentObj.utterance_samples.forEach((utteranceSample) => {
|
|
|
|
// Achieve Cartesian training
|
|
|
|
const utteranceAlternatives =
|
|
|
|
composeFromPattern(utteranceSample)
|
2022-07-01 20:42:23 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
utteranceAlternatives.forEach((utteranceAlternative) => {
|
|
|
|
nlp.addDocument(lang, utteranceAlternative, intent)
|
|
|
|
})
|
2022-07-01 20:42:23 +03:00
|
|
|
})
|
2022-06-30 19:19:36 +03:00
|
|
|
})
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.success(
|
2022-09-03 14:12:41 +03:00
|
|
|
`[${lang}] ${skillName} "${resolverName}" resolver trained`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2022-06-30 19:19:36 +03:00
|
|
|
}
|
2022-09-03 14:12:41 +03:00
|
|
|
})
|
2022-06-30 19:19:36 +03:00
|
|
|
})
|
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
resolve()
|
|
|
|
})
|