【问题标题】:Can I get the full result of Google's speech to text transcription as JSON from the php library?我可以从 php 库中获取 Google 语音到文本转录为 JSON 的完整结果吗?
【发布时间】:2019-02-13 20:28:51
【问题描述】:

我正在使用 google 的 php api (https://github.com/googleapis/google-cloud-php) 进行语音到文本的转录,并且到目前为止一切正常。然而;所有使用 php 库的示例都显示了这样处理的结果:

if ($op->operationSucceeded()) {
  $response = $op->getResult();

  // each result is for a consecutive portion of the audio. iterate
  // through them to get the transcripts for the entire audio file.
  foreach ($response->getResults() as $result) {
    $alternatives = $result->getAlternatives();
    $mostLikely = $alternatives[0];
    $transcript = $mostLikely->getTranscript();
    $confidence = $mostLikely->getConfidence();
    printf('Transcript: %s' . PHP_EOL, $transcript);
    printf('Confidence: %s' . PHP_EOL, $confidence);
  }
}

我真的很想将完整的结果作为 json 格式,这样我就可以轻松地将它存储在数据库表中。有没有办法获得以 json 形式返回的完整结果?

谢谢!

【问题讨论】:

  • 火箭科学家不需要将您拥有的内容转换为 JSON
  • 点赞foreach ($response->getResults() as $result) { $json[] = $result; } $theJson = json_encode($json);
  • 当然不是,但真的,为什么要重新发明轮子,那里(可能)有一个函数调用会返回这个。此外,这些结果非常深,导致大量结构化数组转换为 json。
  • 那么它应该在手册中,如果不是他们可能没有

标签: php google-cloud-platform google-speech-api


【解决方案1】:

您可以在任何继承自Google\Protobuf\Internal\Message 的对象上调用serializeToJsonString()。确保您使用的是相对较新的 google/cloud 版本。

此外,如果您只使用 Cloud Speech,google/cloud-speech 可能会更好,因为它会安装一个小得多的包。

【讨论】:

  • 这就是我要找的,谢谢!我实际上只安装了 google/cloud-speech 和 google/cloud-storage。我应该把它联系起来。
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 2013-02-13
  • 2014-05-30
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
相关资源
最近更新 更多