@@ -111,10 +111,10 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
111
111
List <SpeechRecognitionResult > results = response .getResultsList ();
112
112
113
113
for (SpeechRecognitionResult result : results ) {
114
- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
115
- for ( SpeechRecognitionAlternative alternative : alternatives ) {
116
- System . out . printf ( "Transcription: %s%n" , alternative . getTranscript () );
117
- }
114
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
115
+ // first (most likely) one here.
116
+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
117
+ System . out . printf ( "Transcription: %s%n" , alternative . getTranscript ());
118
118
}
119
119
speech .close ();
120
120
}
@@ -147,17 +147,17 @@ public static void syncRecognizeWords(String fileName) throws Exception, IOExcep
147
147
List <SpeechRecognitionResult > results = response .getResultsList ();
148
148
149
149
for (SpeechRecognitionResult result : results ) {
150
- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
151
- for ( SpeechRecognitionAlternative alternative : alternatives ) {
152
- System . out . printf ( "Transcription: %s%n" , alternative . getTranscript () );
153
- for ( WordInfo wordInfo : alternative .getWordsList ()) {
154
- System . out . println ( wordInfo . getWord ());
155
- System .out .printf ( " \t %s.%s sec - %s.%s sec \n " ,
156
- wordInfo . getStartTime (). getSeconds () ,
157
- wordInfo .getStartTime ().getNanos () / 100000000 ,
158
- wordInfo .getEndTime ().getSeconds () ,
159
- wordInfo .getEndTime ().getNanos () / 100000000 );
160
- }
150
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
151
+ // first (most likely) one here.
152
+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
153
+ System . out . printf ( "Transcription: %s%n" , alternative .getTranscript ());
154
+ for ( WordInfo wordInfo : alternative . getWordsList ()) {
155
+ System .out .println ( wordInfo . getWord ());
156
+ System . out . printf ( " \t %s.%s sec - %s.%s sec \n " ,
157
+ wordInfo .getStartTime ().getSeconds () ,
158
+ wordInfo .getStartTime ().getNanos () / 100000000 ,
159
+ wordInfo .getEndTime ().getSeconds (),
160
+ wordInfo . getEndTime (). getNanos () / 100000000 );
161
161
}
162
162
}
163
163
speech .close ();
@@ -188,10 +188,10 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
188
188
List <SpeechRecognitionResult > results = response .getResultsList ();
189
189
190
190
for (SpeechRecognitionResult result : results ) {
191
- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
192
- for ( SpeechRecognitionAlternative alternative : alternatives ) {
193
- System . out . printf ( "Transcription: %s%n" , alternative . getTranscript () );
194
- }
191
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
192
+ // first (most likely) one here.
193
+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
194
+ System . out . printf ( "Transcription: %s%n" , alternative . getTranscript ());
195
195
}
196
196
speech .close ();
197
197
}
@@ -234,10 +234,10 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
234
234
List <SpeechRecognitionResult > results = response .get ().getResultsList ();
235
235
236
236
for (SpeechRecognitionResult result : results ) {
237
- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
238
- for ( SpeechRecognitionAlternative alternative : alternatives ) {
239
- System . out . printf ( "Transcription: %s%n" , alternative . getTranscript () );
240
- }
237
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
238
+ // first (most likely) one here.
239
+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
240
+ System . out . printf ( "Transcription: %s%n" , alternative . getTranscript ());
241
241
}
242
242
speech .close ();
243
243
}
@@ -275,17 +275,17 @@ public static void asyncRecognizeWords(String gcsUri) throws Exception, IOExcept
275
275
List <SpeechRecognitionResult > results = response .get ().getResultsList ();
276
276
277
277
for (SpeechRecognitionResult result : results ) {
278
- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
279
- for ( SpeechRecognitionAlternative alternative : alternatives ) {
280
- System . out . printf ( "Transcription: %s \n " , alternative . getTranscript () );
281
- for ( WordInfo wordInfo : alternative .getWordsList ()) {
282
- System . out . println ( wordInfo . getWord ());
283
- System .out .printf ( " \t %s.%s sec - %s.%s sec \n " ,
284
- wordInfo . getStartTime (). getSeconds () ,
285
- wordInfo .getStartTime ().getNanos () / 100000000 ,
286
- wordInfo .getEndTime ().getSeconds () ,
287
- wordInfo .getEndTime ().getNanos () / 100000000 );
288
- }
278
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
279
+ // first (most likely) one here.
280
+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
281
+ System . out . printf ( "Transcription: %s \n " , alternative .getTranscript ());
282
+ for ( WordInfo wordInfo : alternative . getWordsList ()) {
283
+ System .out .println ( wordInfo . getWord ());
284
+ System . out . printf ( " \t %s.%s sec - %s.%s sec \n " ,
285
+ wordInfo .getStartTime ().getSeconds () ,
286
+ wordInfo .getStartTime ().getNanos () / 100000000 ,
287
+ wordInfo .getEndTime ().getSeconds (),
288
+ wordInfo . getEndTime (). getNanos () / 100000000 );
289
289
}
290
290
}
291
291
speech .close ();
@@ -323,10 +323,10 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
323
323
List <SpeechRecognitionResult > results = response .get ().getResultsList ();
324
324
325
325
for (SpeechRecognitionResult result : results ) {
326
- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
327
- for ( SpeechRecognitionAlternative alternative : alternatives ) {
328
- System . out . printf ( "Transcription: %s \n " , alternative . getTranscript () );
329
- }
326
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
327
+ // first (most likely) one here.
328
+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
329
+ System . out . printf ( "Transcription: %s \n " , alternative . getTranscript ());
330
330
}
331
331
speech .close ();
332
332
}
@@ -404,11 +404,14 @@ public SettableFuture<List<T>> future() {
404
404
List <StreamingRecognizeResponse > responses = responseObserver .future ().get ();
405
405
406
406
for (StreamingRecognizeResponse response : responses ) {
407
- for (StreamingRecognitionResult result : response .getResultsList ()) {
408
- for (SpeechRecognitionAlternative alternative : result .getAlternativesList ()) {
409
- System .out .println (alternative .getTranscript ());
410
- }
411
- }
407
+ // For streaming recognize, the results list has one is_final result (if available) followed
408
+ // by a number of in-progress results (if iterim_results is true) for subsequent utterances.
409
+ // Just print the first result here.
410
+ StreamingRecognitionResult result = response .getResultsList ().get (0 );
411
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
412
+ // first (most likely) one here.
413
+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
414
+ System .out .println (alternative .getTranscript ());
412
415
}
413
416
speech .close ();
414
417
}
0 commit comments