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 16:29:56 +03:00
|
|
|
import { LogHelper } from '@/helpers/log-helper'
|
2022-06-30 17:35:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Train global entities
|
2022-06-30 19:19:36 +03:00
|
|
|
* Add global entities annotations (@...)
|
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 entities training')
|
2022-09-03 14:12:41 +03:00
|
|
|
|
|
|
|
const globalEntitiesPath = path.join(
|
|
|
|
process.cwd(),
|
2023-03-27 16:51:40 +03:00
|
|
|
'core',
|
|
|
|
'data',
|
2022-09-03 14:12:41 +03:00
|
|
|
lang,
|
|
|
|
'global-entities'
|
|
|
|
)
|
2023-04-02 21:34:50 +03:00
|
|
|
const globalEntityFiles = await fs.promises.readdir(globalEntitiesPath)
|
2022-09-03 14:12:41 +03:00
|
|
|
const newEntitiesObj = {}
|
|
|
|
|
|
|
|
for (let i = 0; i < globalEntityFiles.length; i += 1) {
|
|
|
|
const globalEntityFileName = globalEntityFiles[i]
|
|
|
|
const [entityName] = globalEntityFileName.split('.')
|
|
|
|
const globalEntityPath = path.join(
|
|
|
|
globalEntitiesPath,
|
|
|
|
globalEntityFileName
|
|
|
|
)
|
2023-04-02 21:34:50 +03:00
|
|
|
const { options } = JSON.parse(
|
|
|
|
await fs.promises.readFile(globalEntityPath, 'utf8')
|
|
|
|
)
|
2022-09-03 14:12:41 +03:00
|
|
|
const optionKeys = Object.keys(options)
|
|
|
|
const optionsObj = {}
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.info(`[${lang}] Adding "${entityName}" global entity...`)
|
2022-09-03 14:12:41 +03:00
|
|
|
|
|
|
|
optionKeys.forEach((optionKey) => {
|
|
|
|
const { synonyms } = options[optionKey]
|
|
|
|
|
|
|
|
optionsObj[optionKey] = synonyms
|
|
|
|
})
|
|
|
|
|
|
|
|
newEntitiesObj[entityName] = { options: optionsObj }
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.success(`[${lang}] "${entityName}" global entity added`)
|
2022-09-03 14:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nlp.addEntities(newEntitiesObj, lang)
|
|
|
|
|
|
|
|
resolve()
|
|
|
|
})
|