“无效的识别'config':错误的语言代码。”什么时候不应该

时间:2019-06-07 21:41:57

标签: php json google-speech-api

尝试在php中运行语音识别会返回错误,指出它无法识别来自外部.json文件的语言代码。另一方面,Google在API上的文档中明确指出了应该这样做。

我还使用EXACT SAME配置文件通过curl运行从CLI进行的调用,它运行得很好。我不知道为什么这行不通。

.json RecognitionConfig文件如下所示:

"config": {
  //encoding is missing since I have an external process converting
  //the files to .wav format
  "sampleRateHertz":8000,
  "audioChannelCount": 1,
  "enableSeparateRecognitionPerChannel": false,
  "languageCode":"ro-RO",
  "maxAlternatives": 1,
  "profanityFilter": false,
  "speechContexts": [
    {
      "phrases": [
        "lorem",
        "ipsum",
        "dolor",
        "sit amet"
      ]
    }
  ],
  "enableWordTimeOffsets": false,
  "model": "default",
  "useEnhanced": false
  }

尽管“ ro-RO”是官方文档中可以识别的语言代码,但加载页面会返回错误提示
“无效的识别'config':错误的语言代码。”,“代码”:3,“状态”:“ INVALID_ARGUMENT”,“详细信息”:[], 是什么赋予了?我在哪里出错了,更重要的是为什么?

php代码的调用和构造如下:

$recogConfig = '/path/to/config/file.json';
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json');
//both use absolute paths

$configString = file_get_contents($recogConfig);
$configObj = json_decode($configString/*, true -this doesn't work either*/);
echo $configString; //works, returns the contents aforementioned json file

之后,与$ cofigObj进行的任何类型的交互都会产生“空调用”错误。我了解我对将JSON文件传递到php感到困惑,但是我研究过的每本指南都说应该可以,而且我不知道它的哪一部分崩溃了。除了一个简单的答案之外,我想知道如何使json文件的传递更加冗长,以便我可以了解进程崩溃的原因以及原因。

1 个答案:

答案 0 :(得分:0)

json_decode($configString)时,您将收到一个具有JSON中定义的属性的对象。您的JSON对象不过是变量持有者而已,它不包含任何功能。因此,当您在配置对象上调用->setLanguageCode()时,它显然会失败。

当我看一看Github repository's RecognitionConfig class时,我看到该类期望接收代表不同设置的属性数组。 JSON config sample还显示它对应于具有键值对的简单JSON对象:

{
  "encoding": enum(AudioEncoding),
  "sampleRateHertz": number,
  "audioChannelCount": number,
  "enableSeparateRecognitionPerChannel": boolean,
  "languageCode": string,
  ...
}

而在您的代码中,您将数组通过键“ config”和设置数组包装到一个对象中:

{ "config": {
    //encoding is missing since I have an external process converting
    //the files to .wav format
    "sampleRateHertz":8000,
  ...
  }
}

要么删除“ config”元素(我的建议是JSON将再次根据文档进行建议),或者更改您的代码以访问config元素:

$config = (new RecognitionConfig($configObj->config)); 

让我知道这是否能使您更进一步。