2019-02-28 17:08:37 +03:00
|
|
|
import { NlpManager } from 'node-nlp'
|
2019-02-10 15:26:50 +03:00
|
|
|
import dotenv from 'dotenv'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
|
|
|
|
import log from '@/helpers/log'
|
|
|
|
import string from '@/helpers/string'
|
|
|
|
|
|
|
|
import { langs } from '../core/langs.json'
|
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Training script
|
|
|
|
*
|
|
|
|
* npm run train expressions
|
|
|
|
* npm run train expressions:en
|
|
|
|
*/
|
2019-02-28 17:08:37 +03:00
|
|
|
export default () => new Promise(async (resolve, reject) => {
|
2019-02-10 15:26:50 +03:00
|
|
|
const { argv } = process
|
|
|
|
const packagesDir = 'packages'
|
|
|
|
const expressionsClassifier = 'server/src/data/expressions/classifier.json'
|
|
|
|
let type = (argv[2]) ? argv[2].toLowerCase() : 'expressions'
|
|
|
|
let lang = ''
|
|
|
|
|
|
|
|
if (type.indexOf(':') !== -1) {
|
|
|
|
[type, lang] = type.split(':')
|
|
|
|
} else {
|
|
|
|
lang = langs[process.env.LEON_LANG].short.toLowerCase().substr(0, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (type === 'expressions') {
|
2019-02-28 17:08:37 +03:00
|
|
|
let manager = new NlpManager({ languages: ['en'] })
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
if (lang !== 'en') {
|
2019-02-28 17:08:37 +03:00
|
|
|
manager = new NlpManager({ languages: lang })
|
2019-02-10 15:26:50 +03:00
|
|
|
}
|
|
|
|
const packages = fs.readdirSync(packagesDir)
|
|
|
|
.filter(entity =>
|
|
|
|
fs.statSync(path.join(packagesDir, entity)).isDirectory())
|
2019-05-03 18:31:09 +03:00
|
|
|
let expressionsObj = { }
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < packages.length; i += 1) {
|
|
|
|
log.info(`Training "${string.ucfirst(packages[i])}" package modules expressions...`)
|
|
|
|
|
2019-05-03 18:31:09 +03:00
|
|
|
expressionsObj = JSON.parse(fs.readFileSync(`${packagesDir}/${packages[i]}/data/expressions/${lang}.json`, 'utf8'))
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2019-05-03 18:31:09 +03:00
|
|
|
const modules = Object.keys(expressionsObj)
|
2019-02-10 15:26:50 +03:00
|
|
|
for (let j = 0; j < modules.length; j += 1) {
|
2019-05-03 18:31:09 +03:00
|
|
|
const module = modules[j]
|
|
|
|
const actions = Object.keys(expressionsObj[module])
|
|
|
|
|
|
|
|
for (let k = 0; k < actions.length; k += 1) {
|
|
|
|
const action = actions[k]
|
|
|
|
const exprs = expressionsObj[module][action].expressions
|
|
|
|
|
|
|
|
manager.assignDomain(lang, `${module}.${action}`, packages[i])
|
|
|
|
|
|
|
|
for (let l = 0; l < exprs.length; l += 1) {
|
|
|
|
manager.addDocument(lang, exprs[l], `${module}.${action}`)
|
|
|
|
}
|
2019-02-10 15:26:50 +03:00
|
|
|
}
|
|
|
|
|
2019-05-03 18:31:09 +03:00
|
|
|
log.success(`"${string.ucfirst(module)}" module expressions trained`)
|
2019-02-10 15:26:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 17:08:37 +03:00
|
|
|
await manager.train()
|
|
|
|
|
|
|
|
fs.writeFile(expressionsClassifier, manager.export(true), (err) => {
|
2019-02-10 15:26:50 +03:00
|
|
|
if (err) {
|
|
|
|
log.error(`Failed to save the classifier: ${err}`)
|
|
|
|
reject()
|
|
|
|
} else {
|
|
|
|
log.success('Classifier saved in server/src/data/expressions/classifier.json')
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log.error(`"${type}" training type is unknown. Try "npm run train expressions"`)
|
|
|
|
reject()
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.error(e.message)
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
})
|