谷歌语音api php中的日文文本错误

时间:2017-10-02 05:37:10

标签: php google-speech-api

当我使用'languageCode' => 'en-US'英语音频文件时,Google Speech api对我来说很好。但是当'languageCode' => 'ja-JP'与日语音频文件一起使用时,它会返回像"Transcription: ã‚‚ã—ã‚‚ã—è² ã‘ホンダã—ã¦ã‚‚ã—ã‚‚ã—"

这样的破碎文本

来自Google的示例代码:

# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\Speech\SpeechClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$speech = new SpeechClient([
    'projectId' => $projectId,
    'languageCode' => 'en-US',
]);

# The name of the audio file to transcribe
$fileName = __DIR__ . '/resources/audio.raw';

# The audio file's encoding and sample rate
$options = [
    'encoding' => 'LINEAR16',
    'sampleRateHertz' => 16000,
];

# Detects speech in the audio file
$results = $speech->recognize(fopen($fileName, 'r'), $options);

foreach ($results[0]->alternatives() as $alternative) {
    echo 'Transcription: ' . $alternative['transcript'] . PHP_EOL;
}

我检查过了 Cloud Speech API Client Libraries并按照Google的示例进行操作。

1 个答案:

答案 0 :(得分:0)

Google Speech API在$results内正确地以日语返回响应。默认编码类型为UTF-8。它清楚地写在文档中。 Google\Cloud\Language\LanguageClient

echo中的问题是foreach,它打破了日文字符。在我的情况下,我实际上不需要回应而不是使用$results。所以现在它对我来说很好。

也许,如果有人想使用echo来显示结果,以下链接可能会有所帮助。

  1. PHP Japanese echo string becomes question marks
  2. How to display Japanese characters on a php page?
  3. 感谢。