首页 > 解决方案 > 将 GCP RecognizeResponse 转换为 json

问题描述

我正在尝试在我的 Spring Boot 应用程序中使用 java sdk for java 将 JSON 响应从 GCP 语音返回到文本 api,然后将其传递到 angular 前端以显示。但问题是谷歌文档建议回复类似于 gcp 文档链接

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

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

            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();

这是文件中写的

{
  "results_": [
    {
      "alternatives_": [
        {
          "transcript_": "but what if somebody decides to break it be careful that you keep adequate coverage but look for places to save money baby it\u0027s taking longer to get things squared away then the bankers expected hiring the life for one\u0027s company may win her tax aided retirement income the booth just helpful but inadequate new self to saving Rags or hardly tossed on the two naked bones what it discussion Canyon Sue when the title of this type of song is in question there\u0027s no dying or waxing or gassing need a paperweight maybe personalized known back Quoc Leigh is leather hard place work on a flat surface and smooth out this simplest kind of separate system uses a single self-contained unit to the old shop outage still Holts a good mechanic is usually a bad boss so fingers would go higher in later years so make beautiful chairs cabinets chest doll houses it\u0027s at",
          "confidence_": 0.6881865,
          "words_": [
            {
              "startTime_": {
                "seconds_": 0,
                "nanos_": 0,
                "memoizedIsInitialized": -1,
                "unknownFields": {
                  "fields": {},
                  "fieldsDescending": {}
                },
                "memoizedSize": -1,
                "memoizedHashCode": 0
              },
              "endTime_": {
                "seconds_": 0,
                "nanos_": 200000000,
                "memoizedIsInitialized": -1,
                "unknownFields": {
                  "fields": {},
                  "fieldsDescending": {}
                },
                "memoizedSize": -1,
                "memoizedHashCode": 0
              },
              "word_": "but",
              "confidence_": 0.53119344,
              "speakerTag_": 0,
              "memoizedIsInitialized": -1,
              "unknownFields": {
                "fields": {},
                "fieldsDescending": {}
              },
              "memoizedSize": -1,
              "memoizedHashCode": 0
            },......

没想到所有这些额外的东西加上最后的“_”有人可以帮助我。

谢谢

标签: javaspring-bootgoogle-cloud-platformgoogle-speech-apigoogle-cloud-speech

解决方案


您已选择启用逐字置信度:setEnableWordConfidence(true)。有关 JavaDoc,请参见此处。所以这是为每个单词提供的。

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

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


推荐阅读