Skip to content

Commit 9f1c2e8

Browse files
jerjoulesv
authored andcommitted
Use only first alternative. Comments for clarity (#837)
1 parent c7e1d91 commit 9f1c2e8

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

speech/cloud-client/src/main/java/com/example/speech/QuickstartSample.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ public static void main(String... args) throws Exception {
6060
List<SpeechRecognitionResult> results = response.getResultsList();
6161

6262
for (SpeechRecognitionResult result: results) {
63-
List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
64-
for (SpeechRecognitionAlternative alternative: alternatives) {
65-
System.out.printf("Transcription: %s%n", alternative.getTranscript());
66-
}
63+
// There can be several alternative transcripts for a given chunk of speech. Just use the
64+
// first (most likely) one here.
65+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
66+
System.out.printf("Transcription: %s%n", alternative.getTranscript());
6767
}
6868
speech.close();
6969
}

speech/cloud-client/src/main/java/com/example/speech/Recognize.java

+46-43
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
111111
List<SpeechRecognitionResult> results = response.getResultsList();
112112

113113
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());
118118
}
119119
speech.close();
120120
}
@@ -147,17 +147,17 @@ public static void syncRecognizeWords(String fileName) throws Exception, IOExcep
147147
List<SpeechRecognitionResult> results = response.getResultsList();
148148

149149
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);
161161
}
162162
}
163163
speech.close();
@@ -188,10 +188,10 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
188188
List<SpeechRecognitionResult> results = response.getResultsList();
189189

190190
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());
195195
}
196196
speech.close();
197197
}
@@ -234,10 +234,10 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
234234
List<SpeechRecognitionResult> results = response.get().getResultsList();
235235

236236
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());
241241
}
242242
speech.close();
243243
}
@@ -275,17 +275,17 @@ public static void asyncRecognizeWords(String gcsUri) throws Exception, IOExcept
275275
List<SpeechRecognitionResult> results = response.get().getResultsList();
276276

277277
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);
289289
}
290290
}
291291
speech.close();
@@ -323,10 +323,10 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
323323
List<SpeechRecognitionResult> results = response.get().getResultsList();
324324

325325
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());
330330
}
331331
speech.close();
332332
}
@@ -404,11 +404,14 @@ public SettableFuture<List<T>> future() {
404404
List<StreamingRecognizeResponse> responses = responseObserver.future().get();
405405

406406
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());
412415
}
413416
speech.close();
414417
}

0 commit comments

Comments
 (0)