PHP code example of chipslays / robot

1. Go to this page and download the library: Download chipslays/robot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chipslays / robot example snippets




$robot = new Skynet\Robot('english');

$robot->train([
    [
        'question' => 'buy coffee get some where can',
        'answer' => 'You can buy coffee in our shop: st. Lenina 420',
    ],
    [
        'question' => 'how much coffee costs price',
        'answer' => 'Coffee costs $5',
    ],
]);

$robot->ask('Where I can buy coffee?'); // You can buy coffee in our shop: st. Lenina 420

$robot->train([
    [
        // a set of keywords, sentences, (duplicate words will be removed)
        'question' => '...',

        // answer for this question
        'answer' => '...',
    ],
]);

$answer = $robot->ask('Where I can buy coffee?'); // returns answer string

// returns value from callback
$answer = $robot->ask('Where I can buy coffee?', function (array|null $item, array $result) {
    // $item - it raw value from traind data with question,
    // answer and the values you passed, or null if answer not found.

    // $result - contain sorted matches, if not found returns empty array

    if (!$item) return null;

    return $item['answer'];
});

dump($answer); // You can buy coffee in our shop: st. Lenina 420

$robot->matches(1)->ask(...);

$answer = $robot->debug(true)->ask('Where I can buy coffee?'); // returns array of all matches detail

// ^ array:2 [
//   0 => array:3 [
//     "matches" => 4
//     "words" => array:4 [
//       0 => "buy"
//       1 => "coffe"
//       2 => "where"
//       3 => "can"
//     ]
//     "answer" => "You can buy coffee in our shop: st. Lenina 420"
//   ]
//   1 => array:3 [
//     "matches" => 1
//     "words" => array:1 [
//       0 => "coffe"
//     ]
//     "answer" => "Coffee costs $5"
//   ]
// ]