zloesabo/speechkit-php

Yandex SpeechKit for PHP

2.0.0 2016-04-28 20:25 UTC

This package is not auto-updated.

Last update: 2024-09-28 13:46:05 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

Yandex SpeechKit PHP 库。

安装

SpeechKit 使用 Composer,请访问 Composer 网站 获取更多信息。

在您的 composer.json 中添加 SpeechKit,然后继续操作

composer require zloesabo/speechkit-php

SpeechKit 使用 PSR-4 规范命名其类,这意味着您可以轻松地将 SpeechKit 类的加载集成到您自己的自动加载器中。

对于旧版本用户

强烈不建议使用旧版本,因为它缺乏概念和测试。然而,如果您确定要使用库的旧版本,请使用 composer require zloesabo/speechkit-php:~1.0 来要求它。

使用方法

简单

// Include dependencies installed with composer
require 'vendor/autoload.php';

use SpeechKit\Response\HypothesesList;
use SpeechKit\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;
}

高级

require 'vendor/autoload.php';

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;
}