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

Function main

speech/adaptationCustomClassReference.v2.js:23–102  ·  view source on GitHub ↗
(
  projectId,
  customClassId,
  phraseSetId,
  recognizerName
)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

createCustomClassV2Function · 0.85

Tested by

no test coverage detected