Skip to content

Commit b4fa58a

Browse files
committed
support for keywords, keywords_threshold, and word_alternatives_threshold
1 parent cc6928c commit b4fa58a

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

services/speech_to_text/v1.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var util = require('util');
3131
var WebSocketClient = require('websocket').client;
3232
var pkg = require('../../package.json');
3333

34+
const PARAMS_ALLOWED = ['continuous', 'max_alternatives', 'timestamps', 'word_confidence', 'inactivity_timeout',
35+
'model', 'content-type', 'interim_results', 'keywords', 'keywords_threshold', 'word_alternatives_threshold' ];
36+
3437
function formatChunk(chunk) {
3538
// Convert the string into an array
3639
var result = chunk;
@@ -394,8 +397,7 @@ function RecognizeStream(options){
394397
'content-type': 'audio/wav', // todo: try to determine content-type from the file extension if available
395398
'continuous': false,
396399
'interim_results': true
397-
}, pick(options, ['continuous', 'max_alternatives', 'timestamps',
398-
'word_confidence', 'inactivity_timeout', 'content-type', 'interim_results']));
400+
}, pick(options, [PARAMS_ALLOWED]));
399401

400402
var closingMessage = {action: 'stop'};
401403

test/test.speech_to_text.v1.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('speech_to_text', function() {
3939
session_id: 'foo',
4040
new_session_uri: '#',
4141
recognize: '#',
42-
observe_result: '#',
42+
observe_result: '#'
4343
},
4444
new_session_with_cookie = extend({}, new_session, {cookie_session: 'foobar'});
4545

@@ -226,6 +226,75 @@ describe('speech_to_text', function() {
226226
});
227227
});
228228

229+
describe('recognizeStream()', function() {
230+
var path = '/v1/sessions/foo/recognize',
231+
payload = {
232+
session_id: 'foo',
233+
cookie_session: 'foobar',
234+
content_type: 'audio/l16; rate=41100'
235+
},
236+
service_response = {
237+
result: [{
238+
alternative: [{
239+
transcript: 'one two three'
240+
}],
241+
'final': true
242+
}],
243+
result_index: 0
244+
};
245+
246+
var options = {
247+
content_type: 'audio/l16;rate=41100',
248+
"continuous": true,
249+
"timestamps":true,
250+
"inactivity_timeout": -1,
251+
"max_alternatives": 1,
252+
"interim_results": false,
253+
"keywords": ['one', 'Three'],
254+
"keywords_threshold": 0.9,
255+
"word_alternatives_threshold": 0.25
256+
};
257+
var recognizeStream = speech_to_text.createRecognizeStream(options);
258+
var DEBUG = fs.createReadStream(__dirname + '/resources/audio.wav').pipe(recognizeStream);
259+
recognizeStream.setEncoding('utf8');
260+
261+
it('should have expected _events', function(done) {
262+
assert.equal(true, recognizeStream.hasOwnProperty('_events'));
263+
assert.equal(true, recognizeStream._events.hasOwnProperty('connect'));
264+
assert.equal(true, recognizeStream._events.hasOwnProperty('connection-close'));
265+
assert.equal(true, recognizeStream._events.hasOwnProperty('results'));
266+
assert.equal(true, recognizeStream._events.hasOwnProperty('error'));
267+
assert.equal(true, recognizeStream._events.hasOwnProperty('finish'));
268+
});
269+
270+
recognizeStream.on('connect', function(socket){
271+
it('should have a socket connection with a correct config', function(done){
272+
assert.notStrictEqual(socket, socket.config, socket.config.fragmentOutgoingMessages);
273+
assert.notStrictEqual(socket, socket.config, socket.config.fragmentOutgoingMessages);
274+
});
275+
});
276+
277+
recognizeStream.on('error', function(err){
278+
it('should throw ECONNRESET with bad credentials', function(done){
279+
assert.equal(err.code, 'ECONNRESET');
280+
assert.equal(err.errno, 'ECONNRESET');
281+
})
282+
});
283+
284+
recognizeStream.on('connection-close', function(reasonCode, description){
285+
console.log(139, reasonCode);
286+
console.log(140, description);
287+
});
288+
289+
recognizeStream.on('results', function(obj){
290+
console.log(JSON.stringify(obj));
291+
it('should generate a valid response', function(done) {
292+
assert.equal(obj, service_response);
293+
});
294+
});
295+
});
296+
297+
229298
describe('recognizeLive()', function() {
230299
var path = '/v1/sessions/foo/recognize',
231300
payload = {

0 commit comments

Comments
 (0)