Skip to content

Commit a41fc35

Browse files
dlaqabAce Nassri
authored andcommitted
Added enableWordTimeOffsets: true to async (file) (#440)
1 parent 6846aaf commit a41fc35

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

speech/recognize.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function syncRecognize (filename, encoding, sampleRateHertz, languageCode) {
4545
// const languageCode = 'en-US';
4646

4747
const config = {
48+
enableWordTimeOffsets: false,
4849
encoding: encoding,
4950
sampleRateHertz: sampleRateHertz,
5051
languageCode: languageCode
@@ -91,6 +92,7 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
9192
// const languageCode = 'en-US';
9293

9394
const config = {
95+
enableWordTimeOffsets: false,
9496
encoding: encoding,
9597
sampleRateHertz: sampleRateHertz,
9698
languageCode: languageCode
@@ -138,6 +140,7 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
138140
// const languageCode = 'en-US';
139141

140142
const config = {
143+
enableWordTimeOffsets: true,
141144
encoding: encoding,
142145
sampleRateHertz: sampleRateHertz,
143146
languageCode: languageCode
@@ -162,6 +165,16 @@ function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
162165
.then((results) => {
163166
const transcription = results[0].results[0].alternatives[0].transcript;
164167
console.log(`Transcription: ${transcription}`);
168+
results[0].results[0].alternatives[0].words.forEach((wordInfo) => {
169+
// NOTE: If you have a time offset exceeding 2^32 seconds, use the
170+
// wordInfo.{x}Time.seconds.high to calculate seconds.
171+
let startSecs = `${wordInfo.startTime.seconds.low}` + `.` +
172+
(wordInfo.startTime.nanos / 100000000);
173+
let endSecs = `${wordInfo.endTime.seconds.low}` + `.` +
174+
(wordInfo.endTime.nanos / 100000000);
175+
console.log(`Word: ${wordInfo.word}`);
176+
console.log(`\t ${startSecs} secs - ${endSecs} secs`);
177+
});
165178
})
166179
.catch((err) => {
167180
console.error('ERROR:', err);

speech/system-test/recognize.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ test(`should run sync recognize on a GCS file`, async (t) => {
5656
test(`should run async recognize on a local file`, async (t) => {
5757
const output = await runAsync(`${cmd} async ${filepath}`, cwd);
5858
t.true(output.includes(`Transcription: ${text}`));
59+
// Check for word time offsets
60+
t.true(new RegExp(`\\d+\\.\\d+ secs - \\d+\\.\\d+ secs`).test(output));
5961
});
6062

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

0 commit comments

Comments
 (0)