@@ -130,7 +130,7 @@ public static void syncRecognizeFile(String fileName) throws Exception {
130
130
// There can be several alternative transcripts for a given chunk of speech. Just use the
131
131
// first (most likely) one here.
132
132
SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
133
- System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
133
+ System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
134
134
}
135
135
}
136
136
}
@@ -178,7 +178,6 @@ public static void syncRecognizeWords(String fileName) throws Exception {
178
178
}
179
179
}
180
180
181
-
182
181
/**
183
182
* Performs speech recognition on remote FLAC file and prints the transcription.
184
183
*
@@ -210,6 +209,7 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception {
210
209
}
211
210
}
212
211
212
+
213
213
/**
214
214
* Performs non-blocking speech recognition on raw PCM audio and prints
215
215
* the transcription. Note that transcription is limited to 60 seconds audio.
@@ -415,11 +415,14 @@ public SettableFuture<List<T>> future() {
415
415
List <StreamingRecognizeResponse > responses = responseObserver .future ().get ();
416
416
417
417
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 ());
423
426
}
424
427
speech .close ();
425
428
}
@@ -448,18 +451,13 @@ public static void transcribeFileWithAutomaticPunctuation(String fileName) throw
448
451
.setContent (ByteString .copyFrom (content ))
449
452
.build ();
450
453
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 ());
463
461
}
464
462
// [END transcribe_file_with_automatic_punctuation]
465
463
}
@@ -492,15 +490,13 @@ public static void transcribeGcsWithAutomaticPunctuation(String gcsUri) throws E
492
490
Thread .sleep (10000 );
493
491
}
494
492
495
- List <SpeechRecognitionResult > results = response .get ().getResultsList ();
493
+ // Just print the first result here.
494
+ SpeechRecognitionResult result = response .get ().getResultsList ().get (0 );
496
495
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 ());
504
500
}
505
501
// [START transcribe_gcs_with_automatic_punctuation]
506
502
}
@@ -536,19 +532,13 @@ public static void transcribeVideoFile(String fileName) throws Exception {
536
532
.build ();
537
533
538
534
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 ());
552
542
}
553
543
// [END transcribe_video_file]
554
544
}
@@ -591,16 +581,12 @@ public static void transcribeGcsVideoFile(String gcsUri) throws Exception {
591
581
592
582
List <SpeechRecognitionResult > results = response .get ().getResultsList ();
593
583
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 ());
604
590
}
605
591
// [START transcribe_video_gcs]
606
592
}
0 commit comments