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
import java .io .IOException ;
39
39
import java .nio .file .Files ;
40
40
import java .nio .file .Path ;
@@ -169,8 +169,9 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
169
169
.build ();
170
170
171
171
// Use non-blocking call for getting file transcription
172
- OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
172
+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata , Operation > response =
173
173
speech .longRunningRecognizeAsync (config , audio );
174
+
174
175
while (!response .isDone ()) {
175
176
System .out .println ("Waiting for response..." );
176
177
Thread .sleep (10000 );
@@ -202,13 +203,14 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
202
203
.setEncoding (AudioEncoding .FLAC )
203
204
.setLanguageCode ("en-US" )
204
205
.setSampleRateHertz (16000 )
206
+ .setEnableWordTimeOffsets (true )
205
207
.build ();
206
208
RecognitionAudio audio = RecognitionAudio .newBuilder ()
207
209
.setUri (gcsUri )
208
210
.build ();
209
211
210
212
// Use non-blocking call for getting file transcription
211
- OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
213
+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata , Operation > response =
212
214
speech .longRunningRecognizeAsync (config , audio );
213
215
while (!response .isDone ()) {
214
216
System .out .println ("Waiting for response..." );
@@ -220,7 +222,12 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
220
222
for (SpeechRecognitionResult result : results ) {
221
223
List <SpeechRecognitionAlternative > alternatives = result .getAlternativesList ();
222
224
for (SpeechRecognitionAlternative alternative : alternatives ) {
223
- System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
225
+ System .out .printf ("Transcription: %s\n " ,alternative .getTranscript ());
226
+ for (WordInfo wordInfo : alternative .getWordsList ()) {
227
+ System .out .println (wordInfo .getWord ());
228
+ System .out .printf ("\t %s ns - %s ns\n " ,
229
+ wordInfo .getStartTime ().getNanos (), wordInfo .getEndTime ().getNanos ());
230
+ }
224
231
}
225
232
}
226
233
speech .close ();
0 commit comments