From a41fc351c875b243777983ee618ad6188556e9e5 Mon Sep 17 00:00:00 2001 From: Daryush Laqab Date: Thu, 3 Aug 2017 12:33:18 -0700 Subject: [PATCH] Added enableWordTimeOffsets: true to async (file) (#440) --- speech/recognize.js | 13 +++++++++++++ speech/system-test/recognize.test.js | 2 ++ 2 files changed, 15 insertions(+) diff --git a/speech/recognize.js b/speech/recognize.js index 7edfc1c071..817d5534b4 100644 --- a/speech/recognize.js +++ b/speech/recognize.js @@ -45,6 +45,7 @@ function syncRecognize (filename, encoding, sampleRateHertz, languageCode) { // const languageCode = 'en-US'; const config = { + enableWordTimeOffsets: false, encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode @@ -91,6 +92,7 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) { // const languageCode = 'en-US'; const config = { + enableWordTimeOffsets: false, encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode @@ -138,6 +140,7 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) { // const languageCode = 'en-US'; const config = { + enableWordTimeOffsets: true, encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode @@ -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); diff --git a/speech/system-test/recognize.test.js b/speech/system-test/recognize.test.js index 9c02d6a5f9..72208f429a 100644 --- a/speech/system-test/recognize.test.js +++ b/speech/system-test/recognize.test.js @@ -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) => {