@@ -127,10 +127,10 @@ public static void syncRecognizeFile(String fileName) throws Exception {
127127 List <SpeechRecognitionResult > results = response .getResultsList ();
128128
129129 for (SpeechRecognitionResult result : results ) {
130- List <SpeechRecognitionAlternative > alternatives = result .getAlternativesList ();
131- for (SpeechRecognitionAlternative alternative : alternatives ) {
130+ // There can be several alternative transcripts for a given chunk of speech. Just use the
131+ // first (most likely) one here.
132+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
132133 System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
133- }
134134 }
135135 }
136136 }
@@ -162,17 +162,17 @@ public static void syncRecognizeWords(String fileName) throws Exception {
162162 List <SpeechRecognitionResult > results = response .getResultsList ();
163163
164164 for (SpeechRecognitionResult result : results ) {
165- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
166- for ( SpeechRecognitionAlternative alternative : alternatives ) {
167- System . out . printf ( "Transcription: %s%n" , alternative . getTranscript () );
168- for ( WordInfo wordInfo : alternative .getWordsList ()) {
169- System . out . println ( wordInfo . getWord ());
170- System .out .printf ( " \t %s.%s sec - %s.%s sec \n " ,
171- wordInfo . getStartTime (). getSeconds () ,
172- wordInfo .getStartTime ().getNanos () / 100000000 ,
173- wordInfo .getEndTime ().getSeconds () ,
174- wordInfo .getEndTime ().getNanos () / 100000000 );
175- }
165+ // There can be several alternative transcripts for a given chunk of speech. Just use the
166+ // first (most likely) one here.
167+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
168+ System . out . printf ( "Transcription: %s%n" , alternative .getTranscript ());
169+ for ( WordInfo wordInfo : alternative . getWordsList ()) {
170+ System .out .println ( wordInfo . getWord ());
171+ System . out . printf ( " \t %s.%s sec - %s.%s sec \n " ,
172+ wordInfo .getStartTime ().getSeconds () ,
173+ wordInfo .getStartTime ().getNanos () / 100000000 ,
174+ wordInfo .getEndTime ().getSeconds (),
175+ wordInfo . getEndTime (). getNanos () / 100000000 );
176176 }
177177 }
178178 }
@@ -202,10 +202,10 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception {
202202 List <SpeechRecognitionResult > results = response .getResultsList ();
203203
204204 for (SpeechRecognitionResult result : results ) {
205- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
206- for ( SpeechRecognitionAlternative alternative : alternatives ) {
207- System . out . printf ( "Transcription: %s%n" , alternative . getTranscript () );
208- }
205+ // There can be several alternative transcripts for a given chunk of speech. Just use the
206+ // first (most likely) one here.
207+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
208+ System . out . printf ( "Transcription: %s%n" , alternative . getTranscript ());
209209 }
210210 }
211211 }
@@ -246,10 +246,10 @@ public static void asyncRecognizeFile(String fileName) throws Exception {
246246 List <SpeechRecognitionResult > results = response .get ().getResultsList ();
247247
248248 for (SpeechRecognitionResult result : results ) {
249- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
250- for ( SpeechRecognitionAlternative alternative : alternatives ) {
251- System . out . printf ( "Transcription: %s%n" , alternative . getTranscript () );
252- }
249+ // There can be several alternative transcripts for a given chunk of speech. Just use the
250+ // first (most likely) one here.
251+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
252+ System . out . printf ( "Transcription: %s%n" , alternative . getTranscript ());
253253 }
254254 }
255255 }
@@ -287,17 +287,17 @@ public static void asyncRecognizeWords(String gcsUri) throws Exception {
287287 List <SpeechRecognitionResult > results = response .get ().getResultsList ();
288288
289289 for (SpeechRecognitionResult result : results ) {
290- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
291- for ( SpeechRecognitionAlternative alternative : alternatives ) {
292- System . out . printf ( "Transcription: %s \n " , alternative . getTranscript () );
293- for ( WordInfo wordInfo : alternative .getWordsList ()) {
294- System . out . println ( wordInfo . getWord ());
295- System .out .printf ( " \t %s.%s sec - %s.%s sec \n " ,
296- wordInfo . getStartTime (). getSeconds () ,
297- wordInfo .getStartTime ().getNanos () / 100000000 ,
298- wordInfo .getEndTime ().getSeconds () ,
299- wordInfo .getEndTime ().getNanos () / 100000000 );
300- }
290+ // There can be several alternative transcripts for a given chunk of speech. Just use the
291+ // first (most likely) one here.
292+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
293+ System . out . printf ( "Transcription: %s \n " , alternative .getTranscript ());
294+ for ( WordInfo wordInfo : alternative . getWordsList ()) {
295+ System .out .println ( wordInfo . getWord ());
296+ System . out . printf ( " \t %s.%s sec - %s.%s sec \n " ,
297+ wordInfo .getStartTime ().getSeconds () ,
298+ wordInfo .getStartTime ().getNanos () / 100000000 ,
299+ wordInfo .getEndTime ().getSeconds (),
300+ wordInfo . getEndTime (). getNanos () / 100000000 );
301301 }
302302 }
303303 }
@@ -334,10 +334,10 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception {
334334 List <SpeechRecognitionResult > results = response .get ().getResultsList ();
335335
336336 for (SpeechRecognitionResult result : results ) {
337- List < SpeechRecognitionAlternative > alternatives = result . getAlternativesList ();
338- for ( SpeechRecognitionAlternative alternative : alternatives ) {
339- System . out . printf ( "Transcription: %s \n " , alternative . getTranscript () );
340- }
337+ // There can be several alternative transcripts for a given chunk of speech. Just use the
338+ // first (most likely) one here.
339+ SpeechRecognitionAlternative alternative = result . getAlternativesList (). get ( 0 );
340+ System . out . printf ( "Transcription: %s \n " , alternative . getTranscript ());
341341 }
342342 }
343343 }
0 commit comments