@@ -127,10 +127,10 @@ public static void syncRecognizeFile(String fileName) throws Exception {
127
127
List <SpeechRecognitionResult > results = response .getResultsList ();
128
128
129
129
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 );
132
133
System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
133
- }
134
134
}
135
135
}
136
136
}
@@ -162,17 +162,17 @@ public static void syncRecognizeWords(String fileName) throws Exception {
162
162
List <SpeechRecognitionResult > results = response .getResultsList ();
163
163
164
164
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 );
176
176
}
177
177
}
178
178
}
@@ -202,10 +202,10 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception {
202
202
List <SpeechRecognitionResult > results = response .getResultsList ();
203
203
204
204
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 ());
209
209
}
210
210
}
211
211
}
@@ -246,10 +246,10 @@ public static void asyncRecognizeFile(String fileName) throws Exception {
246
246
List <SpeechRecognitionResult > results = response .get ().getResultsList ();
247
247
248
248
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 ());
253
253
}
254
254
}
255
255
}
@@ -287,17 +287,17 @@ public static void asyncRecognizeWords(String gcsUri) throws Exception {
287
287
List <SpeechRecognitionResult > results = response .get ().getResultsList ();
288
288
289
289
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 );
301
301
}
302
302
}
303
303
}
@@ -334,10 +334,10 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception {
334
334
List <SpeechRecognitionResult > results = response .get ().getResultsList ();
335
335
336
336
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 ());
341
341
}
342
342
}
343
343
}
0 commit comments