{"id":26715870,"url":"https://github.com/dhruv-m1/rivescript-brain","last_synced_at":"2025-04-14T00:35:31.504Z","repository":{"id":47366232,"uuid":"202132269","full_name":"dhruv-m1/rivescript-brain","owner":"dhruv-m1","description":"💬 NN-Based intent detection and middleware support for RiveScript","archived":false,"fork":false,"pushed_at":"2023-09-09T21:40:52.000Z","size":15588,"stargazers_count":6,"open_issues_count":7,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T14:50:51.318Z","etag":null,"topics":["aiml","brainjs","chatbot","classifier","classifier-training","intent","intent-detection","javascript","neural-network","nodejs","rivescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/rivescript-brain","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dhruv-m1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-13T11:44:58.000Z","updated_at":"2023-12-27T22:41:43.000Z","dependencies_parsed_at":"2024-08-05T23:13:30.606Z","dependency_job_id":"defa42b4-63bb-4dc5-abaf-ee146bb6e760","html_url":"https://github.com/dhruv-m1/rivescript-brain","commit_stats":null,"previous_names":["dhruv-m1/rivescript-brain","dhruv-tech/rivescript-brain"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dhruv-m1%2Frivescript-brain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dhruv-m1%2Frivescript-brain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dhruv-m1%2Frivescript-brain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dhruv-m1%2Frivescript-brain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dhruv-m1","download_url":"https://codeload.github.com/dhruv-m1/rivescript-brain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248802297,"owners_count":21163819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aiml","brainjs","chatbot","classifier","classifier-training","intent","intent-detection","javascript","neural-network","nodejs","rivescript"],"created_at":"2025-03-27T14:36:38.748Z","updated_at":"2025-04-14T00:35:31.472Z","avatar_url":"https://github.com/dhruv-m1.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rivescript-brain\n\n## Overview\n`rivescript-brain` combines the power of [AIML](http://www.aiml.foundation/index.html)-based [RiveScript](https://www.rivescript.com/) with [Brain.js](https://brain.js.org/) to add neural-network based [intent](https://chatbotsmagazine.com/chatbot-vocabulary-10-chatbot-terms-you-need-to-know-3911b1ef31b4) detection ability - prevalent in modern chatbot design - to the former.\n\nThe following documentation assumes basic knowledge of [RiveScript](https://www.rivescript.com/) and the concept of [classification](https://towardsdatascience.com/machine-learning-classifiers-a5cc4e1b0623).\n\n## Installation\n\n```javascript\nnpm i rivescript-brain\n```\n\u003cb\u003e📢 Facing difficulties while installing the package?\u003c/b\u003e This is likely due to the `brain.js` dependency.\n\nIf you are on Windows, this can be resolved by installing `windows-build-tools` from npm with *administrative* privligies:\u003cbr/\u003e\n`npm install --global --production windows-build-tools`\n\nIf you are using another operating system or don't have *admin* privligies on Windows, please refer to [this guide](https://brain.js.org/#/getting-started).\n\n## Usage\n\n`rivescript-brain` offers all the same functionality as rivescript with added intent detection and middleware functionality.\n\n📚 Language(s) Supported: English.\n\n### Getting Started\n\n```javascript\nconst rs = require('rivescript-brain');\n\nconst bot = new rs({utf8:true});\n\nbot.loadDirectory(\"./dir/\").then(async() =\u003e {\n    bot.sortReplies();\n    // Classifier Setup\n    bot.classifier.add('Hello, how are you?', 'casual'); // i.e. ([sample utterance], [intent])\n    bot.classifier.train();\n    // Getting Reply\n    console.log(await bot.reply('username','Hello'));\n\n}).catch((e) =\u003e {\n    console.trace(\"Could not load Rive files.\", e);\n});\n```\n### The Classifier\n\nIn `rivescript-brain`, a feed-forward neural network is used to classify phrases.\n\n#### Expected Structure of Rive Files\nFor 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 \u003ca href=\"https://www.rivescript.com/docs/tutorial#labeled-sections\"\u003ehere\u003c/a\u003e.\u003cbr/\u003e\n\n#### Saving Image\nTo save a JSON image of a trained classifier:\n```javascript\n// Assuming 'bot' is already trained\nawait bot.classifier.save('./myFilePath/image.json');\n```\n#### Restoring Image\nTo restore trained classifier from a saved JSON image:\n```javascript\nawait bot.classifier.restore('./myFilePath/image.json');\n```\n#### Training \u0026 Retraining\nTo 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.\n```javascript\nbot.classifier.add('Hello, how do you do?', 'casual');\n```\n\nClassifier training can be initiated as follows:\n```javascript\nbot.classifier.train();\n```\n\n#### Classifying\n```javascript\nlet result = bot.classifier.classify('Hello, how do you do?');\n```\n### Managing Discussions\nTo prevent rivescript-brain's classifier from interfering in \u003ca href = \"https://www.rivescript.com/docs/tutorial#short-discussions\"\u003eshort discussions\u003c/a\u003e, it is important that there start and end points are marked as folows:\n```\n+ Hello\n- Hi, how are you? \u003cset discussion=true\u003e\n\n+ *\n% Hi, how are you?\n- May I know your name please?\n\n+ *\n% May I know your name please?\n- Thanks! How can I help you today? \u003cset name=\u003cstar\u003e\u003e \u003cset discussion=false\u003e\n```\n### The Reply Function\nrivescript-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. \u003cbr/\u003e\nSimply use:\n```javascript\nawait bot.reply('username','Hello');\n```\n\n### Middleware\nMiddleware allows for triggering a script based on the response recevied from the rivescript dialog engine. This is suitable for applications such as [entity detection](https://chatbotsmagazine.com/chatbot-vocabulary-10-chatbot-terms-you-need-to-know-3911b1ef31b4).\n\n#### Writing Middleware\nAdding a middleware function is simple:\n```javascript\nbot.middleware.myFunction = (input, output) =\u003e {\n    return new Promise((resolve) =\u003e {\n\n        //Do Something...\n\n        resolve(output);\n    })\n}\n```\nHere, `input` refers to the phrase entered by the user and `output` refers to the output generated by the RiveScript engine.\nAll middleware functions must accept `input` and `output` as arguments and return a promise that ultimalely resolves to `output`.\nThe output returned by the middleware is what the `reply(input)` function will return.\n\n#### Calling Middleware\nCalling 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:\n```\n+ tell me a fact\n- Here you go: \u003cset event=myFunction\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdhruv-m1%2Frivescript-brain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdhruv-m1%2Frivescript-brain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdhruv-m1%2Frivescript-brain/lists"}