Skip to content

Commit 2b5c336

Browse files
committed
Improve hesitation formatting at the end of a sentence.
Fixes #13
1 parent a0d7e48 commit 2b5c336

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

speech-to-text/format-stream.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function FormatStream(opts) {
3131
}
3232
util.inherits(FormatStream, Transform);
3333

34-
var reHesitation = /%HESITATION\s/g; // when the service detects a "hesitation" pause, it literally puts the string "%HESITATION" into the transcription
34+
var reHesitation = /%HESITATION/g; // when the service detects a "hesitation" pause, it literally puts the string "%HESITATION" into the transcription
3535
var reRepeatedCharacter = /([a-z])\1{2,}/ig; // detect the same character repeated three or more times and remove it
3636
var reDUnderscoreWords = /D_[^\s]+/g; // replace D_(anything)
3737

@@ -75,6 +75,10 @@ FormatStream.prototype.period = function period(text) {
7575
if (!text) {
7676
return ' ';
7777
}
78+
// just add a space if the sentence ends in an ellipse
79+
if (this.options.hesitation && text.substr(-1) === this.options.hesitation) {
80+
return text + ' ';
81+
}
7882
return text + (this.isJaCn ? '。' : '. ');
7983
};
8084

test/format-stream-spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ describe('FormatStream', function() {
8181
stream.write(source);
8282
});
8383

84+
// https://github.com/watson-developer-cloud/speech-javascript-sdk/issues/13
85+
it('should handle %HESITATIONs at the end of a sentence (and not add a period)', function(done) {
86+
var stream = new FormatStream();
87+
stream.setEncoding('utf8');
88+
var source = '%HESITATION asdf %HESITATION ';
89+
var expected = '… asdf … ';
90+
stream.on('data', function(actual) {
91+
assert.equal(actual, expected);
92+
done();
93+
});
94+
stream.on('error', done);
95+
stream.write(source);
96+
});
97+
8498
/*
8599
{ results:
86100
[ { alternatives:

0 commit comments

Comments
 (0)