Skip to content

Commit

Permalink
docs(samples): updated samples code to use async await (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
vijay-qlogic authored and Ace Nassri committed Nov 17, 2022
1 parent 4164b47 commit 0aaeaa9
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 380 deletions.
249 changes: 89 additions & 160 deletions speech/betaFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

'use strict';

function speechTranscribeDiarization(fileName) {
async function speechTranscribeDiarization(fileName) {
// [START speech_transcribe_diarization_beta]
const fs = require('fs');

Expand Down Expand Up @@ -56,32 +56,25 @@ function speechTranscribeDiarization(fileName) {
audio: audio,
};

client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
console.log(`Speaker Diarization:`);
const result = response.results[response.results.length - 1];
const wordsInfo = result.alternatives[0].words;
// Note: The transcript within each result is separate and sequential per result.
// However, the words list within an alternative includes all the words
// from all the results thus far. Thus, to get all the words with speaker
// tags, you only have to take the words list from the last result:
wordsInfo.forEach(a =>
console.log(` word: ${a.word}, speakerTag: ${a.speakerTag}`)
);
})
.catch(err => {
console.error('ERROR:', err);
});
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
console.log(`Speaker Diarization:`);
const result = response.results[response.results.length - 1];
const wordsInfo = result.alternatives[0].words;
// Note: The transcript within each result is separate and sequential per result.
// However, the words list within an alternative includes all the words
// from all the results thus far. Thus, to get all the words with speaker
// tags, you only have to take the words list from the last result:
wordsInfo.forEach(a =>
console.log(` word: ${a.word}, speakerTag: ${a.speakerTag}`)
);
// [END speech_transcribe_diarization_beta]
}

function asyncSpeechTranscribeDiarizationGCS(gcsUri) {
async function asyncSpeechTranscribeDiarizationGCS(gcsUri) {
// [START speech_transcribe_diarization_gcs_beta]
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech').v1p1beta1;
Expand Down Expand Up @@ -112,32 +105,25 @@ function asyncSpeechTranscribeDiarizationGCS(gcsUri) {
audio: audio,
};

client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
console.log(`Speaker Diarization:`);
const result = response.results[response.results.length - 1];
const wordsInfo = result.alternatives[0].words;
// Note: The transcript within each result is separate and sequential per result.
// However, the words list within an alternative includes all the words
// from all the results thus far. Thus, to get all the words with speaker
// tags, you only have to take the words list from the last result:
wordsInfo.forEach(a =>
console.log(` word: ${a.word}, speakerTag: ${a.speakerTag}`)
);
})
.catch(err => {
console.error('ERROR:', err);
});
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
console.log(`Speaker Diarization:`);
const result = response.results[response.results.length - 1];
const wordsInfo = result.alternatives[0].words;
// Note: The transcript within each result is separate and sequential per result.
// However, the words list within an alternative includes all the words
// from all the results thus far. Thus, to get all the words with speaker
// tags, you only have to take the words list from the last result:
wordsInfo.forEach(a =>
console.log(` word: ${a.word}, speakerTag: ${a.speakerTag}`)
);
// [END speech_transcribe_diarization_gcs_beta]
}

function speechTranscribeMultiChannel(fileName) {
async function speechTranscribeMultiChannel(fileName) {
// [START speech_transcribe_multichannel_beta]
const fs = require('fs');

Expand Down Expand Up @@ -168,28 +154,17 @@ function speechTranscribeMultiChannel(fileName) {
audio: audio,
};

client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(
result =>
` Channel Tag: ` +
result.channelTag +
` ` +
result.alternatives[0].transcript
)
.join('\n');
console.log(`Transcription: \n${transcription}`);
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => {
` Channel Tag: ${result.channelTag} ${result.alternatives[0].transcript}`;
})
.catch(err => {
console.error('ERROR:', err);
});
.join('\n');
console.log(`Transcription: \n${transcription}`);
// [END speech_transcribe_multichannel_beta]
}

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

Expand All @@ -212,28 +187,17 @@ function speechTranscribeMultichannelGCS(gcsUri) {
audio: audio,
};

