Skip to content

Commit

Permalink
Require languageCode to be explicitly specified. (googleapis#2186)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored and stephenplusplus committed Apr 4, 2017
1 parent d765148 commit 7ed4291
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"unit-test": "mocha --timeout 5000 --bail packages/*/test/*.js",
"snippet-test": "mocha --timeout 5000 --bail test/docs.js",
"system-test": "mocha packages/*/system-test/*.js --no-timeouts --bail",
"cover": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -x 'packages/*/src/*{/*,/**/*}.js' -- --no-timeouts --bail packages/*/test/*.js -R spec",
"cover-html": "istanbul cover node_modules/mocha/bin/_mocha --report html -x 'packages/*/src/*{/*,/**/*}.js' -- --no-timeouts --bail packages/*/test/*.js -R spec",
"cover": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -x 'packages/*/src/*{/*,/**/*}.js' -- --no-timeouts packages/*/test/*.js -R spec",
"cover-html": "istanbul cover node_modules/mocha/bin/_mocha --report html -x 'packages/*/src/*{/*,/**/*}.js' -- --no-timeouts packages/*/test/*.js -R spec",
"coveralls": "npm run cover && cat ./.coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"license": "Apache-2.0",
Expand Down
46 changes: 35 additions & 11 deletions packages/speech/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ Speech.formatResults_ = function(resultSets, verboseMode) {
*
* @param {object} config - A `StreamingRecognitionConfig` object. See
* [`StreamingRecognitionConfig`](https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#google.cloud.speech.v1beta1.StreamingRecognitionConfig).
* @param {string} config.languageCode - The language of the supplied audio as
* [BCP-47 language tag](http://bit.ly/1ZHeENX). Example: 'en-US'.
* @param {number=} config.timeout - In seconds, the amount of time before the
* underlying API request times out. The default value, `190`, is sufficient
* for audio input of 60 seconds or less. If your input is longer, consider
Expand All @@ -390,6 +392,7 @@ Speech.formatResults_ = function(resultSets, verboseMode) {
* var request = {
* config: {
* encoding: 'LINEAR16',
* languageCode: 'en-US',
* sampleRateHertz: 16000
* },
* singleUtterance: false,
Expand All @@ -410,6 +413,7 @@ Speech.formatResults_ = function(resultSets, verboseMode) {
* var request = {
* config: {
* encoding: 'LINEAR16',
* languageCode: 'en-US',
* sampleRateHertz: 16000
* },
* singleUtterance: false,
Expand All @@ -432,11 +436,19 @@ Speech.prototype.createRecognizeStream = function(config) {
throw new Error('A recognize request requires a configuration object.');
}

config = extend(true, {
config: {
languageCode: 'en-US'
}
}, config);
// As of Speech v1, a language code is required; throw an exception if we
// did not receive one.
//
// This is expected within a nested config, but in the interest of user
// sanity, accept it in the outer config object.
config.config = config.config || {};
if (config.languageCode) {
config.config.languageCode = config.languageCode;
delete config.languageCode;
}
if (is.undefined(config.config.languageCode)) {
throw new Error('A `languageCode` is required in the config object.');
}

var verboseMode = config.verbose === true;
delete config.verbose;
Expand Down Expand Up @@ -524,6 +536,8 @@ Speech.prototype.operation = function(name) {
* object.
* @param {object} config - A `RecognitionConfig` object. See
* [`RecognitionConfig`](https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#google.cloud.speech.v1beta1.RecognitionConfig).
* @param {string} config.languageCode - The language of the supplied audio as
* [BCP-47 language tag](http://bit.ly/1ZHeENX). Example: 'en-US'.
* @param {boolean=} config.verbose - Enable verbose mode for a more detailed
* response. See the examples below. Default: `false`.
* @param {function} callback - The callback function.
Expand All @@ -540,6 +554,7 @@ Speech.prototype.operation = function(name) {
* @example
* var config = {
* encoding: 'LINEAR16',
* languageCode: 'en-US',
* sampleRateHertz: 16000
* };
*
Expand Down Expand Up @@ -582,6 +597,7 @@ Speech.prototype.operation = function(name) {
* //-
* var config = {
* encoding: 'LINEAR16',
* languageCode: 'en-US',
* sampleRateHertz: 16000,
* verbose: true
* };
Expand Down Expand Up @@ -620,9 +636,11 @@ Speech.prototype.recognize = function(file, config, callback) {
throw new Error('A recognize request requires a configuration object.');
}

config = extend({
languageCode: 'en-US'
}, config);
// As of Speech v1, a language code is required; throw an exception if we
// did not receive one.
if (is.undefined(config.languageCode)) {
throw new Error('A `languageCode` is required in the config object.');
}

if (!config.encoding) {
config.encoding = Speech.detectEncoding_(file);
Expand Down Expand Up @@ -674,6 +692,8 @@ Speech.prototype.recognize = function(file, config, callback) {
* [`RecognitionConfig`](https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#google.cloud.speech.v1beta1.RecognitionConfig).
* @param {boolean=} config.verbose - Enable verbose mode for a more detailed
* response. See the examples below. Default: `false`.
* @param {string} config.languageCode - The language of the supplied audio as
* [BCP-47 language tag](http://bit.ly/1ZHeENX). Example: 'en-US'.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:speech/operation} callback.operation - An operation object
Expand All @@ -683,6 +703,7 @@ Speech.prototype.recognize = function(file, config, callback) {
* @example
* var config = {
* encoding: 'LINEAR16',
* languageCode: 'en-US',
* sampleRateHertz: 16000
* };
*
Expand Down Expand Up @@ -732,6 +753,7 @@ Speech.prototype.recognize = function(file, config, callback) {
* //-
* var config = {
* encoding: 'LINEAR16',
* languageCode: 'en-US',
* sampleRateHertz: 16000,
* verbose: true
* };
Expand Down Expand Up @@ -764,9 +786,11 @@ Speech.prototype.recognize = function(file, config, callback) {
Speech.prototype.startRecognition = function(file, config, callback) {
var self = this;

config = extend({
languageCode: 'en-US'
}, config);
// As of Speech v1, a language code is required; throw an exception if we
// did not receive one.
if (is.undefined(config.languageCode)) {
throw new Error('A `languageCode` is required in the config object.');
}

if (!config.encoding) {
config.encoding = Speech.detectEncoding_(file);
Expand Down
6 changes: 4 additions & 2 deletions packages/speech/system-test/speech.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('Speech', function() {

var OPTIONS = {
encoding: 'LINEAR16',
languageCode: 'en-US',
sampleRateHertz: 16000
};

Expand Down Expand Up @@ -109,7 +110,7 @@ describe('Speech', function() {
assert.ifError(err);

speech.recognize({
content: audioFile
content: audioFile,
}, OPTIONS, assertSimplifiedResponse(done));
});
});
Expand All @@ -119,14 +120,15 @@ describe('Speech', function() {
assert.ifError(err);

speech.recognize({
content: audioFile
content: audioFile,
}, OPTIONS_VERBOSE, assertVerboseResponse(done));
});
});

it('recognizes speech from local file', function(done) {
speech.recognize(AUDIO_FILES.bridge.path, {
// encoding should be automatically detected
languageCode: 'en-US',
sampleRateHertz: 16000
}, assertSimplifiedResponse(done));
});
Expand Down
48 changes: 39 additions & 9 deletions packages/speech/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ describe('Speech', function() {
});

describe('createRecognizeStream', function() {
var CONFIG = {};
var CONFIG = {languageCode: 'en-US'};
var stream;
var requestStream;

Expand All @@ -474,6 +474,12 @@ describe('Speech', function() {
}, /A recognize request requires a configuration object\./);
});

it('should throw if a language code is not provided', function() {
assert.throws(function() {
speech.createRecognizeStream({});
}, /languageCode/);
});

it('should make the correct request once writing started', function(done) {
speech.api.Speech = {
streamingRecognize: function() {
Expand Down Expand Up @@ -603,6 +609,7 @@ describe('Speech', function() {

it('should format results from the API in verbose mode', function(done) {
var stream = speech.createRecognizeStream({
languageCode: 'en-US',
verbose: true
});

Expand All @@ -626,6 +633,7 @@ describe('Speech', function() {

it('should delete verbose option from request object', function(done) {
var stream = speech.createRecognizeStream({
languageCode: 'en-US',
verbose: true
});

Expand Down Expand Up @@ -662,6 +670,7 @@ describe('Speech', function() {
};

var stream = speech.createRecognizeStream({
languageCode: 'en-US',
timeout: timeout
});

Expand All @@ -683,6 +692,7 @@ describe('Speech', function() {
};

var stream = speech.createRecognizeStream({
languageCode: 'en-US',
timeout: 90
});

Expand Down Expand Up @@ -738,7 +748,10 @@ describe('Speech', function() {
describe('recognize', function() {
var FILE = {};
var FOUND_FILE = {};
var CONFIG = { a: 'b' };
var CONFIG = {
a: 'b',
languageCode: 'en-US',
};
var DETECTED_ENCODING = 'LINEAR16';

beforeEach(function() {
Expand Down Expand Up @@ -788,8 +801,14 @@ describe('Speech', function() {
speech.recognize(FILE, CONFIG, assert.ifError);
});

it('should fail if no language code is set', function() {
assert.throws(function() {
speech.recognize(FILE, {});
}, /languageCode/);
});

it('should allow setting a languageCode', function(done) {
var languageCode = 'uk';
var languageCode = 'en-GB';

var config = {
languageCode: languageCode
Expand All @@ -807,7 +826,8 @@ describe('Speech', function() {

it('should respect the provided encoding', function(done) {
var config = {
encoding: 'LINEAR32'
encoding: 'LINEAR32',
languageCode: 'en-US'
};

Speech.detectEncoding_ = function() {
Expand Down Expand Up @@ -839,7 +859,7 @@ describe('Speech', function() {
}
};

speech.recognize(FILE, {}, assert.ifError);
speech.recognize(FILE, {languageCode: 'en-US'}, assert.ifError);
});

it('should return an error from findFile_', function(done) {
Expand Down Expand Up @@ -956,7 +976,10 @@ describe('Speech', function() {
describe('startRecognition', function() {
var FILE = {};
var FOUND_FILE = {};
var CONFIG = { a: 'b' };
var CONFIG = {
a: 'b',
languageCode: 'en-US'
};
var DETECTED_ENCODING = 'LINEAR16';

beforeEach(function() {
Expand Down Expand Up @@ -1000,8 +1023,14 @@ describe('Speech', function() {
speech.startRecognition(FILE, CONFIG, assert.ifError);
});

it('should error if no language code is given', function() {
assert.throws(function() {
speech.startRecognition(FILE, {});
}, /languageCode/);
});

it('should respect the provided language code', function(done) {
var languageCode = 'uk';
var languageCode = 'en-GB';

var config = {
languageCode: languageCode
Expand All @@ -1019,7 +1048,8 @@ describe('Speech', function() {

it('should respect the provided encoding', function(done) {
var config = {
encoding: 'LINEAR32'
encoding: 'LINEAR32',
languageCode: 'en-US'
};

Speech.detectEncoding_ = function() {
Expand Down Expand Up @@ -1051,7 +1081,7 @@ describe('Speech', function() {
}
};

speech.startRecognition(FILE, {}, assert.ifError);
speech.startRecognition(FILE, {languageCode: 'en-US'}, assert.ifError);
});

it('should return an error from findFile_', function(done) {
Expand Down

0 comments on commit 7ed4291

Please sign in to comment.