将gcp recognizeresponse转换为json

41ik7eoe  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(441)

我正在尝试在我的spring boot应用程序中使用javasdkforjava从gcp语音到文本api得到json响应,然后将其传递到angular前端以得到显示。但问题是google doc建议回复类似于gcp doc link

{
  "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
            },......

所有这些额外的东西加上结尾的“\”是不希望有人能帮我的。
谢谢

pnwntuvh

pnwntuvh1#

您已选择启用逐字信任: setEnableWordConfidence(true) . 查看这里的javadoc。所以每个单词都有这个。
如果您删除它,或者将其设置为false,您将看不到任何这些详细信息。
这是不同的(即额外的)信心的整体成绩单-你期待看到的部分。

相关问题