Git Product home page Git Product logo

rivescript-brain's Introduction

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.

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>

rivescript-brain's People

Contributors

dependabot[bot] avatar dhruv-tech avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rivescript-brain's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.