From 4d877a2e3f7c8b5d14c4c96e299ab2b66911596e Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 31 Jul 2017 14:58:14 -0700 Subject: [PATCH 1/3] Adds support for word time offset --- speech/cloud-client/pom.xml | 5 ++-- .../java/com/example/speech/Recognize.java | 24 +++++++++++++------ .../java/com/example/speech/RecognizeIT.java | 7 ++++++ 3 files changed, 26 insertions(+), 10 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..4277bc4150c 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,7 +31,9 @@ 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; @@ -169,8 +170,10 @@ 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 +205,15 @@ 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 +225,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); From 383fea3465d90e9ad7de2d7e1b99236b9f0ceaf9 Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 31 Jul 2017 15:59:50 -0700 Subject: [PATCH 2/3] Removes FIXME block in pom.xml --- speech/cloud-client/pom.xml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/speech/cloud-client/pom.xml b/speech/cloud-client/pom.xml index 56c698cfe7d..1b700c077f3 100644 --- a/speech/cloud-client/pom.xml +++ b/speech/cloud-client/pom.xml @@ -33,18 +33,6 @@ UTF-8 - - From d4d0c25678c3208899b20dfc733e841ddbd7a56c Mon Sep 17 00:00:00 2001 From: Gus Class Date: Mon, 31 Jul 2017 16:26:18 -0700 Subject: [PATCH 3/3] Minimizes cloud maven dependencies and fixes lint warnings --- speech/cloud-client/pom.xml | 2 +- .../src/main/java/com/example/speech/Recognize.java | 1 + .../test/java/com/example/speech/QuickstartSampleIT.java | 6 +++--- .../src/test/java/com/example/speech/RecognizeIT.java | 6 +++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/speech/cloud-client/pom.xml b/speech/cloud-client/pom.xml index 1b700c077f3..9d097a566d1 100644 --- a/speech/cloud-client/pom.xml +++ b/speech/cloud-client/pom.xml @@ -37,7 +37,7 @@ com.google.cloud - google-cloud + google-cloud-speech 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 4277bc4150c..88fd592d051 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,6 +15,7 @@ */ package com.example.speech; + import com.google.api.gax.rpc.ApiStreamObserver; import com.google.api.gax.rpc.OperationFuture; import com.google.api.gax.rpc.StreamingCallable; diff --git a/speech/cloud-client/src/test/java/com/example/speech/QuickstartSampleIT.java b/speech/cloud-client/src/test/java/com/example/speech/QuickstartSampleIT.java index 64f6cad201d..42c7fe7cd0a 100644 --- a/speech/cloud-client/src/test/java/com/example/speech/QuickstartSampleIT.java +++ b/speech/cloud-client/src/test/java/com/example/speech/QuickstartSampleIT.java @@ -18,15 +18,15 @@ import static com.google.common.truth.Truth.assertThat; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - /** * Tests for quickstart sample. */ 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 8f7eabcee38..c2417fe7cfc 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 @@ -18,15 +18,15 @@ import static com.google.common.truth.Truth.assertThat; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - /** * Tests for speech recognize sample. */