同步识别标点符号

对原始 PCM 音频数据执行自动加注标点符号的转录。

代码示例

Java

/**
 * Performs transcription with automatic punctuation on raw PCM audio data.
 *
 * @param fileName the path to a PCM audio file to transcribe.
 */
public static void transcribeFileWithAutomaticPunctuation(String fileName) throws Exception {
  Path path = Paths.get(fileName);
  byte[] content = Files.readAllBytes(path);

  try (SpeechClient speechClient = SpeechClient.create()) {
    // Configure request with local raw PCM audio
    RecognitionConfig recConfig =
        RecognitionConfig.newBuilder()
            .setEncoding(AudioEncoding.LINEAR16)
            .setLanguageCode("en-US")
            .setSampleRateHertz(16000)
            .setEnableAutomaticPunctuation(true)
            .build();

    // Get the contents of the local audio file
    RecognitionAudio recognitionAudio =
        RecognitionAudio.newBuilder().setContent(ByteString.copyFrom(content)).build();

    // Perform the transcription request
    RecognizeResponse recognizeResponse = speechClient.recognize(recConfig, recognitionAudio);

    // Just print the first result here.
    SpeechRecognitionResult result = recognizeResponse.getResultsList().get(0);

    // There can be several alternative transcripts for a given chunk of speech. Just use the
    // first (most likely) one here.
    SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);

    // Print out the result
    System.out.printf("Transcript : %s\n", alternative.getTranscript());
  }
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器