Skip to content

Commit 4bc55e1

Browse files
authored
Merge pull request #778 from GoogleCloudPlatform/speech-wordtimeoffset
Adds support for word time offset
2 parents f9e8abf + d4d0c25 commit 4bc55e1

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

speech/cloud-client/pom.xml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,12 @@
3333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3434
</properties>
3535

36-
<!-- FIXME(lesv) - temp to fix an issue w/ GA Datastore -->
37-
<!--
38-
<dependencyManagement>
39-
<dependencies>
40-
<dependency>
41-
<groupId>io.grpc</groupId>
42-
<artifactId>grpc-core</artifactId>
43-
<version>1.2.0</version>
44-
</dependency>
45-
</dependencies>
46-
</dependencyManagement>
47-
-->
48-
4936
<dependencies>
5037
<!-- [START dependencies] -->
5138
<dependency>
5239
<groupId>com.google.cloud</groupId>
53-
<artifactId>google-cloud</artifactId>
54-
<version>0.21.0-alpha</version>
40+
<artifactId>google-cloud-speech</artifactId>
41+
<version>0.21.1-alpha</version>
5542
</dependency>
5643
<!-- [END dependencies] -->
5744

speech/cloud-client/src/main/java/com/example/speech/Recognize.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.example.speech;
1818

19-
import com.google.api.gax.grpc.ApiStreamObserver;
20-
import com.google.api.gax.grpc.OperationFuture;
21-
import com.google.api.gax.grpc.StreamingCallable;
19+
import com.google.api.gax.rpc.ApiStreamObserver;
20+
import com.google.api.gax.rpc.OperationFuture;
21+
import com.google.api.gax.rpc.StreamingCallable;
2222
import com.google.cloud.speech.v1.LongRunningRecognizeMetadata;
2323
import com.google.cloud.speech.v1.LongRunningRecognizeResponse;
2424
import com.google.cloud.speech.v1.RecognitionAudio;
@@ -32,7 +32,9 @@
3232
import com.google.cloud.speech.v1.StreamingRecognitionResult;
3333
import com.google.cloud.speech.v1.StreamingRecognizeRequest;
3434
import com.google.cloud.speech.v1.StreamingRecognizeResponse;
35+
import com.google.cloud.speech.v1.WordInfo;
3536
import com.google.common.util.concurrent.SettableFuture;
37+
import com.google.longrunning.Operation;
3638
import com.google.protobuf.ByteString;
3739

3840
import java.io.IOException;
@@ -169,8 +171,10 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
169171
.build();
170172

171173
// Use non-blocking call for getting file transcription
172-
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
174+
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata,
175+
Operation> response =
173176
speech.longRunningRecognizeAsync(config, audio);
177+
174178
while (!response.isDone()) {
175179
System.out.println("Waiting for response...");
176180
Thread.sleep(10000);
@@ -202,13 +206,15 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
202206
.setEncoding(AudioEncoding.FLAC)
203207
.setLanguageCode("en-US")
204208
.setSampleRateHertz(16000)
209+
.setEnableWordTimeOffsets(true)
205210
.build();
206211
RecognitionAudio audio = RecognitionAudio.newBuilder()
207212
.setUri(gcsUri)
208213
.build();
209214

210215
// Use non-blocking call for getting file transcription
211-
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
216+
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata,
217+
Operation> response =
212218
speech.longRunningRecognizeAsync(config, audio);
213219
while (!response.isDone()) {
214220
System.out.println("Waiting for response...");
@@ -220,7 +226,12 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
220226
for (SpeechRecognitionResult result: results) {
221227
List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
222228
for (SpeechRecognitionAlternative alternative: alternatives) {
223-
System.out.printf("Transcription: %s%n", alternative.getTranscript());
229+
System.out.printf("Transcription: %s\n",alternative.getTranscript());
230+
for (WordInfo wordInfo: alternative.getWordsList()) {
231+
System.out.println(wordInfo.getWord());
232+
System.out.printf("\t%s ns - %s ns\n",
233+
wordInfo.getStartTime().getNanos(), wordInfo.getEndTime().getNanos());
234+
}
224235
}
225236
}
226237
speech.close();

speech/cloud-client/src/test/java/com/example/speech/QuickstartSampleIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
2124
import org.junit.After;
2225
import org.junit.Before;
2326
import org.junit.Test;
2427
import org.junit.runner.RunWith;
2528
import org.junit.runners.JUnit4;
2629

27-
import java.io.ByteArrayOutputStream;
28-
import java.io.PrintStream;
29-
3030
/**
3131
* Tests for quickstart sample.
3232
*/

speech/cloud-client/src/test/java/com/example/speech/RecognizeIT.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
2124
import org.junit.After;
2225
import org.junit.Before;
2326
import org.junit.Test;
2427
import org.junit.runner.RunWith;
2528
import org.junit.runners.JUnit4;
2629

27-
import java.io.ByteArrayOutputStream;
28-
import java.io.PrintStream;
29-
3030
/**
3131
* Tests for speech recognize sample.
3232
*/
@@ -83,6 +83,13 @@ public void testAsyncRecognizeGcs() throws Exception {
8383
assertThat(got).contains("how old is the Brooklyn Bridge");
8484
}
8585

86+
@Test
87+
public void testAsyncWordoffset() throws Exception {
88+
Recognize.asyncRecognizeGcs(gcsPath);
89+
String got = bout.toString();
90+
assertThat(got).contains("\t0.0 sec -");
91+
}
92+
8693
@Test
8794
public void testStreamRecognize() throws Exception {
8895
Recognize.streamingRecognizeFile(fileName);

0 commit comments

Comments
 (0)