Skip to content

Commit 8384bee

Browse files
jerjoujmdobry
authored andcommitted
Speech: Use all results, instead of just the first. (#466)
1 parent bc1edcb commit 8384bee

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

speech/recognize.js

+51-38
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ function syncRecognize (filename, encoding, sampleRateHertz, languageCode) {
6060

6161
// Detects speech in the audio file
6262
speech.recognize(request)
63-
.then((results) => {
64-
const transcription = results[0].results[0].alternatives[0].transcript;
63+
.then((data) => {
64+
const response = data[0];
65+
const transcription = response.results.map(result =>
66+
result.alternatives[0].transcript).join('\n');
6567
console.log(`Transcription: `, transcription);
6668
})
6769
.catch((err) => {
@@ -106,8 +108,10 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
106108

107109
// Detects speech in the audio file
108110
speech.recognize(request)
109-
.then((results) => {
110-
const transcription = results[0].results[0].alternatives[0].transcript;
111+
.then((data) => {
112+
const response = data[0];
113+
const transcription = response.results.map(result =>
114+
result.alternatives[0].transcript).join('\n');
111115
console.log(`Transcription: `, transcription);
112116
})
113117
.catch((err) => {
@@ -154,18 +158,20 @@ function syncRecognizeWords (filename, encoding, sampleRateHertz, languageCode)
154158

155159
// Detects speech in the audio file
156160
speech.recognize(request)
157-
.then((results) => {
158-
const transcription = results[0].results[0].alternatives[0].transcript;
159-
console.log(`Transcription: `, transcription);
160-
results[0].results[0].alternatives[0].words.forEach((wordInfo) => {
161-
// NOTE: If you have a time offset exceeding 2^32 seconds, use the
162-
// wordInfo.{x}Time.seconds.high to calculate seconds.
163-
const startSecs = `${wordInfo.startTime.seconds}` + `.` +
164-
(wordInfo.startTime.nanos / 100000000);
165-
const endSecs = `${wordInfo.endTime.seconds}` + `.` +
166-
(wordInfo.endTime.nanos / 100000000);
167-
console.log(`Word: ${wordInfo.word}`);
168-
console.log(`\t ${startSecs} secs - ${endSecs} secs`);
161+
.then((data) => {
162+
const response = data[0];
163+
response.results.forEach((result) => {
164+
console.log(`Transcription: `, result.alternatives[0].transcript);
165+
result.alternatives[0].words.forEach((wordInfo) => {
166+
// NOTE: If you have a time offset exceeding 2^32 seconds, use the
167+
// wordInfo.{x}Time.seconds.high to calculate seconds.
168+
const startSecs = `${wordInfo.startTime.seconds}` + `.` +
169+
(wordInfo.startTime.nanos / 100000000);
170+
const endSecs = `${wordInfo.endTime.seconds}` + `.` +
171+
(wordInfo.endTime.nanos / 100000000);
172+
console.log(`Word: ${wordInfo.word}`);
173+
console.log(`\t ${startSecs} secs - ${endSecs} secs`);
174+
});
169175
});
170176
})
171177
.catch((err) => {
@@ -212,13 +218,16 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
212218
// Detects speech in the audio file. This creates a recognition job that you
213219
// can wait for now, or get its result later.
214220
speech.longRunningRecognize(request)
215-
.then((results) => {
216-
const operation = results[0];
221+
.then((data) => {
222+
const response = data[0];
223+
const operation = response;
217224
// Get a Promise representation of the final result of the job
218225
return operation.promise();
219226
})
220-
.then((results) => {
221-
const transcription = results[0].results[0].alternatives[0].transcript;
227+
.then((data) => {
228+
const response = data[0];
229+
const transcription = response.results.map(result =>
230+
result.alternatives[0].transcript).join('\n');
222231
console.log(`Transcription: ${transcription}`);
223232
})
224233
.catch((err) => {
@@ -265,13 +274,15 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
265274
// Detects speech in the audio file. This creates a recognition job that you
266275
// can wait for now, or get its result later.
267276
speech.longRunningRecognize(request)
268-
.then((results) => {
269-
const operation = results[0];
277+
.then((data) => {
278+
const operation = data[0];
270279
// Get a Promise representation of the final result of the job
271280
return operation.promise();
272281
})
273-
.then((results) => {
274-
const transcription = results[0].results[0].alternatives[0].transcript;
282+
.then((data) => {
283+
const response = data[0];
284+
const transcription = response.results.map(result =>
285+
result.alternatives[0].transcript).join('\n');
275286
console.log(`Transcription: ${transcription}`);
276287
})
277288
.catch((err) => {
@@ -319,23 +330,25 @@ function asyncRecognizeGCSWords (gcsUri, encoding, sampleRateHertz, languageCode
319330
// Detects speech in the audio file. This creates a recognition job that you
320331
// can wait for now, or get its result later.
321332
speech.longRunningRecognize(request)
322-
.then((results) => {
323-
const operation = results[0];
333+
.then((data) => {
334+
const operation = data[0];
324335
// Get a Promise representation of the final result of the job
325336
return operation.promise();
326337
})
327-
.then((results) => {
328-
const transcription = results[0].results[0].alternatives[0].transcript;
329-
console.log(`Transcription: ${transcription}`);
330-
results[0].results[0].alternatives[0].words.forEach((wordInfo) => {
331-
// NOTE: If you have a time offset exceeding 2^32 seconds, use the
332-
// wordInfo.{x}Time.seconds.high to calculate seconds.
333-
const startSecs = `${wordInfo.startTime.seconds}` + `.` +
334-
(wordInfo.startTime.nanos / 100000000);
335-
const endSecs = `${wordInfo.endTime.seconds}` + `.` +
336-
(wordInfo.endTime.nanos / 100000000);
337-
console.log(`Word: ${wordInfo.word}`);
338-
console.log(`\t ${startSecs} secs - ${endSecs} secs`);
338+
.then((data) => {
339+
const response = data[0];
340+
response.results.forEach((result) => {
341+
console.log(`Transcription: ${result.alternatives[0].transcript}`);
342+
result.alternatives[0].words.forEach((wordInfo) => {
343+
// NOTE: If you have a time offset exceeding 2^32 seconds, use the
344+
// wordInfo.{x}Time.seconds.high to calculate seconds.
345+
const startSecs = `${wordInfo.startTime.seconds}` + `.` +
346+
(wordInfo.startTime.nanos / 100000000);
347+
const endSecs = `${wordInfo.endTime.seconds}` + `.` +
348+
(wordInfo.endTime.nanos / 100000000);
349+
console.log(`Word: ${wordInfo.word}`);
350+
console.log(`\t ${startSecs} secs - ${endSecs} secs`);
351+
});
339352
});
340353
})
341354
.catch((err) => {

0 commit comments

Comments
 (0)