owlycode / thematic-speech
一个实验性库,可用于从自然语言中确定要执行的操作
dev-master
2015-12-15 12:51 UTC
This package is auto-updated.
Last update: 2024-09-11 13:39:42 UTC
README
这是一个实验性库,可用于从自然语言中确定要执行的操作。
安装
{
"require": {
"owlycode/thematic-speech": "dev-master"
}
}
基本用法
<?php use OwlyCode\ThematicSpeech\ThematicSpeech; require __DIR__ . '/vendor/autoload.php'; $speech = new ThematicSpeech(); $speech->registerThematics([ 'light' => ['light', 'lightbulb'], 'on' => ['on', 'power', 'start'], 'off' => ['off', 'cut', 'shutdown'], ]); $speech->register(['light', 'on'], [], function () { echo "Light turned on.\n"; }); $speech->register(['light', 'off'], [], function () { echo "Light turned off.\n"; }); $speech->process('Switch on the light please'); $speech->process('Now, switch off the light'); $speech->process('Power the lightbulb'); $speech->process('Cut the light');
这应该产生类似以下输出
Light turned on.
Light turned off.
Light turned on.
Light turned off.
使用参数
您可以通过使用参数模式从句子中收集一些动态值
<?php use OwlyCode\ThematicSpeech\Parser\ArgumentCollection; use OwlyCode\ThematicSpeech\Parser\ArgumentPattern; use OwlyCode\ThematicSpeech\ThematicSpeech; require __DIR__ . '/vendor/autoload.php'; $speech = new ThematicSpeech(); // Each integers found in sentences will be considered as identifiers. $identifierPattern = new ArgumentPattern('identifier', '/(\d+)/'); $speech->registerThematics([ 'light' => ['light', 'lightbulb'], 'on' => ['on', 'power', 'start'], 'off' => ['off', 'cut', 'shutdown'], ]); $speech->register(['light', 'on'], [$identifierPattern], function (ArgumentCollection $arguments) { if (!$arguments->hasOfType('identifier', 1)) { // Checks we've got at least one identifier. echo sprintf('You must provide at least one light identifier.'); } else { $poweredLights = $arguments->getOfType('identifier'); $text = implode(', ', array_map(function ($id) { return sprintf('"%s"', $id->getValue()); }, $poweredLights)); echo sprintf("Light(s) %s turned on.\n", $text); } }); $speech->process('Switch on the light 1, 2 and 3 please'); $speech->process('Turn on the light');
这应该产生类似以下输出
Light(s) "1", "2", "3" turned on.
You must provide at least one light identifier.