npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rivescript-brain

v1.0.5

Published

💬 NN-Based intent detection and middleware support for RiveScript.

Downloads

12

Readme

rivescript-brain

Overview

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.

The following documentation assumes basic knowledge of RiveScript and the concept of classification.

ℹ Facing issues or have feedback? Click here.

Installation

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.

Getting Started

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);
});

The Classifier

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:

// Assuming 'bot' is already trained
await bot.classifier.save('./myFilePath/image.json');

Restoring Image

To restore trained classifier from a saved JSON image:

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.

bot.classifier.add('Hello, how do you do?', 'casual');

Classifier training can be initiated as follows:

bot.classifier.train();

Classifying

let result = bot.classifier.classify('Hello, how do you do?');

Managing Discussions

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? <set discussion=true>

+ *
% Hi, how are you?
- May I know your name please?

+ *
% May I know your name please?
- Thanks! How can I help you today? <set name=<star>> <set discussion=false>

The Reply Function

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:

await bot.reply('username','Hello');

Middleware

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:

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: <set event=myFunction>