2022-06-30 17:35:54 +03:00
|
|
|
import { containerBootstrap } from '@nlpjs/core-loader'
|
|
|
|
import { Nlp } from '@nlpjs/nlp'
|
|
|
|
import { LangAll } from '@nlpjs/lang-all'
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
import { LogHelper } from '@/helpers/log-helper'
|
2022-09-26 16:17:36 +03:00
|
|
|
import { LangHelper } from '@/helpers/lang-helper'
|
2022-09-26 19:01:03 +03:00
|
|
|
|
2022-06-30 17:35:54 +03:00
|
|
|
import trainGlobalResolvers from './train-resolvers-model/train-global-resolvers'
|
2022-06-30 19:19:36 +03:00
|
|
|
import trainSkillsResolvers from './train-resolvers-model/train-skills-resolvers'
|
2022-06-30 17:35:54 +03:00
|
|
|
import trainGlobalEntities from './train-main-model/train-global-entities'
|
|
|
|
import trainSkillsActions from './train-main-model/train-skills-actions'
|
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Training utterance samples script
|
|
|
|
*
|
|
|
|
* npm run train [en or fr]
|
|
|
|
*/
|
2022-09-03 14:12:41 +03:00
|
|
|
export default () =>
|
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
const globalResolversModelFileName =
|
|
|
|
'core/data/models/leon-global-resolvers-model.nlp'
|
|
|
|
const skillsResolversModelFileName =
|
|
|
|
'core/data/models/leon-skills-resolvers-model.nlp'
|
|
|
|
const mainModelFileName = 'core/data/models/leon-main-model.nlp'
|
2022-07-12 17:27:38 +03:00
|
|
|
|
|
|
|
try {
|
2022-09-03 14:12:41 +03:00
|
|
|
/**
|
|
|
|
* Global resolvers NLP model configuration
|
|
|
|
*/
|
|
|
|
const globalResolversContainer = await containerBootstrap()
|
|
|
|
|
|
|
|
globalResolversContainer.use(Nlp)
|
|
|
|
globalResolversContainer.use(LangAll)
|
|
|
|
|
|
|
|
const globalResolversNlp = globalResolversContainer.get('nlp')
|
|
|
|
const globalResolversNluManager =
|
|
|
|
globalResolversContainer.get('nlu-manager')
|
|
|
|
|
|
|
|
globalResolversNluManager.settings.log = false
|
|
|
|
globalResolversNluManager.settings.trainByDomain = false
|
|
|
|
globalResolversNlp.settings.modelFileName = globalResolversModelFileName
|
|
|
|
globalResolversNlp.settings.threshold = 0.8
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skills resolvers NLP model configuration
|
|
|
|
*/
|
|
|
|
const skillsResolversContainer = await containerBootstrap()
|
|
|
|
|
|
|
|
skillsResolversContainer.use(Nlp)
|
|
|
|
skillsResolversContainer.use(LangAll)
|
|
|
|
|
|
|
|
const skillsResolversNlp = skillsResolversContainer.get('nlp')
|
|
|
|
const skillsResolversNluManager =
|
|
|
|
skillsResolversContainer.get('nlu-manager')
|
|
|
|
|
|
|
|
skillsResolversNluManager.settings.log = false
|
|
|
|
skillsResolversNluManager.settings.trainByDomain = true
|
|
|
|
skillsResolversNlp.settings.modelFileName = skillsResolversModelFileName
|
|
|
|
skillsResolversNlp.settings.threshold = 0.8
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main NLP model configuration
|
|
|
|
*/
|
|
|
|
const mainContainer = await containerBootstrap()
|
|
|
|
|
|
|
|
mainContainer.use(Nlp)
|
|
|
|
mainContainer.use(LangAll)
|
|
|
|
|
|
|
|
const mainNlp = mainContainer.get('nlp')
|
|
|
|
const mainNluManager = mainContainer.get('nlu-manager')
|
|
|
|
// const mainSlotManager = container.get('SlotManager')
|
|
|
|
|
|
|
|
mainNluManager.settings.log = false
|
|
|
|
mainNluManager.settings.trainByDomain = true
|
|
|
|
// mainSlotManager.settings.
|
|
|
|
mainNlp.settings.forceNER = true // https://github.com/axa-group/nlp.js/blob/master/examples/17-ner-nlg/index.js
|
|
|
|
// mainNlp.settings.nlu = { useNoneFeature: true }
|
|
|
|
mainNlp.settings.calculateSentiment = true
|
|
|
|
mainNlp.settings.modelFileName = mainModelFileName
|
|
|
|
mainNlp.settings.threshold = 0.8
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Training phases
|
|
|
|
*/
|
2022-09-26 16:17:36 +03:00
|
|
|
const shortLangs = LangHelper.getShortCodes()
|
2022-09-03 14:12:41 +03:00
|
|
|
for (let h = 0; h < shortLangs.length; h += 1) {
|
|
|
|
const lang = shortLangs[h]
|
|
|
|
|
|
|
|
globalResolversNlp.addLanguage(lang)
|
|
|
|
await trainGlobalResolvers(lang, globalResolversNlp)
|
|
|
|
|
|
|
|
skillsResolversNlp.addLanguage(lang)
|
|
|
|
await trainSkillsResolvers(lang, skillsResolversNlp)
|
|
|
|
|
|
|
|
mainNlp.addLanguage(lang)
|
|
|
|
await trainGlobalEntities(lang, mainNlp)
|
|
|
|
await trainSkillsActions(lang, mainNlp)
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await globalResolversNlp.train()
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.success(
|
2022-09-03 14:12:41 +03:00
|
|
|
`Global resolvers NLP model saved in ${globalResolversModelFileName}`
|
|
|
|
)
|
|
|
|
resolve()
|
|
|
|
} catch (e) {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.error(`Failed to save global resolvers NLP model: ${e}`)
|
2022-09-03 14:12:41 +03:00
|
|
|
reject()
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await skillsResolversNlp.train()
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.success(
|
2022-09-03 14:12:41 +03:00
|
|
|
`Skills resolvers NLP model saved in ${skillsResolversModelFileName}`
|
|
|
|
)
|
|
|
|
resolve()
|
|
|
|
} catch (e) {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.error(`Failed to save skills resolvers NLP model: ${e}`)
|
2022-09-03 14:12:41 +03:00
|
|
|
reject()
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await mainNlp.train()
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.success(`Main NLP model saved in ${mainModelFileName}`)
|
2022-09-03 14:12:41 +03:00
|
|
|
resolve()
|
|
|
|
} catch (e) {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.error(`Failed to save main NLP model: ${e}`)
|
2022-09-03 14:12:41 +03:00
|
|
|
reject()
|
|
|
|
}
|
2022-06-30 17:35:54 +03:00
|
|
|
} catch (e) {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.error(e.message)
|
2022-09-03 14:12:41 +03:00
|
|
|
reject(e)
|
2022-06-30 17:35:54 +03:00
|
|
|
}
|
2022-09-03 14:12:41 +03:00
|
|
|
})
|