From 1e4cf44b91c73f05c73f26030f5ffe2ab4cb6cb8 Mon Sep 17 00:00:00 2001 From: Daryush Laqab Date: Tue, 1 Aug 2017 14:38:40 -0700 Subject: [PATCH 1/3] Added setEnableWordTimeOffsets(true) to Async Recognize for a File Added setEnableWordTimeOffsets(false) to Sync samples and quick start --- .../main/java/com/example/speech/QuickstartSample.java | 1 + .../src/main/java/com/example/speech/Recognize.java | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/speech/cloud-client/src/main/java/com/example/speech/QuickstartSample.java b/speech/cloud-client/src/main/java/com/example/speech/QuickstartSample.java index fbb0053f17b..5115f75ff2a 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/QuickstartSample.java +++ b/speech/cloud-client/src/main/java/com/example/speech/QuickstartSample.java @@ -50,6 +50,7 @@ public static void main(String... args) throws Exception { .setEncoding(AudioEncoding.LINEAR16) .setSampleRateHertz(16000) .setLanguageCode("en-US") + .setEnableWordTimeOffsets(false) .build(); RecognitionAudio audio = RecognitionAudio.newBuilder() .setContent(audioBytes) 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 88fd592d051..b7b84365fd6 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 @@ -95,6 +95,7 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept .setEncoding(AudioEncoding.LINEAR16) .setLanguageCode("en-US") .setSampleRateHertz(16000) + .setEnableWordTimeOffsets(false) .build(); RecognitionAudio audio = RecognitionAudio.newBuilder() .setContent(audioBytes) @@ -127,6 +128,7 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException .setEncoding(AudioEncoding.FLAC) .setLanguageCode("en-US") .setSampleRateHertz(16000) + .setEnableWordTimeOffsets(false) .build(); RecognitionAudio audio = RecognitionAudio.newBuilder() .setUri(gcsUri) @@ -165,6 +167,7 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep .setEncoding(AudioEncoding.LINEAR16) .setLanguageCode("en-US") .setSampleRateHertz(16000) + .setEnableWordTimeOffsets(true) .build(); RecognitionAudio audio = RecognitionAudio.newBuilder() .setContent(audioBytes) @@ -186,6 +189,11 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep List alternatives = result.getAlternativesList(); for (SpeechRecognitionAlternative alternative: alternatives) { 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(); From a893740573098c14a1af90deacf23f76e420f110 Mon Sep 17 00:00:00 2001 From: Daryush Laqab Date: Wed, 2 Aug 2017 09:53:50 -0700 Subject: [PATCH 2/3] Added missing test for AsyncRecognizeFile Fixed the tests for WordTimeOffsets --- .../src/main/java/com/example/speech/Recognize.java | 2 +- .../src/test/java/com/example/speech/RecognizeIT.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) 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 b7b84365fd6..e9b7d75c29b 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 @@ -188,7 +188,7 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep 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", 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 c2417fe7cfc..94f525b2a5a 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 @@ -84,10 +84,17 @@ public void testAsyncRecognizeGcs() throws Exception { } @Test - public void testAsyncWordoffset() throws Exception { + public void testAsyncWordoffsetFile() throws Exception { + Recognize.asyncRecognizeFile(fileName); + String got = bout.toString(); + assertThat(got).contains("\t0 ns"); + } + + @Test + public void testAsyncWordoffsetGcs() throws Exception { Recognize.asyncRecognizeGcs(gcsPath); String got = bout.toString(); - assertThat(got).contains("\t0.0 sec -"); + assertThat(got).contains("\t0 ns"); } @Test From e4a7cce33bbf5c82178d2c77fce61ac287ec342a Mon Sep 17 00:00:00 2001 From: Daryush Laqab Date: Wed, 2 Aug 2017 11:48:34 -0700 Subject: [PATCH 3/3] Changed Nanoseconds to Seconds, and fixed the tests --- .../src/main/java/com/example/speech/Recognize.java | 7 +++++-- .../src/test/java/com/example/speech/RecognizeIT.java | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) 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 2ec27a0acca..b3d156a2295 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 @@ -191,8 +191,11 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep 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()); + System.out.printf("\t%s.%s sec - %s.%s sec\n", + wordInfo.getStartTime().getSeconds(), + wordInfo.getStartTime().getNanos() / 100000000, + wordInfo.getEndTime().getSeconds(), + wordInfo.getEndTime().getNanos() / 100000000); } } } 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 94f525b2a5a..e4544bef84c 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 @@ -87,14 +87,14 @@ public void testAsyncRecognizeGcs() throws Exception { public void testAsyncWordoffsetFile() throws Exception { Recognize.asyncRecognizeFile(fileName); String got = bout.toString(); - assertThat(got).contains("\t0 ns"); + assertThat(got).contains("\t0.0 sec -"); } @Test public void testAsyncWordoffsetGcs() throws Exception { Recognize.asyncRecognizeGcs(gcsPath); String got = bout.toString(); - assertThat(got).contains("\t0 ns"); + assertThat(got).contains("\t0.0 sec -"); } @Test