chipslays / robot
1.2.5
2022-10-27 05:57 UTC
Requires
- wamania/php-stemmer: ^3.0
Requires (Dev)
- pestphp/pest: ^1.21
- symfony/var-dumper: ^6.0
README
这是一个非常简单且不可或缺的助手,可以帮助您自动化应用程序中简单问题的回答,例如聊天机器人、网站聊天等。
支持来自 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 文件夹中找到。
方法
train(array $data): self
训练机器人大脑。
$robot->train([ [ // a set of keywords, sentences, (duplicate words will be removed) 'question' => '...', // answer for this question 'answer' => '...', ], ]);
ask(string $text, callable $callback = 1): string|array|null
获取消息的答案。
$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
matches(int $count = 1): self
我们需要剪掉多少个不必要的结果。
$robot->matches(1)->ask(...);
debug(bool $enable = false): self
通过 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" // ] // ]
许可证
MIT