23
23
24
24
'use strict' ;
25
25
26
- function speechTranscribeDiarization ( fileName ) {
26
+ async function speechTranscribeDiarization ( fileName ) {
27
27
// [START speech_transcribe_diarization_beta]
28
28
const fs = require ( 'fs' ) ;
29
29
@@ -56,32 +56,25 @@ function speechTranscribeDiarization(fileName) {
56
56
audio : audio ,
57
57
} ;
58
58
59
- client
60
- . recognize ( request )
61
- . then ( data => {
62
- const response = data [ 0 ] ;
63
- const transcription = response . results
64
- . map ( result => result . alternatives [ 0 ] . transcript )
65
- . join ( '\n' ) ;
66
- console . log ( `Transcription: ${ transcription } ` ) ;
67
- console . log ( `Speaker Diarization:` ) ;
68
- const result = response . results [ response . results . length - 1 ] ;
69
- const wordsInfo = result . alternatives [ 0 ] . words ;
70
- // Note: The transcript within each result is separate and sequential per result.
71
- // However, the words list within an alternative includes all the words
72
- // from all the results thus far. Thus, to get all the words with speaker
73
- // tags, you only have to take the words list from the last result:
74
- wordsInfo . forEach ( a =>
75
- console . log ( ` word: ${ a . word } , speakerTag: ${ a . speakerTag } ` )
76
- ) ;
77
- } )
78
- . catch ( err => {
79
- console . error ( 'ERROR:' , err ) ;
80
- } ) ;
59
+ const [ response ] = await client . recognize ( request ) ;
60
+ const transcription = response . results
61
+ . map ( result => result . alternatives [ 0 ] . transcript )
62
+ . join ( '\n' ) ;
63
+ console . log ( `Transcription: ${ transcription } ` ) ;
64
+ console . log ( `Speaker Diarization:` ) ;
65
+ const result = response . results [ response . results . length - 1 ] ;
66
+ const wordsInfo = result . alternatives [ 0 ] . words ;
67
+ // Note: The transcript within each result is separate and sequential per result.
68
+ // However, the words list within an alternative includes all the words
69
+ // from all the results thus far. Thus, to get all the words with speaker
70
+ // tags, you only have to take the words list from the last result:
71
+ wordsInfo . forEach ( a =>
72
+ console . log ( ` word: ${ a . word } , speakerTag: ${ a . speakerTag } ` )
73
+ ) ;
81
74
// [END speech_transcribe_diarization_beta]
82
75
}
83
76
84
- function asyncSpeechTranscribeDiarizationGCS ( gcsUri ) {
77
+ async function asyncSpeechTranscribeDiarizationGCS ( gcsUri ) {
85
78
// [START speech_transcribe_diarization_gcs_beta]
86
79
// Imports the Google Cloud client library
87
80
const speech = require ( '@google-cloud/speech' ) . v1p1beta1 ;
@@ -112,32 +105,25 @@ function asyncSpeechTranscribeDiarizationGCS(gcsUri) {
112
105
audio : audio ,
113
106
} ;
114
107
115
- client
116
- . recognize ( request )
117
- . then ( data => {
118
- const response = data [ 0 ] ;
119
- const transcription = response . results
120
- . map ( result => result . alternatives [ 0 ] . transcript )
121
- . join ( '\n' ) ;
122
- console . log ( `Transcription: ${ transcription } ` ) ;
123
- console . log ( `Speaker Diarization:` ) ;
124
- const result = response . results [ response . results . length - 1 ] ;
125
- const wordsInfo = result . alternatives [ 0 ] . words ;
126
- // Note: The transcript within each result is separate and sequential per result.
127
- // However, the words list within an alternative includes all the words
128
- // from all the results thus far. Thus, to get all the words with speaker
129
- // tags, you only have to take the words list from the last result:
130
- wordsInfo . forEach ( a =>
131
- console . log ( ` word: ${ a . word } , speakerTag: ${ a . speakerTag } ` )
132
- ) ;
133
- } )
134
- . catch ( err => {
135
- console . error ( 'ERROR:' , err ) ;
136
- } ) ;
108
+ const [ response ] = await client . recognize ( request ) ;
109
+ const transcription = response . results
110
+ . map ( result => result . alternatives [ 0 ] . transcript )
111
+ . join ( '\n' ) ;
112
+ console . log ( `Transcription: ${ transcription } ` ) ;
113
+ console . log ( `Speaker Diarization:` ) ;
114
+ const result = response . results [ response . results . length - 1 ] ;
115
+ const wordsInfo = result . alternatives [ 0 ] . words ;
116
+ // Note: The transcript within each result is separate and sequential per result.
117
+ // However, the words list within an alternative includes all the words
118
+ // from all the results thus far. Thus, to get all the words with speaker
119
+ // tags, you only have to take the words list from the last result:
120
+ wordsInfo . forEach ( a =>
121
+ console . log ( ` word: ${ a . word } , speakerTag: ${ a . speakerTag } ` )
122
+ ) ;
137
123
// [END speech_transcribe_diarization_gcs_beta]
138
124
}
139
125
140
- function speechTranscribeMultiChannel ( fileName ) {
126
+ async function speechTranscribeMultiChannel ( fileName ) {
141
127
// [START speech_transcribe_multichannel_beta]
142
128
const fs = require ( 'fs' ) ;
143
129
@@ -168,28 +154,17 @@ function speechTranscribeMultiChannel(fileName) {
168
154
audio : audio ,
169
155
} ;
170
156
171
- client
172
- . recognize ( request )
173
- . then ( data => {
174
- const response = data [ 0 ] ;
175
- const transcription = response . results
176
- . map (
177
- result =>
178
- ` Channel Tag: ` +
179
- result . channelTag +
180
- ` ` +
181
- result . alternatives [ 0 ] . transcript
182
- )
183
- . join ( '\n' ) ;
184
- console . log ( `Transcription: \n${ transcription } ` ) ;
157
+ const [ response ] = await client . recognize ( request ) ;
158
+ const transcription = response . results
159
+ . map ( result => {
160
+ ` Channel Tag: ${ result . channelTag } ${ result . alternatives [ 0 ] . transcript } ` ;
185
161
} )
186
- . catch ( err => {
187
- console . error ( 'ERROR:' , err ) ;
188
- } ) ;
162
+ . join ( '\n' ) ;
163
+ console . log ( `Transcription: \n${ transcription } ` ) ;
189
164
// [END speech_transcribe_multichannel_beta]
190
165
}
191
166
192
- function speechTranscribeMultichannelGCS ( gcsUri ) {
167
+ async function speechTranscribeMultichannelGCS ( gcsUri ) {
193
168
// [START speech_transcribe_multichannel_gcs_beta]
194
169
const speech = require ( '@google-cloud/speech' ) . v1p1beta1 ;
195
170
@@ -212,28 +187,17 @@ function speechTranscribeMultichannelGCS(gcsUri) {
212
187
audio : audio ,
213
188
} ;
214
189
215
- client
216
- . recognize ( request )
217
- . then ( data => {
218
- const response = data [ 0 ] ;
219
- const transcription = response . results
220
- . map (
221
- result =>
222
- ` Channel Tag: ` +
223
- result . channelTag +
224
- ` ` +
225
- result . alternatives [ 0 ] . transcript
226
- )
227
- . join ( '\n' ) ;
228
- console . log ( `Transcription: \n${ transcription } ` ) ;
190
+ const [ response ] = await client . recognize ( request ) ;
191
+ const transcription = response . results
192
+ . map ( result => {
193
+ ` Channel Tag: ${ result . channelTag } ${ result . alternatives [ 0 ] . transcript } ` ;
229
194
} )
230
- . catch ( err => {
231
- console . error ( 'ERROR:' , err ) ;
232
- } ) ;
195
+ . join ( '\n' ) ;
196
+ console . log ( `Transcription: \n${ transcription } ` ) ;
233
197
// [END speech_transcribe_multichannel_gcs_beta]
234
198
}
235
199
236
- function speechTranscribeMultilang ( fileName ) {
200
+ async function speechTranscribeMultilang ( fileName ) {
237
201
// [START speech_transcribe_multilanguage_beta]
238
202
const fs = require ( 'fs' ) ;
239
203
@@ -264,22 +228,15 @@ function speechTranscribeMultilang(fileName) {
264
228
audio : audio ,
265
229
} ;
266
230
267
- client
268
- . recognize ( request )
269
- . then ( data => {
270
- const response = data [ 0 ] ;
271
- const transcription = response . results
272
- . map ( result => result . alternatives [ 0 ] . transcript )
273
- . join ( '\n' ) ;
274
- console . log ( `Transcription: ${ transcription } ` ) ;
275
- } )
276
- . catch ( err => {
277
- console . error ( 'ERROR:' , err ) ;
278
- } ) ;
231
+ const [ response ] = await client . recognize ( request ) ;
232
+ const transcription = response . results
233
+ . map ( result => result . alternatives [ 0 ] . transcript )
234
+ . join ( '\n' ) ;
235
+ console . log ( `Transcription: ${ transcription } ` ) ;
279
236
// [END speech_transcribe_multilanguage_beta]
280
237
}
281
238
282
- function speechTranscribeMultilangGCS ( gcsUri ) {
239
+ async function speechTranscribeMultilangGCS ( gcsUri ) {
283
240
// [START speech_transcribe_multilanguage_gcs_beta]
284
241
// Imports the Google Cloud client library
285
242
const speech = require ( '@google-cloud/speech' ) . v1p1beta1 ;
@@ -308,26 +265,16 @@ function speechTranscribeMultilangGCS(gcsUri) {
308
265
audio : audio ,
309
266
} ;
310
267
311
- client
312
- . longRunningRecognize ( request )
313
- . then ( data => {
314
- const operation = data [ 0 ] ;
315
- return operation . promise ( ) ;
316
- } )
317
- . then ( data => {
318
- const response = data [ 0 ] ;
319
- const transcription = response . results
320
- . map ( result => result . alternatives [ 0 ] . transcript )
321
- . join ( '\n' ) ;
322
- console . log ( `Transcription: ${ transcription } ` ) ;
323
- } )
324
- . catch ( err => {
325
- console . error ( 'ERROR:' , err ) ;
326
- } ) ;
268
+ const [ operation ] = await client . longRunningRecognize ( request ) ;
269
+ const [ response ] = await operation . promise ( ) ;
270
+ const transcription = response . results
271
+ . map ( result => result . alternatives [ 0 ] . transcript )
272
+ . join ( '\n' ) ;
273
+ console . log ( `Transcription: ${ transcription } ` ) ;
327
274
// [END speech_transcribe_multilanguage_gcs_beta]
328
275
}
329
276
330
- function speechTranscribeWordLevelConfidence ( fileName ) {
277
+ async function speechTranscribeWordLevelConfidence ( fileName ) {
331
278
// [START speech_transcribe_word_level_confidence_beta]
332
279
const fs = require ( 'fs' ) ;
333
280
@@ -358,33 +305,24 @@ function speechTranscribeWordLevelConfidence(fileName) {
358
305
audio : audio ,
359
306
} ;
360
307
361
- client
362
- . recognize ( request )
363
- . then ( data => {
364
- const response = data [ 0 ] ;
365
- const transcription = response . results
366
- . map ( result => result . alternatives [ 0 ] . transcript )
367
- . join ( '\n' ) ;
368
- const confidence = response . results
369
- . map ( result => result . alternatives [ 0 ] . confidence )
370
- . join ( `\n` ) ;
371
- console . log (
372
- `Transcription: ${ transcription } \n Confidence: ${ confidence } `
373
- ) ;
374
-
375
- console . log ( `Word-Level-Confidence:` ) ;
376
- const words = response . results . map ( result => result . alternatives [ 0 ] ) ;
377
- words [ 0 ] . words . forEach ( a => {
378
- console . log ( ` word: ${ a . word } , confidence: ${ a . confidence } ` ) ;
379
- } ) ;
380
- } )
381
- . catch ( err => {
382
- console . error ( 'ERROR:' , err ) ;
383
- } ) ;
308
+ const [ response ] = await client . recognize ( request ) ;
309
+ const transcription = response . results
310
+ . map ( result => result . alternatives [ 0 ] . transcript )
311
+ . join ( '\n' ) ;
312
+ const confidence = response . results
313
+ . map ( result => result . alternatives [ 0 ] . confidence )
314
+ . join ( `\n` ) ;
315
+ console . log ( `Transcription: ${ transcription } \n Confidence: ${ confidence } ` ) ;
316
+
317
+ console . log ( `Word-Level-Confidence:` ) ;
318
+ const words = response . results . map ( result => result . alternatives [ 0 ] ) ;
319
+ words [ 0 ] . words . forEach ( a => {
320
+ console . log ( ` word: ${ a . word } , confidence: ${ a . confidence } ` ) ;
321
+ } ) ;
384
322
// [END speech_transcribe_word_level_confidence_beta]
385
323
}
386
324
387
- function speechTranscribeWordLevelConfidenceGCS ( gcsUri ) {
325
+ async function speechTranscribeWordLevelConfidenceGCS ( gcsUri ) {
388
326
// [START speech_transcribe_word_level_confidence_gcs_beta]
389
327
// Imports the Google Cloud client library
390
328
const speech = require ( '@google-cloud/speech' ) . v1p1beta1 ;
@@ -413,29 +351,20 @@ function speechTranscribeWordLevelConfidenceGCS(gcsUri) {
413
351
audio : audio ,
414
352
} ;
415
353
416
- client
417
- . recognize ( request )
418
- . then ( data => {
419
- const response = data [ 0 ] ;
420
- const transcription = response . results
421
- . map ( result => result . alternatives [ 0 ] . transcript )
422
- . join ( '\n' ) ;
423
- const confidence = response . results
424
- . map ( result => result . alternatives [ 0 ] . confidence )
425
- . join ( `\n` ) ;
426
- console . log (
427
- `Transcription: ${ transcription } \n Confidence: ${ confidence } `
428
- ) ;
429
-
430
- console . log ( `Word-Level-Confidence:` ) ;
431
- const words = response . results . map ( result => result . alternatives [ 0 ] ) ;
432
- words [ 0 ] . words . forEach ( a => {
433
- console . log ( ` word: ${ a . word } , confidence: ${ a . confidence } ` ) ;
434
- } ) ;
435
- } )
436
- . catch ( err => {
437
- console . error ( 'ERROR:' , err ) ;
438
- } ) ;
354
+ const [ response ] = await client . recognize ( request ) ;
355
+ const transcription = response . results
356
+ . map ( result => result . alternatives [ 0 ] . transcript )
357
+ . join ( '\n' ) ;
358
+ const confidence = response . results
359
+ . map ( result => result . alternatives [ 0 ] . confidence )
360
+ . join ( `\n` ) ;
361
+ console . log ( `Transcription: ${ transcription } \n Confidence: ${ confidence } ` ) ;
362
+
363
+ console . log ( `Word-Level-Confidence:` ) ;
364
+ const words = response . results . map ( result => result . alternatives [ 0 ] ) ;
365
+ words [ 0 ] . words . forEach ( a => {
366
+ console . log ( ` word: ${ a . word } , confidence: ${ a . confidence } ` ) ;
367
+ } ) ;
439
368
// [END speech_transcribe_word_level_confidence_gcs_beta]
440
369
}
441
370
0 commit comments