Giter Club home page Giter Club logo

natural-language-flows's Introduction

Natural language flows

A very simple prototype of Natural Language Flow builder and Natural Language Generator for bots, using edges of contexts, and based on intents/skills defined by the user.

Installation

npm install natural-language-flows

Or use it into the browser:

<script type="text/javascript" src="lib/natural-language-flows.js"></script>

How it works

Initialize on node.js

// Require the natural language flows
var nlf = require('natural-language-flows');

// Initialize the bot
var bot = new nlf.Bot();

Initialize on the browser

// Initialize the bot
var bot = new Bot();

Use it adding skills/intents

// Set a default non matched reply
bot.NonMatchedReply = "Sorry, I do not understand...";

// Add some bot's skills/intents with edges of contexts
bot.AddMultipleCommands([
{
    intent: 'buy-car',
    utterances: [
        "I want to {action} a {product}",
        "I should {action} a {product}",
        "Can you {action} me one {product}?"
    ],
    entities: {
        action: ['buy','have','take','give'],
        product: ['car','automobile','ferrari','sport car']
    },
    response: {
        default: 'Yes, of course! Could you tell me which model of Ferrari? (F12/GTC4/J50/California T/488 GTB)',
        context: 'car-model',
        no_need_context: true
    }
},{
    intent: 'car-model',
    utterances: [
        "{model}",
    ],
    entities: {
        model: ['F12','GTC4','J50','California T','488 GTB']
    },
    response: {
        default: 'Oh, great! Do you confirm you want to purchase it? (yes/no)',
        context: 'car-yes-no'
    }
},{
    intent: 'car-yes-no',
    utterances: [
        "{qresp}",
    ],
    entities: {
        qresp: ['yes','no']
    },
    response: {
        default: 'Sorry, could you confirm the purchase? (yes/no)',
        context: 'car-yes-no',
        conditionals: [
            {
                conditions: {qresp: 'yes'},
                resp: 'Wonderful! A great purchase!',
                context: 'buy-car'
            },
            {
                conditions: {qresp: 'no'},
                resp: "I'm sorry, do you want buy another model? (F12/GTC4/J50/California T/488 GTB)",
                context: 'car-model'
            }
        ]
    }
}]);

Start a discussion with the bot after skills' training:

// Printing the full discussion's objects
esit = bot.answer({text: 'I want to buy a ferrari', context: 'buy-car'});
console.log(esit);

esit = bot.answer({text: 'GTC4', context: esit.context});
console.log(esit);

esit = bot.answer({text: 'Maybe...', context: esit.context});
console.log(esit);

esit = bot.answer({text: 'yes', context: esit.context});
console.log(esit);

That will print the complete result object in this way:

{ text: 'I want to buy a ferrari',
  intent: true,
  entities: { action: [ 'buy' ], product: [ 'ferrari' ] },
  reply: 'Yes, of course! Could you tell me which model of Ferrari? (F12/GTC4/J50/California T/488 GTB)',
  context: 'car-model' }
{ text: 'GTC4',
  intent: true,
  entities: { model: [ 'GTC4' ] },
  reply: 'Oh, great! Do you confirm you want to purchase it? (yes/no)',
  context: 'car-yes-no' }
{ text: 'Maybe...',
  intent: null,
  entities: {},
  reply: 'Sorry, I do not understand...',
  context: 'car-yes-no' }
{ text: 'yes',
  intent: true,
  entities: { qresp: [ 'yes' ] },
  reply: 'Wonderful! A great purchase!',
  context: 'buy-car' }

To print only the discussion data:

// Only discussion's replies
esit = bot.answer({text: 'I want to buy a ferrari', context: 'buy-car'});
console.log('D:', esit.text);
console.log('R:', esit.reply);

esit = bot.answer({text: 'GTC4', context: esit.context});
console.log('D:', esit.text);
console.log('R:', esit.reply);

esit = bot.answer({text: 'Maybe...', context: esit.context});
console.log('D:', esit.text);
console.log('R:', esit.reply);

esit = bot.answer({text: 'no', context: esit.context});
console.log('D:', esit.text);
console.log('R:', esit.reply);

esit = bot.answer({text: 'California T', context: esit.context});
console.log('D:', esit.text);
console.log('R:', esit.reply);

esit = bot.answer({text: 'yes', context: esit.context});
console.log('D:', esit.text);
console.log('R:', esit.reply);

That will print:

D: I want to buy a ferrari
R: Yes, of course! Could you tell me which model of Ferrari? (F12/GTC4/J50/California T/488 GTB)
D: GTC4
R: Oh, great! Do you confirm you want to purchase it? (yes/no)
D: Maybe...
R: Sorry, I do not understand...
D: no
R: I'm sorry, do you want buy another model? (F12/GTC4/J50/California T/488 GTB)
D: California T
R: Oh, great! Do you confirm you want to purchase it? (yes/no)
D: yes
R: Wonderful! A great purchase!

natural-language-flows's People

Contributors

davidemiceli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

valenti1234

natural-language-flows's Issues

Support non-English locales

Can this tool be used to build conversations in languages other than English?
If so, what could be some possible limitations?

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.