💬 NN-Based intent detection and middleware support for RiveScript.
npm install rivescript-brain
rivescript-brain combines the power of AIML-based RiveScript with Brain.js to add neural-network based intent detection ability - prevalent in modern chatbot design - to the former.
javascript
npm i rivescript-brain
`
📢 Facing difficulties while installing the package? This is likely due to the brain.js dependency.
If you are on Windows, this can be resolved by installing windows-build-tools from npm with administrative privligies:
npm install --global --production windows-build-tools
If you are using another operating system or don't have admin privligies on Windows, please refer to this guide.
Usage
rivescript-brain offers all the same functionality as rivescript with added intent detection and middleware functionality.
📚 Language(s) Supported: English.
$3
`javascript
const rs = require('rivescript-brain');
const bot = new rs({utf8:true});
bot.loadDirectory("./dir/").then(async() => {
bot.sortReplies();
// Classifier Setup
bot.classifier.add('Hello, how are you?', 'casual'); // i.e. ([sample utterance], [intent])
bot.classifier.train();
// Getting Reply
console.log(await bot.reply('username','Hello'));
}).catch((e) => {
console.trace("Could not load Rive files.", e);
});
`
$3
In rivescript-brain, a feed-forward neural network is used to classify phrases.
#### Expected Structure of Rive Files
For the classifier to work as intended, it is expected that all conversations in the RiveScript files, are sorted by topic. These topics must be the same as the intents used to train the classifier. To learn more about topics in RiveScript, please click here.
#### Saving Image
To save a JSON image of a trained classifier:
`javascript
// Assuming 'bot' is already trained
await bot.classifier.save('./myFilePath/image.json');
`
#### Restoring Image
To restore trained classifier from a saved JSON image:
`javascript
await bot.classifier.restore('./myFilePath/image.json');
`
#### Training & Retraining
To train a classifier, data must be added first. Note that when retraining, previously added data is retained and does not need to be added again.
`javascript
bot.classifier.add('Hello, how do you do?', 'casual');
`
Classifier training can be initiated as follows:
`javascript
bot.classifier.train();
`
#### Classifying
`javascript
let result = bot.classifier.classify('Hello, how do you do?');
`
$3
To prevent rivescript-brain's classifier from interfering in short discussions, it is important that there start and end points are marked as folows:
`
+ Hello
- Hi, how are you?
+ *
% Hi, how are you?
- May I know your name please?
+ *
% May I know your name please?
- Thanks! How can I help you today? >
`
$3
rivescript-brain modifies the stock rivescript reply function to incoperate classifcation and middleware functionality. There is no need to classify and set the convsersation topic seperately.
Simply use:
`javascript
await bot.reply('username','Hello');
`
$3
Middleware allows for triggering a script based on the response recevied from the rivescript dialog engine. This is suitable for applications such as entity detection.
#### Writing Middleware
Adding a middleware function is simple:
`javascript
bot.middleware.myFunction = (input, output) => {
return new Promise((resolve) => {
//Do Something...
resolve(output);
})
}
`
Here, input refers to the phrase entered by the user and output refers to the output generated by the RiveScript engine.
All middleware functions must accept input and output as arguments and return a promise that ultimalely resolves to output.
The output returned by the middleware is what the reply(input) function will return.
#### Calling Middleware
Calling middleware from a RiveScript file is very simple. Simply set the event variable to the name of the middleware function that you'd like to call:
`
+ tell me a fact
- Here you go:
``