PHP code example of zloesabo / speechkit-php

1. Go to this page and download the library: Download zloesabo/speechkit-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zloesabo / speechkit-php example snippets


// Include dependencies installed with composer
echKit\Response\Hypothesis;
use SpeechKit\Speech\SpeechContent;
use SpeechKit\SpeechKit;

$key = 'your-key-here';

$speechKit = new SpeechKit($key);

//It can be any type of stream. File, string, instance of StreamInterface, etc.
$source = fopen(__DIR__.'/some/path/to/file.mp3', 'r');

$speech = new SpeechContent($source);

//Defaults will be used: mp3, general topic, russian language
/** @var HypothesesList $result */
$result = $speechKit->recognize($speech);

/** @var Hypothesis $hyphotesis */
foreach ($result as $hyphotesis) {
    echo sprintf(
        'Confidence: %.2f Content: %s',
        $hyphotesis->getConfidence(),
        $hyphotesis->getContent()
    ), PHP_EOL;
}



use SpeechKit\Client\Curl;
use SpeechKit\Response\HypothesesList;
use SpeechKit\Response\Hypothesis;
use SpeechKit\ResponseParser\SimpleXML;
use SpeechKit\Speech\SpeechContent;
use SpeechKit\Speech\SpeechContentInterface;
use SpeechKit\SpeechKit;
use SpeechKit\Uploader\Uploader;
use SpeechKit\Uploader\UrlGenerator;

$key = 'your-key-here';

$urlGenerator = new UrlGenerator($key);

//You could use any type of client which implements ClientInterface
$client = new Curl();
$uploader = new Uploader($urlGenerator, $client);

//You could use any type of parser which implements ResponseParserInterface
$responseParser = new SimpleXML();

$speechKit = new SpeechKit($key, $uploader, $responseParser);

$source = fopen(__DIR__.'/some/path/to/file.mp3', 'r');
$speech = new SpeechContent($source);

//These settings are default, so you can skip setting them
$speech->setContentType(SpeechContentInterface::CONTENT_MP3);
$speech->setTopic(SpeechContentInterface::TOPIC_GENERAL);
$speech->setLang(SpeechContentInterface::LANG_RU);
$speech->setUuid(bin2hex(openssl_random_pseudo_bytes(16)));

/** @var HypothesesList $result */
$result = $speechKit->recognize($speech);

/** @var Hypothesis $hyphotesis */
foreach ($result as $hyphotesis) {
    echo sprintf(
        'Confidence: %.2f Content: %s',
        $hyphotesis->getConfidence(),
        $hyphotesis->getContent()
    ), PHP_EOL;
}
bash
composer 
composer