From b4fa58a13761c5430c49c41b0568e9a93966e056 Mon Sep 17 00:00:00 2001 From: Monte Hayward Date: Mon, 21 Dec 2015 15:02:53 -0600 Subject: [PATCH 1/4] support for keywords, keywords_threshold, and word_alternatives_threshold --- services/speech_to_text/v1.js | 6 ++- test/test.speech_to_text.v1.js | 71 +++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/services/speech_to_text/v1.js b/services/speech_to_text/v1.js index 67d375308d..5b5e6b9c8d 100644 --- a/services/speech_to_text/v1.js +++ b/services/speech_to_text/v1.js @@ -31,6 +31,9 @@ var util = require('util'); var WebSocketClient = require('websocket').client; var pkg = require('../../package.json'); +const PARAMS_ALLOWED = ['continuous', 'max_alternatives', 'timestamps', 'word_confidence', 'inactivity_timeout', + 'model', 'content-type', 'interim_results', 'keywords', 'keywords_threshold', 'word_alternatives_threshold' ]; + function formatChunk(chunk) { // Convert the string into an array var result = chunk; @@ -394,8 +397,7 @@ function RecognizeStream(options){ 'content-type': 'audio/wav', // todo: try to determine content-type from the file extension if available 'continuous': false, 'interim_results': true - }, pick(options, ['continuous', 'max_alternatives', 'timestamps', - 'word_confidence', 'inactivity_timeout', 'content-type', 'interim_results'])); + }, pick(options, [PARAMS_ALLOWED])); var closingMessage = {action: 'stop'}; diff --git a/test/test.speech_to_text.v1.js b/test/test.speech_to_text.v1.js index 5b42a98896..5991d06a21 100644 --- a/test/test.speech_to_text.v1.js +++ b/test/test.speech_to_text.v1.js @@ -39,7 +39,7 @@ describe('speech_to_text', function() { session_id: 'foo', new_session_uri: '#', recognize: '#', - observe_result: '#', + observe_result: '#' }, new_session_with_cookie = extend({}, new_session, {cookie_session: 'foobar'}); @@ -226,6 +226,75 @@ describe('speech_to_text', function() { }); }); + describe('recognizeStream()', function() { + var path = '/v1/sessions/foo/recognize', + payload = { + session_id: 'foo', + cookie_session: 'foobar', + content_type: 'audio/l16; rate=41100' + }, + service_response = { + result: [{ + alternative: [{ + transcript: 'one two three' + }], + 'final': true + }], + result_index: 0 + }; + + var options = { + content_type: 'audio/l16;rate=41100', + "continuous": true, + "timestamps":true, + "inactivity_timeout": -1, + "max_alternatives": 1, + "interim_results": false, + "keywords": ['one', 'Three'], + "keywords_threshold": 0.9, + "word_alternatives_threshold": 0.25 + }; + var recognizeStream = speech_to_text.createRecognizeStream(options); + var DEBUG = fs.createReadStream(__dirname + '/resources/audio.wav').pipe(recognizeStream); + recognizeStream.setEncoding('utf8'); + + it('should have expected _events', function(done) { + assert.equal(true, recognizeStream.hasOwnProperty('_events')); + assert.equal(true, recognizeStream._events.hasOwnProperty('connect')); + assert.equal(true, recognizeStream._events.hasOwnProperty('connection-close')); + assert.equal(true, recognizeStream._events.hasOwnProperty('results')); + assert.equal(true, recognizeStream._events.hasOwnProperty('error')); + assert.equal(true, recognizeStream._events.hasOwnProperty('finish')); + }); + + recognizeStream.on('connect', function(socket){ + it('should have a socket connection with a correct config', function(done){ + assert.notStrictEqual(socket, socket.config, socket.config.fragmentOutgoingMessages); + assert.notStrictEqual(socket, socket.config, socket.config.fragmentOutgoingMessages); + }); + }); + + recognizeStream.on('error', function(err){ + it('should throw ECONNRESET with bad credentials', function(done){ + assert.equal(err.code, 'ECONNRESET'); + assert.equal(err.errno, 'ECONNRESET'); + }) + }); + + recognizeStream.on('connection-close', function(reasonCode, description){ + console.log(139, reasonCode); + console.log(140, description); + }); + + recognizeStream.on('results', function(obj){ + console.log(JSON.stringify(obj)); + it('should generate a valid response', function(done) { + assert.equal(obj, service_response); + }); + }); + }); + + describe('recognizeLive()', function() { var path = '/v1/sessions/foo/recognize', payload = { From 8657c78edea741bc02ba227e84574ced47ee361a Mon Sep 17 00:00:00 2001 From: Monte Hayward Date: Mon, 21 Dec 2015 15:18:18 -0600 Subject: [PATCH 2/4] [speech-to-text] add keywords, keywords_threshold, word_alternatives_threshold #189 --- services/speech_to_text/v1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/speech_to_text/v1.js b/services/speech_to_text/v1.js index 5b5e6b9c8d..fb89b0475a 100644 --- a/services/speech_to_text/v1.js +++ b/services/speech_to_text/v1.js @@ -31,7 +31,7 @@ var util = require('util'); var WebSocketClient = require('websocket').client; var pkg = require('../../package.json'); -const PARAMS_ALLOWED = ['continuous', 'max_alternatives', 'timestamps', 'word_confidence', 'inactivity_timeout', +var PARAMS_ALLOWED = ['continuous', 'max_alternatives', 'timestamps', 'word_confidence', 'inactivity_timeout', 'model', 'content-type', 'interim_results', 'keywords', 'keywords_threshold', 'word_alternatives_threshold' ]; function formatChunk(chunk) { From e43359f6023f16b886c92c3147fee6916926cac5 Mon Sep 17 00:00:00 2001 From: Monte Hayward Date: Mon, 21 Dec 2015 15:25:03 -0600 Subject: [PATCH 3/4] [speech-to-text] keywords, keywords_threshold, word_alternatives_threshold #189 --- test/test.speech_to_text.v1.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/test.speech_to_text.v1.js b/test/test.speech_to_text.v1.js index 5991d06a21..dd09cdadab 100644 --- a/test/test.speech_to_text.v1.js +++ b/test/test.speech_to_text.v1.js @@ -265,12 +265,14 @@ describe('speech_to_text', function() { assert.equal(true, recognizeStream._events.hasOwnProperty('results')); assert.equal(true, recognizeStream._events.hasOwnProperty('error')); assert.equal(true, recognizeStream._events.hasOwnProperty('finish')); + done(); }); recognizeStream.on('connect', function(socket){ it('should have a socket connection with a correct config', function(done){ assert.notStrictEqual(socket, socket.config, socket.config.fragmentOutgoingMessages); assert.notStrictEqual(socket, socket.config, socket.config.fragmentOutgoingMessages); + done(); }); }); @@ -278,18 +280,15 @@ describe('speech_to_text', function() { it('should throw ECONNRESET with bad credentials', function(done){ assert.equal(err.code, 'ECONNRESET'); assert.equal(err.errno, 'ECONNRESET'); - }) - }); - - recognizeStream.on('connection-close', function(reasonCode, description){ - console.log(139, reasonCode); - console.log(140, description); + done(); + }); }); recognizeStream.on('results', function(obj){ console.log(JSON.stringify(obj)); it('should generate a valid response', function(done) { assert.equal(obj, service_response); + done(); }); }); }); From e0dca8d26ab91351e69ea2b3906749729fba9d22 Mon Sep 17 00:00:00 2001 From: Monte Hayward Date: Mon, 21 Dec 2015 15:29:58 -0600 Subject: [PATCH 4/4] [speech-to-text] keywords, keywords_threshold, word_alternatives_threshold #189 --- test/test.speech_to_text.v1.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test.speech_to_text.v1.js b/test/test.speech_to_text.v1.js index dd09cdadab..39aa20000c 100644 --- a/test/test.speech_to_text.v1.js +++ b/test/test.speech_to_text.v1.js @@ -261,10 +261,11 @@ describe('speech_to_text', function() { it('should have expected _events', function(done) { assert.equal(true, recognizeStream.hasOwnProperty('_events')); assert.equal(true, recognizeStream._events.hasOwnProperty('connect')); - assert.equal(true, recognizeStream._events.hasOwnProperty('connection-close')); + assert.equal(true, recognizeStream._events.hasOwnProperty('end')); assert.equal(true, recognizeStream._events.hasOwnProperty('results')); assert.equal(true, recognizeStream._events.hasOwnProperty('error')); assert.equal(true, recognizeStream._events.hasOwnProperty('finish')); + assert.equal(true, recognizeStream._events.hasOwnProperty('listening')); done(); });