Skip to content

Commit e5a62e5

Browse files
committed
made formatting methods avaliable outside of streaming interface
1 parent 0d527f7 commit e5a62e5

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
### v0.21.0
4+
* Made FormatStream formatting methods available outside of streaming interface
5+
36
### v0.20.4
47
* Fix looping error propagation when not using FormatStream in recognizeMicrophone
58

dist/watson-speech.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7034,7 +7034,7 @@ RecognizeStream.prototype.initialize = function() {
70347034
'content-type': 'audio/wav',
70357035
continuous: true,
70367036
inactivity_timeout: 30,
7037-
interim_results: true,
7037+
interim_results: false,
70387038
word_confidence: false,
70397039
timestamps: false,
70407040
max_alternatives: 1

speech-to-text/format-stream.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ var clone = require('clone');
66
var defaults = require('defaults');
77

88
/**
9-
* Applies some basic formating to transcriptions:
9+
* Applies some basic formatting to transcriptions:
1010
* - Capitalize the first word of each sentence
1111
* - Add a period to the end
1212
* - Fix any "cruft" in the transcription
1313
* - etc.
1414
*
15+
* May be used as either a Stream, or a standalone helper.
16+
*
1517
* @param {Object} opts
1618
* @param {String} opts.model - some models / languages need special handling
1719
* @param {String} [opts.hesitation='\u2026'] - what to put down for a "hesitation" event, defaults to an ellipsis (...)
@@ -27,7 +29,7 @@ function FormatStream(opts) {
2729
Transform.call(this, this.options);
2830

2931
this.isJaCn = ((this.options.model.substring(0,5) === 'ja-JP') || (this.options.model.substring(0,5) === 'zh-CN'));
30-
this._transform = this.options.objectMode ? this.formatResult : this.formatString;
32+
this._transform = this.options.objectMode ? this.transformObject : this.transformString;
3133
}
3234
util.inherits(FormatStream, Transform);
3335

@@ -82,25 +84,42 @@ FormatStream.prototype.period = function period(text) {
8284
return text + (this.isJaCn ? '。' : '. ');
8385
};
8486

85-
FormatStream.prototype.formatString = function(chunk, encoding, next) {
86-
this.push(this.period(this.capitalize(this.clean(chunk.toString()))));
87+
FormatStream.prototype.transformString = function(chunk, encoding, next) {
88+
this.push(this.formatString(chunk.toString()));
89+
next();
90+
};
91+
92+
FormatStream.prototype.transformObject = function formatResult(result, encoding, next) {
93+
this.push(this.formatResult(result));
8794
next();
8895
};
8996

97+
/**
98+
* Formats a single string result.
99+
*
100+
* May be used outside of Node.js streams
101+
*
102+
* @param {String} str - text to format
103+
* @param {bool} [isInterim=false] - set to true to prevent adding a period to the end of the sentence
104+
* @returns {String}
105+
*/
106+
FormatStream.prototype.formatString = function(str, isInterim) {
107+
str = this.capitalize(this.clean(str));
108+
return isInterim ? str : this.period(str);
109+
};
110+
90111
/**
91112
* Creates a new result with all transcriptions formatted
92113
*
114+
* May be used outside of Node.js streams
115+
*
93116
* @param {Object} result
94-
* @param {String} encoding
95-
* @param {Function} next
117+
* @returns {Object}
96118
*/
97-
FormatStream.prototype.formatResult = function formatResult(result, encoding, next) {
119+
FormatStream.prototype.formatResult = function formatResult(result) {
98120
result = clone(result);
99121
result.alternatives = result.alternatives.map(function(alt) {
100-
alt.transcript = this.capitalize(this.clean(alt.transcript));
101-
if (result.final) {
102-
alt.transcript = this.period(alt.transcript);
103-
}
122+
alt.transcript = this.formatString(alt.transcript, !result.final);
104123
if (alt.timestamps) {
105124
alt.timestamps = alt.timestamps.map(function(ts, i, arr) {
106125
// timestamps is an array of arrays, each sub-array is in the form ["word", startTime, endTime]'
@@ -119,8 +138,7 @@ FormatStream.prototype.formatResult = function formatResult(result, encoding, ne
119138
}
120139
return alt;
121140
}, this);
122-
this.push(result);
123-
next();
141+
return result;
124142
};
125143

126144
FormatStream.prototype.promise = require('./to-promise');

0 commit comments

Comments
 (0)