-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
|
||
import fs from 'fs' | ||
|
||
import fs from 'fs'; | ||
import { NlpManager } from 'node-nlp'; | ||
import path from 'path'; | ||
import dotenv from 'dotenv'; | ||
|
||
export const manager = new NlpManager({ languages: ["en"] }); | ||
dotenv.config(); | ||
|
||
async function trainManager() { | ||
const intentFiles = fs.readdirSync('./intents'); | ||
const intentsPath = process.env.INTENTS_PATH || path.resolve(__dirname, '../Intents'); | ||
|
||
for (const file of intentFiles) { | ||
try { | ||
const filePath = `./intents/${file}`; | ||
const data = await fs.promises.readFile(filePath, 'utf8'); | ||
const jsonData = JSON.parse(data); | ||
export const manager = new NlpManager({ languages: ['en'] }); | ||
|
||
const intent = file.replace('.json', ''); | ||
|
||
for (const utterances of jsonData.utterances) { | ||
manager.addDocument('en', utterances, intent); | ||
} | ||
|
||
for (const responses of jsonData.responses) { | ||
manager.addAnswer('en', intent, responses); | ||
async function trainManager() { | ||
try { | ||
const intentFiles = await fs.promises.readdir(intentsPath); | ||
|
||
for (const file of intentFiles) { | ||
try { | ||
const filePath = path.join(intentsPath, file); | ||
const data = await fs.promises.readFile(filePath, 'utf8'); | ||
const jsonData = JSON.parse(data); | ||
|
||
const intent = file.replace('.json', ''); | ||
|
||
for (const utterance of jsonData.utterances) { | ||
manager.addDocument('en', utterance, intent); | ||
} | ||
|
||
for (const response of jsonData.responses) { | ||
manager.addAnswer('en', intent, response); | ||
} | ||
} catch (error) { | ||
console.error(`Error processing intent file ${file}:`, error); | ||
} | ||
} catch (error) { | ||
console.error(`Error processing intent file ${file}:`, error); | ||
} | ||
} | ||
|
||
await manager.train(); | ||
await manager.train(); | ||
} catch (error) { | ||
console.error('Error reading intent files:', error); | ||
} | ||
} | ||
|
||
trainManager() | ||
.then(async () => { | ||
manager.save(); | ||
await manager.save(); | ||
}) | ||
.catch((error) => console.error('Error training NLP manager:', error)); | ||
.catch(error => console.error('Error training NLP manager:', error)); | ||
|
||
module.exports = { | ||
manager, | ||
trainManager | ||
}; | ||
module.exports = { | ||
manager, | ||
trainManager, | ||
}; |