@@ -130,7 +130,7 @@ public static void syncRecognizeFile(String fileName) throws Exception {
130130 // There can be several alternative transcripts for a given chunk of speech. Just use the
131131 // first (most likely) one here.
132132 SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
133- System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
133+ System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
134134 }
135135 }
136136 }
@@ -178,7 +178,6 @@ public static void syncRecognizeWords(String fileName) throws Exception {
178178 }
179179 }
180180
181-
182181 /**
183182 * Performs speech recognition on remote FLAC file and prints the transcription.
184183 *
@@ -210,6 +209,7 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception {
210209 }
211210 }
212211
212+
213213 /**
214214 * Performs non-blocking speech recognition on raw PCM audio and prints
215215 * the transcription. Note that transcription is limited to 60 seconds audio.
@@ -415,11 +415,14 @@ public SettableFuture<List<T>> future() {
415415 List <StreamingRecognizeResponse > responses = responseObserver .future ().get ();
416416
417417 for (StreamingRecognizeResponse response : responses ) {
418- for (StreamingRecognitionResult result : response .getResultsList ()) {
419- for (SpeechRecognitionAlternative alternative : result .getAlternativesList ()) {
420- System .out .println (alternative .getTranscript ());
421- }
422- }
418+ // For streaming recognize, the results list has one is_final result (if available) followed
419+ // by a number of in-progress results (if iterim_results is true) for subsequent utterances.
420+ // Just print the first result here.
421+ StreamingRecognitionResult result = response .getResultsList ().get (0 );
422+ // There can be several alternative transcripts for a given chunk of speech. Just use the
423+ // first (most likely) one here.
424+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
425+ System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
423426 }
424427 speech .close ();
425428 }
@@ -448,18 +451,13 @@ public static void transcribeFileWithAutomaticPunctuation(String fileName) throw
448451 .setContent (ByteString .copyFrom (content ))
449452 .build ();
450453
451- RecognizeResponse result = speechClient .recognize (recConfig , recognitionAudio );
452- List <SpeechRecognitionResult > results = result .getResultsList ();
453-
454- if (results .isEmpty ()) {
455- System .out .println ("No results" );
456- return ;
457- }
458- List <SpeechRecognitionAlternative > alternatives = results .get (0 ).getAlternativesList ();
459- for (SpeechRecognitionAlternative alternative : alternatives ) {
460- System .out .printf ("Confidence : %f, Transcript : %s\n " , alternative .getConfidence (),
461- alternative .getTranscript ());
462- }
454+ RecognizeResponse recognizeResponse = speechClient .recognize (recConfig , recognitionAudio );
455+ // Just print the first result here.
456+ SpeechRecognitionResult result = recognizeResponse .getResultsList ().get (0 );
457+ // There can be several alternative transcripts for a given chunk of speech. Just use the
458+ // first (most likely) one here.
459+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
460+ System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
463461 }
464462 // [END transcribe_file_with_automatic_punctuation]
465463 }
@@ -492,15 +490,13 @@ public static void transcribeGcsWithAutomaticPunctuation(String gcsUri) throws E
492490 Thread .sleep (10000 );
493491 }
494492
495- List <SpeechRecognitionResult > results = response .get ().getResultsList ();
493+ // Just print the first result here.
494+ SpeechRecognitionResult result = response .get ().getResultsList ().get (0 );
496495
497- for (SpeechRecognitionResult result : results ) {
498- List <SpeechRecognitionAlternative > alternatives = result .getAlternativesList ();
499- for (SpeechRecognitionAlternative alternative : alternatives ) {
500- System .out .printf ("Confidence : %f, Transcript : %s\n " , alternative .getConfidence (),
501- alternative .getTranscript ());
502- }
503- }
496+ // There can be several alternative transcripts for a given chunk of speech. Just use the
497+ // first (most likely) one here.
498+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
499+ System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
504500 }
505501 // [START transcribe_gcs_with_automatic_punctuation]
506502 }
@@ -536,19 +532,13 @@ public static void transcribeVideoFile(String fileName) throws Exception {
536532 .build ();
537533
538534
539- RecognizeResponse result = speech .recognize (recConfig , recognitionAudio );
540- List <SpeechRecognitionResult > results = result .getResultsList ();
541-
542- if (results .isEmpty ()) {
543- System .out .println ("No results" );
544- }
545- for (SpeechRecognitionResult recognitionResult : results ) {
546- List <SpeechRecognitionAlternative > alternatives = recognitionResult .getAlternativesList ();
547- for (SpeechRecognitionAlternative alternative : alternatives ) {
548- System .out .printf ("Confidence : %f, Transcript : %s\n " , alternative .getConfidence (),
549- alternative .getTranscript ());
550- }
551- }
535+ RecognizeResponse recognizeResponse = speech .recognize (recConfig , recognitionAudio );
536+ // Just print the first result here.
537+ SpeechRecognitionResult result = recognizeResponse .getResultsList ().get (0 );
538+ // There can be several alternative transcripts for a given chunk of speech. Just use the
539+ // first (most likely) one here.
540+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
541+ System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
552542 }
553543 // [END transcribe_video_file]
554544 }
@@ -591,16 +581,12 @@ public static void transcribeGcsVideoFile(String gcsUri) throws Exception {
591581
592582 List <SpeechRecognitionResult > results = response .get ().getResultsList ();
593583
594- if (results .isEmpty ()) {
595- System .out .println ("No results" );
596- }
597- for (SpeechRecognitionResult result : results ) {
598- List <SpeechRecognitionAlternative > alternatives = result .getAlternativesList ();
599- for (SpeechRecognitionAlternative alternative : alternatives ) {
600- System .out .printf ("Confidence : %f, Transcript : %s\n " ,alternative .getConfidence (),
601- alternative .getTranscript ());
602- }
603- }
584+ // Just print the first result here.
585+ SpeechRecognitionResult result = results .get (0 );
586+ // There can be several alternative transcripts for a given chunk of speech. Just use the
587+ // first (most likely) one here.
588+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
589+ System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
604590 }
605591 // [START transcribe_video_gcs]
606592 }
0 commit comments