将GCP RecognizeResponse转换为json

问题描述

我正在尝试在春季启动应用程序中使用java的Java sdk将GCP语音的JSON响应返回到text api,然后将其传递给有角度的前端以进行显示。但是问题是谷歌文档建议回复gcp doc link

int

但是当我调用speechclient.recognize并尝试使用GSn库将其转换为Json时,如下所示

    {
  "results": [
    {
      "alternatives": [
        {
          "transcript": "how old is the brooklyn Bridge","confidence": 0.98360395,"words": [
            {
              "startTime": "0s","endTime": "0.300s","word": "how","confidence": SOME NUMBER
            },...
          ]
        }
      ]
    }
  ]
}

这是写在文件中的内容

            Path path = Paths.get(fileName);
            byte[] data = Files.readAllBytes(path);
            ByteString audioBytes = ByteString.copyFrom(data);

            // Builds the sync recognize request
            RecognitionConfig config =
                    RecognitionConfig.newBuilder()
                            .setEncoding(AudioEncoding.LINEAR16)
                            .setLanguageCode("en-US")
                            .setEnableWordConfidence(true)
                            .setEnableWordTimeOffsets(true)
                            .build();
            RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();

            // Performs speech recognition on the audio file
            response = speechClient.recognize(config,audio);
            Gson gson = new GsonBuilder().setPrettyPrinting().create();

            logger.error(gson.toJson(response));

            file.write(gson.toJson(response));

            file.flush();
            file.close();

所有其他多余的内容加上结尾的'_'都没想到,有人可以帮我解决这个问题。

谢谢

解决方法

您已选择启用逐词置信度:setEnableWordConfidence(true)。有关JavaDoc,请参见here。因此,每个单词都提供了此功能。

如果将其删除或将其设置为false,将不会看到任何这些详细信息。

这不同于(即附加)整体成绩单(您希望看到的部分)的可信度。