mirror of
https://github.com/leon-ai/leon.git
synced 2024-12-30 04:05:58 +03:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
import path from 'path'
|
|
import fs from 'fs'
|
|
|
|
import { LogHelper } from '@/helpers/log-helper'
|
|
|
|
/**
|
|
* Train global entities
|
|
* Add global entities annotations (@...)
|
|
*/
|
|
export default (lang, nlp) =>
|
|
new Promise((resolve) => {
|
|
LogHelper.title('Global entities training')
|
|
|
|
const globalEntitiesPath = path.join(
|
|
process.cwd(),
|
|
'core/data',
|
|
lang,
|
|
'global-entities'
|
|
)
|
|
const globalEntityFiles = fs.readdirSync(globalEntitiesPath)
|
|
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
|
|
)
|
|
const { options } = JSON.parse(fs.readFileSync(globalEntityPath, 'utf8'))
|
|
const optionKeys = Object.keys(options)
|
|
const optionsObj = {}
|
|
|
|
LogHelper.info(`[${lang}] Adding "${entityName}" global entity...`)
|
|
|
|
optionKeys.forEach((optionKey) => {
|
|
const { synonyms } = options[optionKey]
|
|
|
|
optionsObj[optionKey] = synonyms
|
|
})
|
|
|
|
newEntitiesObj[entityName] = { options: optionsObj }
|
|
LogHelper.success(`[${lang}] "${entityName}" global entity added`)
|
|
}
|
|
|
|
nlp.addEntities(newEntitiesObj, lang)
|
|
|
|
resolve()
|
|
})
|