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

Function main

speech/adaptationPhraseSetReference.v2.js:21–81  ·  view source on GitHub ↗
(projectId, phraseSetId, recognizerName)

Source from the content-addressed store, hash-verified

19// description: Create persistent PhraseSet for adaptation in Speech-to-Text v2
20
21async function main(projectId, phraseSetId, recognizerName) {
22 // [START speech_adaptation_v2_phrase_set_reference]
23 /**
24 * TODO(developer): Uncomment these variables before running the sample.
25 */
26 // const projectId = "your Google Cloud project id";
27 // const phraseSetId = "a unique ID in your project for this phrase set";
28 // const recognizerName = "projects/[PROJECT_ID]/locations/[LOCATION]/recognizers/[RECOGNIZER_ID]";
29
30 // TODO(developer): Replace with your own file
31 const audioFilePath = 'resources/brooklyn.flac';
32
33 // Imports the Google Cloud Speech-to-Text library (v2)
34 const speech = require('@google-cloud/speech').v2;
35 const fs = require('fs');
36
37 async function createPersistentPhraseSetV2() {
38 // Instantiate the Speech-to-Text client
39 const client = new speech.SpeechClient();
40
41 // Create a persistent PhraseSet to reference in a recognition request
42 const parent = `projects/${projectId}/locations/global`;
43 const phraseSetRequest = {
44 parent,
45 phraseSetId,
46 phraseSet: {
47 phrases: [{value: 'Brooklyn', boost: 10}],
48 },
49 };
50
51 const phraseSetOp = await client.createPhraseSet(phraseSetRequest);
52 const phraseSet = phraseSetOp[0].result;
53 console.log(`PhraseSet name: ${phraseSet.name}`);
54
55 // Add a reference to the Phrase set into the recognition request
56 const adaptation = {
57 phraseSets: [{phraseSet: phraseSet.name}],
58 };
59
60 const config = {
61 autoDecodingConfig: {},
62 adaptation,
63 };
64
65 // Transcribe audio using speech adaptation
66 const content = fs.readFileSync(audioFilePath).toString('base64');
67 const transcriptionRequest = {
68 recognizer: recognizerName,
69 config,
70 content,
71 };
72
73 const response = await client.recognize(transcriptionRequest);
74 for (const result of response[0].results) {
75 console.log(`Transcript: ${result.alternatives[0].transcript}`);
76 }
77 }
78

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected