A very simple and indispensable helper that helps you automate the answers to simple questions in your application like a chatbot, chat on website, etc.
Supports all languages from wamania/php-stemmer.
composer require chipslays/robot
require __DIR__ . '/vendor/autoload.php';
$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
Examples can be found in examples folder.
Train robot brain.
$robot->train([
[
// a set of keywords, sentences, (duplicate words will be removed)
'question' => '...',
// answer for this question
'answer' => '...',
],
]);
Get answer for message.
$answer = $robot->ask('Where I can buy coffee?'); // returns answer string
With callback.
// 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
How many matches we need to cut off unnecessary results.
$robot->matches(1)->ask(...);
Get more details by ask()
method (matches count, words, etc).
NOTE Not working if callback passed.
$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"
// ]
// ]
MIT