Skip to content

Commit

Permalink
Added a sample code to show auto punctuation (#57)
Browse files Browse the repository at this point in the history
* Added a sample code to show auto punctuation

* Ran prettier
  • Loading branch information
happyhuman authored and Ace Nassri committed Nov 17, 2022
1 parent b15b1cc commit e49eac0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
65 changes: 65 additions & 0 deletions speech/recognize.v1p1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,58 @@ function syncRecognizeModelSelectionGCS(
// [END speech_transcribe_model_selection_gcs]
}

function syncRecognizeWithAutoPunctuation(
filename,
encoding,
sampleRateHertz,
languageCode
) {
// [START speech_transcribe_file_with_auto_punctuation]
// Imports the Google Cloud client library
const fs = require('fs');
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const config = {
encoding: encoding,
languageCode: languageCode,
enableAutomaticPunctuation: true,
};
const audio = {
content: fs.readFileSync(filename).toString('base64'),
};

const request = {
config: config,
audio: audio,
};

// Detects speech in the audio file
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);
});
// [END speech_transcribe_file_with_auto_punctuation]
}

require(`yargs`)
.demand(1)
.command(
Expand Down Expand Up @@ -160,6 +212,18 @@ require(`yargs`)
opts.languageCode
)
)
.command(
`sync-auto-punctuation <filename>`,
`Detects speech in a local audio file with auto punctuation.`,
{},
opts =>
syncRecognizeWithAutoPunctuation(
opts.filename,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.options({
encoding: {
alias: 'e',
Expand Down Expand Up @@ -189,6 +253,7 @@ require(`yargs`)
.example(
`node $0 sync-model-gcs gs://gcs-test-data/Google_Gnome.wav phone_call -e FLAC -r 16000`
)
.example(`node $0 sync-auto-punctuation ./resources/commercial_mono.wav`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
Expand Down
Binary file added speech/resources/commercial_mono.wav
Binary file not shown.
27 changes: 19 additions & 8 deletions speech/system-test/recognize.v1p1beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ const {runAsync} = require(`@google-cloud/nodejs-repo-tools`);
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node recognize.v1p1beta1.js`;
const cwd = path.join(__dirname, `..`);
const filename = `Google_Gnome.wav`;
const filepath = path.join(__dirname, `../resources/${filename}`);
const text = `the weather outside is sunny`;
const filename1 = `Google_Gnome.wav`;
const filename2 = `commercial_mono.wav`;
const filepath1 = path.join(__dirname, `../resources/${filename1}`);
const filepath2 = path.join(__dirname, `../resources/${filename2}`);
const text1 = `the weather outside is sunny`;
const text2 = `Terrific. It's on the way.`;

test.before(async () => {
const [bucket] = await storage.createBucket(bucketName);
await bucket.upload(filepath);
await bucket.upload(filepath1);
});

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

test(`should run sync recognize with model selection`, async t => {
const model = `video`;
const output = await runAsync(`${cmd} sync-model ${filepath} ${model}`, cwd);
const output = await runAsync(`${cmd} sync-model ${filepath1} ${model}`, cwd);
t.true(output.includes(`Transcription:`));
t.true(output.includes(text));
t.true(output.includes(text1));
});

test(`should run sync recognize on a GCS file with model selection`, async t => {
const model = `video`;
const output = await runAsync(
`${cmd} sync-model-gcs gs://${bucketName}/${filename} ${model}`,
`${cmd} sync-model-gcs gs://${bucketName}/${filename1} ${model}`,
cwd
);
t.true(output.includes(`Transcription:`));
t.true(output.includes(text));
t.true(output.includes(text1));
});

test(`should run sync recognize with auto punctuation`, async t => {
const output = await runAsync(
`${cmd} sync-auto-punctuation ${filepath2}`,
cwd
);
t.true(output.includes(text2));
});

0 comments on commit e49eac0

Please sign in to comment.