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