Skip to content

Commit efb598a

Browse files
committed
fixing a bug where timing stream would loose the result_index
1 parent 1b13c36 commit efb598a

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

speech-to-text/timing-stream.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function getEnd(msg) {
182182

183183
TimingStream.prototype.tickSpeakerLables = function tickSpeakerLabels() {
184184
clearTimeout(this.nextSpeakerLabelsTick);
185-
if (getEnd(this.speakerLabels[0]) <= this.cutoff()) {
185+
while (this.speakerLabels.length && getEnd(this.speakerLabels[0]) <= this.cutoff()) {
186186
this.push(this.speakerLabels.shift());
187187
}
188188
if (this.speakerLabels.length) {
@@ -253,7 +253,7 @@ TimingStream.prototype.handleResult = function handleResult(data) {
253253
}
254254

255255
// http://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#SpeechRecognitionEvent
256-
var index = data.results[0].result_index;
256+
var index = data.result_index;
257257

258258
// process each result individually
259259
data.results.forEach(function(result) {
@@ -268,9 +268,12 @@ TimingStream.prototype.handleResult = function handleResult(data) {
268268
}
269269

270270
// in case this data object had multiple results in it
271-
var newData = clone(data);
272-
data.results = [result];
273-
data.result_index = index;
271+
var newData = {
272+
results: [result],
273+
result_index: index
274+
};
275+
276+
index++;
274277

275278
if (result.final) {
276279
// then add it to the final results array
@@ -281,7 +284,6 @@ TimingStream.prototype.handleResult = function handleResult(data) {
281284
this.interim.push(newData);
282285
}
283286

284-
index++;
285287
}, this);
286288

287289
this.tick();

test/timing-stream-spec.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var clone = require('clone');
77
var TimingStream = require('../speech-to-text/timing-stream.js');
88

99
var results = require('./resources/results.json');
10-
var messageStream = require('./resources/car_loan_stream.json');
10+
var messages = require('./resources/car_loan_stream.json');
1111

1212
describe('TimingStream', function() {
1313

@@ -171,22 +171,19 @@ describe('TimingStream', function() {
171171
});
172172
stream.on('error', done);
173173

174-
messageStream.forEach(function(msg) {
174+
messages.forEach(function(msg) {
175175
if (msg.results) {
176176
stream.write(msg);
177177
}
178178
});
179+
stream.end();
179180

180-
var numTicks = 37.26 * 1000;
181-
182-
for (var i = 0; i < numTicks; i++) {
183-
clock.tick(1);
184-
}
181+
clock.tick(37.26 * 1000);
185182

186183
nextTick(function() { // write is always async (?)
187-
184+
assert(actual.length);
188185
actual.reduce(function(lastIndex, msg) {
189-
assert.equal(msg.result_index, lastIndex);
186+
assert.equal(msg.result_index, lastIndex, "wrong index on result, expecting " + lastIndex + " got " + JSON.stringify(msg, null, 2));
190187
// index should always increment after a final message
191188
return (msg.results[0].final) ? lastIndex + 1 : lastIndex;
192189
}, 0);
@@ -203,15 +200,12 @@ describe('TimingStream', function() {
203200
});
204201
stream.on('error', done);
205202

206-
messageStream.forEach(function(msg) {
203+
messages.forEach(function(msg) {
207204
stream.write(msg);
208205
});
206+
stream.end();
209207

210-
var numTicks = 37.26 * 1000;
211-
212-
for (var i = 0; i < numTicks; i++) {
213-
clock.tick(1);
214-
}
208+
clock.tick(37.26 * 1000);
215209

216210
nextTick(function() { // write is always async (?)
217211

@@ -245,17 +239,13 @@ describe('TimingStream', function() {
245239
});
246240
stream.on('error', done);
247241

248-
messageStream.forEach(function(msg) {
242+
messages.forEach(function(msg) {
249243
if (msg.results) {
250244
stream.write(msg);
251245
}
252246
});
253247

254-
var numTicks = 37.26 * 1000;
255-
256-
for (var i = 0; i < numTicks; i++) {
257-
clock.tick(1);
258-
}
248+
clock.tick(37.26 * 1000);
259249

260250
nextTick(function() { // write is always async (?)
261251

0 commit comments

Comments
 (0)