client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(
result =>
` Channel Tag: ` +
result.channelTag +
` ` +
result.alternatives[0].transcript
)
.join('\n');
console.log(`Transcription: \n${transcription}`);
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => {
` Channel Tag: ${result.channelTag} ${result.alternatives[0].transcript}`;
})
.catch(err => {
console.error('ERROR:', err);
});
.join('\n');
console.log(`Transcription: \n${transcription}`);
// [END speech_transcribe_multichannel_gcs_beta]
}

function speechTranscribeMultilang(fileName) {
async function speechTranscribeMultilang(fileName) {
// [START speech_transcribe_multilanguage_beta]
const fs = require('fs');

Expand Down Expand Up @@ -264,22 +228,15 @@ function speechTranscribeMultilang(fileName) {
audio: audio,
};

client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
})
.catch(err => {
console.error('ERROR:', err);
});
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
// [END speech_transcribe_multilanguage_beta]
}

function speechTranscribeMultilangGCS(gcsUri) {
async function speechTranscribeMultilangGCS(gcsUri) {
// [START speech_transcribe_multilanguage_gcs_beta]
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech').v1p1beta1;
Expand Down Expand Up @@ -308,26 +265,16 @@ function speechTranscribeMultilangGCS(gcsUri) {
audio: audio,
};

client
.longRunningRecognize(request)
.then(data => {
const operation = data[0];
return operation.promise();
})
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
})
.catch(err => {
console.error('ERROR:', err);
});
const [operation] = await client.longRunningRecognize(request);
const [response] = await operation.promise();
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
// [END speech_transcribe_multilanguage_gcs_beta]
}

function speechTranscribeWordLevelConfidence(fileName) {
async function speechTranscribeWordLevelConfidence(fileName) {
// [START speech_transcribe_word_level_confidence_beta]
const fs = require('fs');

Expand Down Expand Up @@ -358,33 +305,24 @@ function speechTranscribeWordLevelConfidence(fileName) {
audio: audio,
};

client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
const confidence = response.results
.map(result => result.alternatives[0].confidence)
.join(`\n`);
console.log(
`Transcription: ${transcription} \n Confidence: ${confidence}`
);

console.log(`Word-Level-Confidence:`);
const words = response.results.map(result => result.alternatives[0]);
words[0].words.forEach(a => {
console.log(` word: ${a.word}, confidence: ${a.confidence}`);
});
})
.catch(err => {
console.error('ERROR:', err);
});
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
const confidence = response.results
.map(result => result.alternatives[0].confidence)
.join(`\n`);
console.log(`Transcription: ${transcription} \n Confidence: ${confidence}`);

console.log(`Word-Level-Confidence:`);
const words = response.results.map(result => result.alternatives[0]);
words[0].words.forEach(a => {
console.log(` word: ${a.word}, confidence: ${a.confidence}`);
});
// [END speech_transcribe_word_level_confidence_beta]
}

function speechTranscribeWordLevelConfidenceGCS(gcsUri) {
async function speechTranscribeWordLevelConfidenceGCS(gcsUri) {
// [START speech_transcribe_word_level_confidence_gcs_beta]
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech').v1p1beta1;
Expand Down Expand Up @@ -413,29 +351,20 @@ function speechTranscribeWordLevelConfidenceGCS(gcsUri) {
audio: audio,
};

client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
const confidence = response.results
.map(result => result.alternatives[0].confidence)
.join(`\n`);
console.log(
`Transcription: ${transcription} \n Confidence: ${confidence}`
);

console.log(`Word-Level-Confidence:`);
const words = response.results.map(result => result.alternatives[0]);
words[0].words.forEach(a => {
console.log(` word: ${a.word}, confidence: ${a.confidence}`);
});
})
.catch(err => {
console.error('ERROR:', err);
});
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
const confidence = response.results
.map(result => result.alternatives[0].confidence)
.join(`\n`);
console.log(`Transcription: ${transcription} \n Confidence: ${confidence}`);

console.log(`Word-Level-Confidence:`);
const words = response.results.map(result => result.alternatives[0]);
words[0].words.forEach(a => {
console.log(` word: ${a.word}, confidence: ${a.confidence}`);
});
// [END speech_transcribe_word_level_confidence_gcs_beta]
}

Expand Down
Loading

0 comments on commit 0aaeaa9

Please sign in to comment.