1
1
mirror of https://github.com/leon-ai/leon.git synced 2025-01-02 05:31:38 +03:00
leon/scripts/train/train-main-model/train-global-entities.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

import path from 'node:path'
import fs from 'node:fs'
import { LogHelper } from '@/helpers/log-helper'
/**
* Train global entities
* Add global entities annotations (@...)
*/
2022-09-03 14:12:41 +03:00
export default (lang, nlp) =>
2023-04-02 21:34:50 +03:00
new Promise(async (resolve) => {
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 = {}
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 }
LogHelper.success(`[${lang}] "${entityName}" global entity added`)
2022-09-03 14:12:41 +03:00
}
nlp.addEntities(newEntitiesObj, lang)
resolve()
})