Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/owlycode/thematic-speech

An experimental library that can be used to determine actions to issue from natural language
https://github.com/owlycode/thematic-speech

Last synced: about 1 month ago
JSON representation

An experimental library that can be used to determine actions to issue from natural language

Awesome Lists containing this project

README

        

# Thematic Speech

This is an experimental library that can be used to determine actions to issue from natural language.

## Installation

```
{
"require": {
"owlycode/thematic-speech": "dev-master"
}
}
```

## Basic usage

```php
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');
```

This should produce an output like this :

```
Light turned on.
Light turned off.
Light turned on.
Light turned off.
```

### Using arguments

You can gather some dynamic values from sentences by using argument patterns :

```php
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');
```

This should produce an output like this :

```
Light(s) "1", "2", "3" turned on.
You must provide at least one light identifier.
```