@@ -6,12 +6,14 @@ var clone = require('clone');
6
6
var defaults = require ( 'defaults' ) ;
7
7
8
8
/**
9
- * Applies some basic formating to transcriptions:
9
+ * Applies some basic formatting to transcriptions:
10
10
* - Capitalize the first word of each sentence
11
11
* - Add a period to the end
12
12
* - Fix any "cruft" in the transcription
13
13
* - etc.
14
14
*
15
+ * May be used as either a Stream, or a standalone helper.
16
+ *
15
17
* @param {Object } opts
16
18
* @param {String } opts.model - some models / languages need special handling
17
19
* @param {String } [opts.hesitation='\u2026'] - what to put down for a "hesitation" event, defaults to an ellipsis (...)
@@ -27,7 +29,7 @@ function FormatStream(opts) {
27
29
Transform . call ( this , this . options ) ;
28
30
29
31
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 ;
31
33
}
32
34
util . inherits ( FormatStream , Transform ) ;
33
35
@@ -82,25 +84,42 @@ FormatStream.prototype.period = function period(text) {
82
84
return text + ( this . isJaCn ? '。' : '. ' ) ;
83
85
} ;
84
86
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 ) ) ;
87
94
next ( ) ;
88
95
} ;
89
96
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
+
90
111
/**
91
112
* Creates a new result with all transcriptions formatted
92
113
*
114
+ * May be used outside of Node.js streams
115
+ *
93
116
* @param {Object } result
94
- * @param {String } encoding
95
- * @param {Function } next
117
+ * @returns {Object }
96
118
*/
97
- FormatStream . prototype . formatResult = function formatResult ( result , encoding , next ) {
119
+ FormatStream . prototype . formatResult = function formatResult ( result ) {
98
120
result = clone ( result ) ;
99
121
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 ) ;
104
123
if ( alt . timestamps ) {
105
124
alt . timestamps = alt . timestamps . map ( function ( ts , i , arr ) {
106
125
// 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
119
138
}
120
139
return alt ;
121
140
} , this ) ;
122
- this . push ( result ) ;
123
- next ( ) ;
141
+ return result ;
124
142
} ;
125
143
126
144
FormatStream . prototype . promise = require ( './to-promise' ) ;
0 commit comments