├── .gitignore ├── sample └── sample.mp3 ├── demo.php ├── composer.json ├── LICENSE ├── src ├── Command │ ├── MessageCommand.php │ ├── QuickstartCommand.php │ └── SpeechCommand.php └── Action │ └── QuickAction.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /sample/sample.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgallice/wit-php-example/HEAD/sample/sample.mp3 -------------------------------------------------------------------------------- /demo.php: -------------------------------------------------------------------------------- 1 | add(new \Tgallice\WitDemo\Command\QuickstartCommand()); 7 | $application->add(new \Tgallice\WitDemo\Command\MessageCommand()); 8 | $application->add(new \Tgallice\WitDemo\Command\SpeechCommand()); 9 | $application->run(); 10 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tgallice/wit-php-exemple", 3 | "description": "Some examples of the wit-php sdk", 4 | "minimum-stability": "stable", 5 | "license": "proprietary", 6 | "authors": [ 7 | { 8 | "name": "Thomas Gallice", 9 | "email": "tm.gallice@gmail.com" 10 | } 11 | ], 12 | "autoload": { 13 | "psr-4": { 14 | "Tgallice\\WitDemo\\": "src/" 15 | } 16 | }, 17 | "require": { 18 | "symfony/console": "^3.0", 19 | "tgallice/wit-php": "1.0.x-dev" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Thomas Gallice 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Command/MessageCommand.php: -------------------------------------------------------------------------------- 1 | setName('wit:message') 19 | ->setDescription('Extract meaning of a text message') 20 | ->addArgument( 21 | 'token', 22 | InputArgument::REQUIRED, 23 | 'Access token ID' 24 | ) 25 | ; 26 | } 27 | 28 | protected function execute(InputInterface $input, OutputInterface $output) 29 | { 30 | $token = $input->getArgument('token'); 31 | $client = new Client($token); 32 | $api = new MessageApi($client); 33 | 34 | ask: 35 | 36 | $helper = $this->getHelper('question'); 37 | $messagePrompt = new Question('>>> '); 38 | $message = $helper->ask($input, $output, $messagePrompt); 39 | $intent = $api->extractMeaning($message); 40 | 41 | $output->writeln('+ Response body :'); 42 | $output->writeln(''.json_encode($intent, JSON_PRETTY_PRINT).''); 43 | 44 | goto ask; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Command/QuickstartCommand.php: -------------------------------------------------------------------------------- 1 | setName('wit:quickstart') 22 | ->setDescription('Interactive converse with the Wit.ai api') 23 | ->addArgument( 24 | 'token', 25 | InputArgument::REQUIRED, 26 | 'Access token ID' 27 | ) 28 | ; 29 | } 30 | 31 | protected function execute(InputInterface $input, OutputInterface $output) 32 | { 33 | $token = $input->getArgument('token'); 34 | $client = new Client($token); 35 | $api = new ConverseApi($client); 36 | $actionMapping = new QuickAction($output); 37 | $conversation = new Conversation($api, $actionMapping); 38 | 39 | $sessionId = 'user-'.time(); 40 | $context = new Context(); 41 | 42 | ask: 43 | 44 | $helper = $this->getHelper('question'); 45 | $messagePrompt = new Question('>>> '); 46 | $message = $helper->ask($input, $output, $messagePrompt); 47 | $context = $conversation->converse($sessionId, $message, $context); 48 | 49 | goto ask; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Command/SpeechCommand.php: -------------------------------------------------------------------------------- 1 | setName('wit:speech') 19 | ->setDescription('Extract meaning of a speech file') 20 | ->addArgument( 21 | 'token', 22 | InputArgument::REQUIRED, 23 | 'Access token ID' 24 | ) 25 | ; 26 | } 27 | 28 | protected function execute(InputInterface $input, OutputInterface $output) 29 | { 30 | $token = $input->getArgument('token'); 31 | $client = new Client($token); 32 | $api = new SpeechApi($client); 33 | 34 | ask: 35 | 36 | $helper = $this->getHelper('question'); 37 | $messagePrompt = new Question('File path >>> '); 38 | $file = $helper->ask($input, $output, $messagePrompt); 39 | 40 | if (!file_exists($file)) { 41 | $output->writeln('Invalid File'); 42 | goto ask; 43 | } 44 | 45 | if (0 !== strpos($file, '/')) { 46 | $file = __DIR__.'/../../'.$file; 47 | } 48 | 49 | $output->writeln('+ Please wait...'); 50 | 51 | $intent = $api->extractMeaning($file); 52 | 53 | $output->writeln('+ Response body :'); 54 | $output->writeln(''.json_encode($intent, JSON_PRETTY_PRINT).''); 55 | 56 | goto ask; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Action/QuickAction.php: -------------------------------------------------------------------------------- 1 | output = $output; 21 | } 22 | 23 | /** 24 | * @inheritdoc 25 | */ 26 | public function action($sessionId, Context $context, Action $step) 27 | { 28 | $this->output->writeln(sprintf('+ (%f) Action : %s', $step->getConfidence(), $step->getAction())); 29 | 30 | if (!empty($step->getEntities())) { 31 | $this->output->writeln('+ Entities provided:'); 32 | $this->output->writeln(''.json_encode($step->getEntities(), JSON_PRETTY_PRINT).''); 33 | } 34 | 35 | if ($step->getAction() === 'getForecast') { 36 | $context = $this->getForecast($context, $step->getEntities()); 37 | } 38 | 39 | return $context; 40 | } 41 | 42 | /** 43 | * @inheritdoc 44 | */ 45 | public function say($sessionId, Context $context, Message $step) 46 | { 47 | $this->output->writeln(sprintf('+ (%f) Say : %s', $step->getConfidence(), $step->getMessage())); 48 | $this->output->writeln('+++ '.$step->getMessage()); 49 | } 50 | 51 | /** 52 | * @inheritdoc 53 | */ 54 | public function error($sessionId, Context $context, $error = '', array $step = null) 55 | { 56 | $this->output->writeln(''.$error.''); 57 | } 58 | 59 | /** 60 | * @inheritdoc 61 | */ 62 | public function merge($sessionId, Context $context, Merge $step) 63 | { 64 | $this->output->writeln(sprintf('+ (%f) Merge context with :', $step->getConfidence())); 65 | $this->output->writeln(''.json_encode($step->getEntities(), JSON_PRETTY_PRINT).''); 66 | 67 | return $context; 68 | } 69 | 70 | /** 71 | * @inheritdoc 72 | */ 73 | public function stop($sessionId, Context $context, Stop $step) 74 | { 75 | $this->output->writeln(sprintf('+ (%f) Stop', $step->getConfidence())); 76 | } 77 | 78 | private function getForecast(Context $context, array $entities = []) 79 | { 80 | $loc = Helper::getFirstEntityValue('location', $entities); 81 | 82 | if (!$loc) { 83 | return new Context(['missingLocation' => true]); 84 | } 85 | 86 | return new Context(['forecast' => 'sunny in '.$loc]); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Wit.ai php demo 2 | =============== 3 | 4 | This is an interactive demo using the [tgallice/wit-php][1] sdk. 5 | 6 | - You should create a [wit.ai][2] account before run the demo. 7 | - Make sure to install dependencies with composer before 8 | running the demo. 9 | 10 | All these demos are built with the [symfony/console][3] component and should be used with a CLI. 11 | 12 | Install 13 | ------- 14 | ```bash 15 | 16 | $ git clone git@github.com:tgallice/wit-php-example.git 17 | $ cd wit-php-example 18 | $ composer install 19 | 20 | ``` 21 | 22 | Quickstart demo 23 | --------------- 24 | 25 | It's based on the quickstart tutorial of wit.ai which can be found [here][4] 26 | 27 | ```bash 28 | $ php demo.php wit:quickstart 29 | >>> What is the weather ? 30 | + Action : getForecast 31 | +++ Where exactly? 32 | >>> In London 33 | + Action : getForecast 34 | + Entities provided: 35 | { 36 | "location": [ 37 | { 38 | "confidence": 0.99930685842002, 39 | "type": "value", 40 | "value": "London", 41 | "suggested": true 42 | } 43 | ] 44 | } 45 | 46 | + Say : The weather will be sunny in London 47 | +++ The weather will be sunny in London 48 | + Stop 49 | 50 | ``` 51 | 52 | Intent by text 53 | -------------- 54 | 55 | This command provides an easy way to extract meaning based on message input. 56 | 57 | ```bash 58 | $ php demo.php wit:message 59 | >>> I live in London 60 | + Response body : 61 | { 62 | "msg_id": "e8cca629-cf2a-445a-b1c8-7bd1d569330e", 63 | "_text": "I live in London", 64 | "entities": { 65 | "location": [ 66 | { 67 | "confidence": 0.99984823481825, 68 | "type": "value", 69 | "value": "London", 70 | "suggested": true 71 | } 72 | ] 73 | } 74 | } 75 | >>> 76 | 77 | ``` 78 | 79 | Intent by speech 80 | ---------------- 81 | 82 | You can test to extract meaning by speech. A sample is provided, but you can use your own. 83 | 84 | ```bash 85 | $ php demo.php wit:speech 86 | File path >>> sample/sample.mp3 87 | + Please wait... 88 | + Response body : 89 | { 90 | "msg_id": "702657bc-5672-444e-a0cc-5a673306aa8b", 91 | "_text": "hello i live in london", 92 | "entities": { 93 | "location": [ 94 | { 95 | "confidence": 0.99984823481825, 96 | "type": "value", 97 | "value": "london", 98 | "suggested": true 99 | } 100 | ] 101 | } 102 | } 103 | File path >>> 104 | 105 | ``` 106 | 107 | [1]: https://github.com/tgallice/wit-php 108 | [2]: https://wit.ai/ 109 | [3]: http://symfony.com/doc/current/components/console/introduction.html 110 | [4]: https://wit.ai/docs/quickstart 111 | --------------------------------------------------------------------------------