Skip to content

Commit 631ae96

Browse files
authored
Added a sample code to show auto punctuation (#57)
* Added a sample code to show auto punctuation * Ran prettier
1 parent f612114 commit 631ae96

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

speech/recognize.v1p1beta1.js

+65
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,58 @@ function syncRecognizeModelSelectionGCS(
132132
// [END speech_transcribe_model_selection_gcs]
133133
}
134134

135+
function syncRecognizeWithAutoPunctuation(
136+
filename,
137+
encoding,
138+
sampleRateHertz,
139+
languageCode
140+
) {
141+
// [START speech_transcribe_file_with_auto_punctuation]
142+
// Imports the Google Cloud client library
143+
const fs = require('fs');
144+
const speech = require('@google-cloud/speech');
145+
146+
// Creates a client
147+
const client = new speech.SpeechClient();
148+
149+
/**
150+
* TODO(developer): Uncomment the following lines before running the sample.
151+
*/
152+
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
153+
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
154+
// const sampleRateHertz = 16000;
155+
// const languageCode = 'BCP-47 language code, e.g. en-US';
156+
157+
const config = {
158+
encoding: encoding,
159+
languageCode: languageCode,
160+
enableAutomaticPunctuation: true,
161+
};
162+
const audio = {
163+
content: fs.readFileSync(filename).toString('base64'),
164+
};
165+
166+
const request = {
167+
config: config,
168+
audio: audio,
169+
};
170+
171+
// Detects speech in the audio file
172+
client
173+
.recognize(request)
174+
.then(data => {
175+
const response = data[0];
176+
const transcription = response.results
177+
.map(result => result.alternatives[0].transcript)
178+
.join('\n');
179+
console.log(`Transcription: `, transcription);
180+
})
181+
.catch(err => {
182+
console.error('ERROR:', err);
183+
});
184+
// [END speech_transcribe_file_with_auto_punctuation]
185+
}
186+
135187
require(`yargs`)
136188
.demand(1)
137189
.command(
@@ -160,6 +212,18 @@ require(`yargs`)
160212
opts.languageCode
161213
)
162214
)
215+
.command(
216+
`sync-auto-punctuation <filename>`,
217+
`Detects speech in a local audio file with auto punctuation.`,
218+
{},
219+
opts =>
220+
syncRecognizeWithAutoPunctuation(
221+
opts.filename,
222+
opts.encoding,
223+
opts.sampleRateHertz,
224+
opts.languageCode
225+
)
226+
)
163227
.options({
164228
encoding: {
165229
alias: 'e',
@@ -189,6 +253,7 @@ require(`yargs`)
189253
.example(
190254
`node $0 sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000`
191255
)
256+
.example(`node $0 sync-auto-punctuation ./resources/commercial_mono.wav`)
192257
.wrap(120)
193258
.recommendCommands()
194259
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)

speech/resources/commercial_mono.wav

708 KB
Binary file not shown.

speech/system-test/recognize.v1p1beta1.test.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ const {runAsync} = require(`@google-cloud/nodejs-repo-tools`);
2525
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
2626
const cmd = `node recognize.v1p1beta1.js`;
2727
const cwd = path.join(__dirname, `..`);
28-
const filename = `Google_Gnome.wav`;
29-
const filepath = path.join(__dirname, `../resources/${filename}`);
30-
const text = `the weather outside is sunny`;
28+
const filename1 = `Google_Gnome.wav`;
29+
const filename2 = `commercial_mono.wav`;
30+
const filepath1 = path.join(__dirname, `../resources/${filename1}`);
31+
const filepath2 = path.join(__dirname, `../resources/${filename2}`);
32+
const text1 = `the weather outside is sunny`;
33+
const text2 = `Terrific. It's on the way.`;
3134

3235
test.before(async () => {
3336
const [bucket] = await storage.createBucket(bucketName);
34-
await bucket.upload(filepath);
37+
await bucket.upload(filepath1);
3538
});
3639

3740
test.after.always(async () => {
@@ -43,17 +46,25 @@ test.after.always(async () => {
4346

4447
test(`should run sync recognize with model selection`, async t => {
4548
const model = `video`;
46-
const output = await runAsync(`${cmd} sync-model ${filepath} ${model}`, cwd);
49+
const output = await runAsync(`${cmd} sync-model ${filepath1} ${model}`, cwd);
4750
t.true(output.includes(`Transcription:`));
48-
t.true(output.includes(text));
51+
t.true(output.includes(text1));
4952
});
5053

5154
test(`should run sync recognize on a GCS file with model selection`, async t => {
5255
const model = `video`;
5356
const output = await runAsync(
54-
`${cmd} sync-model-gcs gs://${bucketName}/${filename} ${model}`,
57+
`${cmd} sync-model-gcs gs://${bucketName}/${filename1} ${model}`,
5558
cwd
5659
);
5760
t.true(output.includes(`Transcription:`));
58-
t.true(output.includes(text));
61+
t.true(output.includes(text1));
62+
});
63+
64+
test(`should run sync recognize with auto punctuation`, async t => {
65+
const output = await runAsync(
66+
`${cmd} sync-auto-punctuation ${filepath2}`,
67+
cwd
68+
);
69+
t.true(output.includes(text2));
5970
});

0 commit comments

Comments
 (0)