16
16
17
17
package com .example .speech ;
18
18
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 ;
22
22
import com .google .cloud .speech .v1 .LongRunningRecognizeMetadata ;
23
23
import com .google .cloud .speech .v1 .LongRunningRecognizeResponse ;
24
24
import com .google .cloud .speech .v1 .RecognitionAudio ;
32
32
import com .google .cloud .speech .v1 .StreamingRecognitionResult ;
33
33
import com .google .cloud .speech .v1 .StreamingRecognizeRequest ;
34
34
import com .google .cloud .speech .v1 .StreamingRecognizeResponse ;
35
+ import com .google .cloud .speech .v1 .WordInfo ;
35
36
import com .google .common .util .concurrent .SettableFuture ;
37
+ import com .google .longrunning .Operation ;
36
38
import com .google .protobuf .ByteString ;
37
39
38
40
import java .io .IOException ;
@@ -169,8 +171,10 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
169
171
.build ();
170
172
171
173
// Use non-blocking call for getting file transcription
172
- OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
174
+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata ,
175
+ Operation > response =
173
176
speech .longRunningRecognizeAsync (config , audio );
177
+
174
178
while (!response .isDone ()) {
175
179
System .out .println ("Waiting for response..." );
176
180
Thread .sleep (10000 );
@@ -202,13 +206,15 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
202
206
.setEncoding (AudioEncoding .FLAC )
203
207
.setLanguageCode ("en-US" )
204
208
.setSampleRateHertz (16000 )
209
+ .setEnableWordTimeOffsets (true )
205
210
.build ();
206
211
RecognitionAudio audio = RecognitionAudio .newBuilder ()
207
212
.setUri (gcsUri )
208
213
.build ();
209
214
210
215
// Use non-blocking call for getting file transcription
211
- OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
216
+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata ,
217
+ Operation > response =
212
218
speech .longRunningRecognizeAsync (config , audio );
213
219
while (!response .isDone ()) {
214
220
System .out .println ("Waiting for response..." );
@@ -220,7 +226,12 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
220
226
for (SpeechRecognitionResult result : results ) {
221
227
List <SpeechRecognitionAlternative > alternatives = result .getAlternativesList ();
222
228
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
+ }
224
235
}
225
236
}
226
237
speech .close ();
0 commit comments