@@ -60,8 +60,10 @@ function syncRecognize (filename, encoding, sampleRateHertz, languageCode) {
60
60
61
61
// Detects speech in the audio file
62
62
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' ) ;
65
67
console . log ( `Transcription: ` , transcription ) ;
66
68
} )
67
69
. catch ( ( err ) => {
@@ -106,8 +108,10 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
106
108
107
109
// Detects speech in the audio file
108
110
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' ) ;
111
115
console . log ( `Transcription: ` , transcription ) ;
112
116
} )
113
117
. catch ( ( err ) => {
@@ -154,18 +158,20 @@ function syncRecognizeWords (filename, encoding, sampleRateHertz, languageCode)
154
158
155
159
// Detects speech in the audio file
156
160
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
+ } ) ;
169
175
} ) ;
170
176
} )
171
177
. catch ( ( err ) => {
@@ -212,13 +218,16 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
212
218
// Detects speech in the audio file. This creates a recognition job that you
213
219
// can wait for now, or get its result later.
214
220
speech . longRunningRecognize ( request )
215
- . then ( ( results ) => {
216
- const operation = results [ 0 ] ;
221
+ . then ( ( data ) => {
222
+ const response = data [ 0 ] ;
223
+ const operation = response ;
217
224
// Get a Promise representation of the final result of the job
218
225
return operation . promise ( ) ;
219
226
} )
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' ) ;
222
231
console . log ( `Transcription: ${ transcription } ` ) ;
223
232
} )
224
233
. catch ( ( err ) => {
@@ -265,13 +274,15 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
265
274
// Detects speech in the audio file. This creates a recognition job that you
266
275
// can wait for now, or get its result later.
267
276
speech . longRunningRecognize ( request )
268
- . then ( ( results ) => {
269
- const operation = results [ 0 ] ;
277
+ . then ( ( data ) => {
278
+ const operation = data [ 0 ] ;
270
279
// Get a Promise representation of the final result of the job
271
280
return operation . promise ( ) ;
272
281
} )
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' ) ;
275
286
console . log ( `Transcription: ${ transcription } ` ) ;
276
287
} )
277
288
. catch ( ( err ) => {
@@ -319,23 +330,25 @@ function asyncRecognizeGCSWords (gcsUri, encoding, sampleRateHertz, languageCode
319
330
// Detects speech in the audio file. This creates a recognition job that you
320
331
// can wait for now, or get its result later.
321
332
speech . longRunningRecognize ( request )
322
- . then ( ( results ) => {
323
- const operation = results [ 0 ] ;
333
+ . then ( ( data ) => {
334
+ const operation = data [ 0 ] ;
324
335
// Get a Promise representation of the final result of the job
325
336
return operation . promise ( ) ;
326
337
} )
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
+ } ) ;
339
352
} ) ;
340
353
} )
341
354
. catch ( ( err ) => {
0 commit comments