Examples

The first step is to create a json file with training data. Save this file as "chatbot.json".


          
    {
      "intents": [

      {"tag": "intro",
        "patterns": ["Hello", "Hi", "Hi there", "hey", "Whats up"],
        "responses": ["Hi, how can I help you ?", "Hey, do you need any help ?", "How are you doing?", "Greetings !"]
      },

      {"tag": "age",
        "patterns": ["how old are you?", "can i know your age", "what is your age"],
        "responses": ["I am 19 years old", "My age is 19", "My birthday is Jan 5th and I was born in 2002, so I am 19 years old !"]
      }]
    
    }
  
        
      

Training & Getting Response

Now our training data is ready so we can use it to train chatbot See the below code :


    
    require_once __DIR__ . '/vendor/autoload.php';

    use Chatman\BotWithPatterns;

    //arg1 : training data json file path
    //arg2 : ignore ML for small sentences
    $bot = new BotWithPatterns("chatbot.json", true);

    //training
    $bot->train();

    //user query
    $query = "Hello Chatman";

    //getting response
    $resp = $bot->getResponse($query);

    echo $resp['resp'];

    /*
    EXPECTED OUTPUT : 
    RANDOM : "Hi, how can I help you ?", "Hey, do you need any help ?", "How are you doing?", "Greetings !"

    */

      

In the above code, first we have created an instance of the 'Chatman' class and called it’s 'train' method. The training can be longer depending on the size of the training data. Once the training gets completed, you can call the ‘getResponse’ method by giving ‘query’ as an argument. The ‘getResponse’ method will return a key, value pair array. The key ‘tag’ will contain the matched tag name and key ‘resp’ will contain the chatbot response.


Without Pattern Training Dataset

Save this file as chatbot.json

        
    {
      "responses": [
          "My name is chatman",
          "We accept paypal and credit cards as payment",
          "I speak English"
        ]
  }
    

    

Training & Getting Response


        
  require_once __DIR__ . '/vendor/autoload.php';

  use Chatman\BotWithoutPatterns;

  //arg1 : training data json file path
  $bot = new BotWithoutPatterns("chatbot.json");

  //training
  $bot->train();

  //user query
  $query = "Do you accept paypal?";

  //getting response
  $resp = $bot->getResponse($query);

  echo $resp;

  /*
  EXPECTED OUTPUT : 
      We accept paypal and credit cards as payment
  */