2022-09-26 18:28:25 +03:00
|
|
|
import path from 'node:path'
|
|
|
|
import fs from 'node:fs'
|
2022-06-30 17:35:54 +03:00
|
|
|
|
2022-09-26 19:01:03 +03:00
|
|
|
import { composeFromPattern } from '@nlpjs/utils'
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
import { LogHelper } from '@/helpers/log-helper'
|
2022-06-30 17:35:54 +03:00
|
|
|
|
|
|
|
/**
|
2022-06-30 19:19:36 +03:00
|
|
|
* Train global resolvers
|
2022-06-30 17:35:54 +03:00
|
|
|
*/
|
2022-09-03 14:12:41 +03:00
|
|
|
export default (lang, nlp) =>
|
2023-04-02 21:34:50 +03:00
|
|
|
new Promise(async (resolve) => {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.title('Global resolvers training')
|
2022-09-03 14:12:41 +03:00
|
|
|
|
|
|
|
const resolversPath = path.join(
|
|
|
|
process.cwd(),
|
2023-03-27 16:51:40 +03:00
|
|
|
'core',
|
|
|
|
'data',
|
2022-09-03 14:12:41 +03:00
|
|
|
lang,
|
|
|
|
'global-resolvers'
|
|
|
|
)
|
2023-04-02 21:34:50 +03:00
|
|
|
const resolverFiles = await fs.promises.readdir(resolversPath)
|
2022-09-03 14:12:41 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < resolverFiles.length; i += 1) {
|
|
|
|
const resolverFileName = resolverFiles[i]
|
|
|
|
const resolverPath = path.join(resolversPath, resolverFileName)
|
|
|
|
const { name: resolverName, intents: resolverIntents } = JSON.parse(
|
2023-04-02 21:34:50 +03:00
|
|
|
await fs.promises.readFile(resolverPath, 'utf8')
|
2022-09-03 14:12:41 +03:00
|
|
|
)
|
|
|
|
const intentKeys = Object.keys(resolverIntents)
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.info(`[${lang}] Training "${resolverName}" resolver...`)
|
2022-09-03 14:12:41 +03:00
|
|
|
|
|
|
|
for (let j = 0; j < intentKeys.length; j += 1) {
|
|
|
|
const intentName = intentKeys[j]
|
|
|
|
const intent = `resolver.global.${resolverName}.${intentName}`
|
|
|
|
const intentObj = resolverIntents[intentName]
|
|
|
|
|
|
|
|
nlp.assignDomain(lang, intent, 'system')
|
|
|
|
|
|
|
|
for (let k = 0; k < intentObj.utterance_samples.length; k += 1) {
|
|
|
|
const utteranceSample = intentObj.utterance_samples[k]
|
|
|
|
// Achieve Cartesian training
|
|
|
|
const utteranceAlternatives = composeFromPattern(utteranceSample)
|
|
|
|
|
|
|
|
utteranceAlternatives.forEach((utteranceAlternative) => {
|
|
|
|
nlp.addDocument(lang, utteranceAlternative, intent)
|
|
|
|
})
|
|
|
|
}
|
2022-06-30 17:35:54 +03:00
|
|
|
}
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.success(`[${lang}] "${resolverName}" resolver trained`)
|
2022-09-03 14:12:41 +03:00
|
|
|
}
|
2022-06-30 17:35:54 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
resolve()
|
|
|
|
})
|