From f48fff1eefec624c4ea101b4681fff493fcee6c2 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 31 Jul 2017 14:58:14 -0700 Subject: [PATCH] Adds support for word time offset --- speech/cloud-client/pom.xml | 5 ++-- .../java/com/example/speech/Recognize.java | 23 ++++++++++++------- .../java/com/example/speech/RecognizeIT.java | 7 ++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/speech/cloud-client/pom.xml b/speech/cloud-client/pom.xml index 1efbd2a9372..56c698cfe7d 100644 --- a/speech/cloud-client/pom.xml +++ b/speech/cloud-client/pom.xml @@ -34,7 +34,7 @@ - - com.google.cloud google-cloud - 0.20.3-alpha + 0.21.1-alpha diff --git a/speech/cloud-client/src/main/java/com/example/speech/Recognize.java b/speech/cloud-client/src/main/java/com/example/speech/Recognize.java index d1935d4482e..15af9cd0c49 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/Recognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/Recognize.java @@ -15,10 +15,9 @@ */ package com.example.speech; - -import com.google.api.gax.grpc.ApiStreamObserver; -import com.google.api.gax.grpc.OperationFuture; -import com.google.api.gax.grpc.StreamingCallable; +import com.google.api.gax.rpc.ApiStreamObserver; +import com.google.api.gax.rpc.OperationFuture; +import com.google.api.gax.rpc.StreamingCallable; import com.google.cloud.speech.v1.LongRunningRecognizeMetadata; import com.google.cloud.speech.v1.LongRunningRecognizeResponse; import com.google.cloud.speech.v1.RecognitionAudio; @@ -32,9 +31,10 @@ import com.google.cloud.speech.v1.StreamingRecognitionResult; import com.google.cloud.speech.v1.StreamingRecognizeRequest; import com.google.cloud.speech.v1.StreamingRecognizeResponse; +import com.google.cloud.speech.v1.WordInfo; import com.google.common.util.concurrent.SettableFuture; +import com.google.longrunning.Operation; import com.google.protobuf.ByteString; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -169,8 +169,9 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep .build(); // Use non-blocking call for getting file transcription - OperationFuture response = + OperationFuture response = speech.longRunningRecognizeAsync(config, audio); + while (!response.isDone()) { System.out.println("Waiting for response..."); Thread.sleep(10000); @@ -202,13 +203,14 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio .setEncoding(AudioEncoding.FLAC) .setLanguageCode("en-US") .setSampleRateHertz(16000) + .setEnableWordTimeOffsets(true) .build(); RecognitionAudio audio = RecognitionAudio.newBuilder() .setUri(gcsUri) .build(); // Use non-blocking call for getting file transcription - OperationFuture response = + OperationFuture response = speech.longRunningRecognizeAsync(config, audio); while (!response.isDone()) { System.out.println("Waiting for response..."); @@ -220,7 +222,12 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio for (SpeechRecognitionResult result: results) { List alternatives = result.getAlternativesList(); for (SpeechRecognitionAlternative alternative: alternatives) { - System.out.printf("Transcription: %s%n", alternative.getTranscript()); + System.out.printf("Transcription: %s\n",alternative.getTranscript()); + for (WordInfo wordInfo: alternative.getWordsList()) { + System.out.println(wordInfo.getWord()); + System.out.printf("\t%s ns - %s ns\n", + wordInfo.getStartTime().getNanos(), wordInfo.getEndTime().getNanos()); + } } } speech.close(); diff --git a/speech/cloud-client/src/test/java/com/example/speech/RecognizeIT.java b/speech/cloud-client/src/test/java/com/example/speech/RecognizeIT.java index dac2f6be8ef..8f7eabcee38 100644 --- a/speech/cloud-client/src/test/java/com/example/speech/RecognizeIT.java +++ b/speech/cloud-client/src/test/java/com/example/speech/RecognizeIT.java @@ -83,6 +83,13 @@ public void testAsyncRecognizeGcs() throws Exception { assertThat(got).contains("how old is the Brooklyn Bridge"); } + @Test + public void testAsyncWordoffset() throws Exception { + Recognize.asyncRecognizeGcs(gcsPath); + String got = bout.toString(); + assertThat(got).contains("\t0.0 sec -"); + } + @Test public void testStreamRecognize() throws Exception { Recognize.streamingRecognizeFile(fileName);