From d5c024818f485571f44f4f7ad6d313d922c12481 Mon Sep 17 00:00:00 2001 From: Daryush Laqab Date: Tue, 1 Aug 2017 14:56:06 -0700 Subject: [PATCH 1/3] Added enableWordTimeOffsets: true to async (file) Added enableWordTimeOffsets: false to sync samples --- speech/package-lock.json | 1 - speech/recognize.js | 13 +++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/speech/package-lock.json b/speech/package-lock.json index 09490bf3aa..c2985709f8 100644 --- a/speech/package-lock.json +++ b/speech/package-lock.json @@ -2,7 +2,6 @@ "name": "nodejs-docs-samples-speech", "version": "0.0.1", "lockfileVersion": 1, - "requires": true, "dependencies": { "@ava/babel-plugin-throws-helper": { "version": "2.0.0", 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); From aa5d5f9991a4ae61328f33ad9a12d63387540c4e Mon Sep 17 00:00:00 2001 From: Daryush Laqab Date: Wed, 2 Aug 2017 12:55:15 -0700 Subject: [PATCH 2/3] Added the missing unit test --- speech/system-test/recognize.test.js | 2 ++ 1 file changed, 2 insertions(+) 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) => { From 8f6816d209a496fad5177c428cb5b46fc1edeb71 Mon Sep 17 00:00:00 2001 From: Daryush Laqab Date: Wed, 2 Aug 2017 15:13:53 -0700 Subject: [PATCH 3/3] added "requires": true, back to package-lock.json --- speech/package-lock.json | 1 + 1 file changed, 1 insertion(+) diff --git a/speech/package-lock.json b/speech/package-lock.json index c2985709f8..09490bf3aa 100644 --- a/speech/package-lock.json +++ b/speech/package-lock.json @@ -2,6 +2,7 @@ "name": "nodejs-docs-samples-speech", "version": "0.0.1", "lockfileVersion": 1, + "requires": true, "dependencies": { "@ava/babel-plugin-throws-helper": { "version": "2.0.0",