Skip to content

Commit 240bd93

Browse files
vijay-qlogicJustinBeckwith
authored andcommitted
docs(samples): updated samples code to use async await (#214)
1 parent fbedf2e commit 240bd93

File tree

4 files changed

+250
-380
lines changed

4 files changed

+250
-380
lines changed

speech/betaFeatures.js

+89-160
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
'use strict';
2525

26-
function speechTranscribeDiarization(fileName) {
26+
async function speechTranscribeDiarization(fileName) {
2727
// [START speech_transcribe_diarization_beta]
2828
const fs = require('fs');
2929

@@ -56,32 +56,25 @@ function speechTranscribeDiarization(fileName) {
5656
audio: audio,
5757
};
5858

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+
);
8174
// [END speech_transcribe_diarization_beta]
8275
}
8376

84-
function asyncSpeechTranscribeDiarizationGCS(gcsUri) {
77+
async function asyncSpeechTranscribeDiarizationGCS(gcsUri) {
8578
// [START speech_transcribe_diarization_gcs_beta]
8679
// Imports the Google Cloud client library
8780
const speech = require('@google-cloud/speech').v1p1beta1;
@@ -112,32 +105,25 @@ function asyncSpeechTranscribeDiarizationGCS(gcsUri) {
112105
audio: audio,
113106
};
114107

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+
);
137123
// [END speech_transcribe_diarization_gcs_beta]
138124
}
139125

140-
function speechTranscribeMultiChannel(fileName) {
126+
async function speechTranscribeMultiChannel(fileName) {
141127
// [START speech_transcribe_multichannel_beta]
142128
const fs = require('fs');
143129

@@ -168,28 +154,17 @@ function speechTranscribeMultiChannel(fileName) {
168154
audio: audio,
169155
};
170156

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}`;
185161
})
186-
.catch(err => {
187-
console.error('ERROR:', err);
188-
});
162+
.join('\n');
163+
console.log(`Transcription: \n${transcription}`);
189164
// [END speech_transcribe_multichannel_beta]
190165
}
191166

192-
function speechTranscribeMultichannelGCS(gcsUri) {
167+
async function speechTranscribeMultichannelGCS(gcsUri) {
193168
// [START speech_transcribe_multichannel_gcs_beta]
194169
const speech = require('@google-cloud/speech').v1p1beta1;
195170

@@ -212,28 +187,17 @@ function speechTranscribeMultichannelGCS(gcsUri) {
212187
audio: audio,
213188
};
214189

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}`;
229194
})
230-
.catch(err => {
231-
console.error('ERROR:', err);
232-
});
195+
.join('\n');
196+
console.log(`Transcription: \n${transcription}`);
233197
// [END speech_transcribe_multichannel_gcs_beta]
234198
}
235199

236-
function speechTranscribeMultilang(fileName) {
200+
async function speechTranscribeMultilang(fileName) {
237201
// [START speech_transcribe_multilanguage_beta]
238202
const fs = require('fs');
239203

@@ -264,22 +228,15 @@ function speechTranscribeMultilang(fileName) {
264228
audio: audio,
265229
};
266230

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}`);
279236
// [END speech_transcribe_multilanguage_beta]
280237
}
281238

282-
function speechTranscribeMultilangGCS(gcsUri) {
239+
async function speechTranscribeMultilangGCS(gcsUri) {
283240
// [START speech_transcribe_multilanguage_gcs_beta]
284241
// Imports the Google Cloud client library
285242
const speech = require('@google-cloud/speech').v1p1beta1;
@@ -308,26 +265,16 @@ function speechTranscribeMultilangGCS(gcsUri) {
308265
audio: audio,
309266
};
310267

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}`);
327274
// [END speech_transcribe_multilanguage_gcs_beta]
328275
}
329276

330-
function speechTranscribeWordLevelConfidence(fileName) {
277+
async function speechTranscribeWordLevelConfidence(fileName) {
331278
// [START speech_transcribe_word_level_confidence_beta]
332279
const fs = require('fs');
333280

@@ -358,33 +305,24 @@ function speechTranscribeWordLevelConfidence(fileName) {
358305
audio: audio,
359306
};
360307

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+
});
384322
// [END speech_transcribe_word_level_confidence_beta]
385323
}
386324

387-
function speechTranscribeWordLevelConfidenceGCS(gcsUri) {
325+
async function speechTranscribeWordLevelConfidenceGCS(gcsUri) {
388326
// [START speech_transcribe_word_level_confidence_gcs_beta]
389327
// Imports the Google Cloud client library
390328
const speech = require('@google-cloud/speech').v1p1beta1;
@@ -413,29 +351,20 @@ function speechTranscribeWordLevelConfidenceGCS(gcsUri) {
413351
audio: audio,
414352
};
415353

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+
});
439368
// [END speech_transcribe_word_level_confidence_gcs_beta]
440369
}
441370

0 commit comments

Comments
 (0)