MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

speech/transcribeContextClasses.js:22–73  ·  view source on GitHub ↗
(storageUri)

Source from the content-addressed store, hash-verified

20// usage: node samples/transcribeContextClasses.js <storageUri>
21
22function main(storageUri) {
23 // [START speech_transcribe_sync]
24 // Provides "hints" to the speech recognizer to favor
25 // specific classes of words in the results.
26
27 // Imports the Google Cloud client library
28 const speech = require('@google-cloud/speech');
29
30 // Creates a client
31 const client = new speech.SpeechClient();
32
33 async function transcribeContextClasses() {
34 // storageUri = 'gs://YOUR_BUCKET_ID/path/to/your/file.wav'
35 const audio = {
36 uri: storageUri,
37 };
38
39 // SpeechContext: to configure your speech_context see:
40 // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#speechcontext
41 // Full list of supported phrases(class tokens) here:
42 // https://cloud.google.com/speech-to-text/docs/class-tokens
43 const speechContext = {
44 phrases: ['$TIME'],
45 };
46
47 // RecognitionConfig: to configure your encoding and sample_rate_hertz, see:
48 // https://cloud.google.com/speech-to-text/docs/reference/rpc/google.cloud.speech.v1#recognitionconfig
49 const config = {
50 encoding: 'LINEAR16',
51 sampleRateHertz: 8000,
52 languageCode: 'en-US',
53 speechContexts: [speechContext],
54 };
55
56 const request = {
57 config: config,
58 audio: audio,
59 };
60
61 // Detects speech in the audio file.
62 const [response] = await client.recognize(request);
63 response.results.forEach((result, index) => {
64 const transcript = result.alternatives[0].transcript;
65 console.log('-'.repeat(20));
66 console.log(`First alternative of result ${index}`);
67 console.log(`Transcript: ${transcript}`);
68 });
69 }
70
71 transcribeContextClasses();
72 // [END speech_transcribe_sync]
73}
74
75process.on('unhandledRejection', err => {
76 console.error(err.message);

Callers 1

Calls 1

transcribeContextClassesFunction · 0.85

Tested by

no test coverage detected