Skip to content

Commit

Permalink
Added enableWordTimeOffsets: true to async (file) (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlaqab authored and Ace Nassri committed Nov 17, 2022
1 parent 6846aaf commit a41fc35
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions speech/recognize.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function syncRecognize (filename, encoding, sampleRateHertz, languageCode) {
// const languageCode = 'en-US';

const config = {
enableWordTimeOffsets: false,
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode
Expand Down Expand Up @@ -91,6 +92,7 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
// const languageCode = 'en-US';

const config = {
enableWordTimeOffsets: false,
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode
Expand Down Expand Up @@ -138,6 +140,7 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
// const languageCode = 'en-US';

const config = {
enableWordTimeOffsets: true,
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode
Expand All @@ -162,6 +165,16 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
.then((results) => {
const transcription = results[0].results[0].alternatives[0].transcript;
console.log(`Transcription: ${transcription}`);
results[0].results[0].alternatives[0].words.forEach((wordInfo) => {
// NOTE: If you have a time offset exceeding 2^32 seconds, use the
// wordInfo.{x}Time.seconds.high to calculate seconds.
let startSecs = `${wordInfo.startTime.seconds.low}` + `.` +
(wordInfo.startTime.nanos / 100000000);
let endSecs = `${wordInfo.endTime.seconds.low}` + `.` +
(wordInfo.endTime.nanos / 100000000);
console.log(`Word: ${wordInfo.word}`);
console.log(`\t ${startSecs} secs - ${endSecs} secs`);
});
})
.catch((err) => {
console.error('ERROR:', err);
Expand Down
2 changes: 2 additions & 0 deletions speech/system-test/recognize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ test(`should run sync recognize on a GCS file`, async (t) => {
test(`should run async recognize on a local file`, async (t) => {
const output = await runAsync(`${cmd} async ${filepath}`, cwd);
t.true(output.includes(`Transcription: ${text}`));
// Check for word time offsets
t.true(new RegExp(`\\d+\\.\\d+ secs - \\d+\\.\\d+ secs`).test(output));
});

test(`should run async recognize on a GCS file`, async (t) => {
Expand Down

0 comments on commit a41fc35

Please sign in to comment.