@@ -6,12 +6,14 @@ var clone = require('clone');
66var 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}
3234util . 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
126144FormatStream . prototype . promise = require ( './to-promise' ) ;
0 commit comments