Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language: allow raw API terminology #2055

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/language/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,19 @@ Document.PART_OF_SPEECH = {
* @resource [documents.annotateText API Documentation]{@link https://cloud.google.com/natural-language/reference/rest/v1/documents/annotateText}
*
* @param {object=} options - Configuration object. See
* [documents.annotateText](https://cloud.google.com/natural-language/reference/rest/v1/documents/annotateText#request-body).
* [documents.annotateText](https://cloud.google.com/natural-language/docs/reference/rest/v1/documents/annotateText#features).
* @param {boolean} options.entities - Detect the entities from this document.
* By default, all features (`entities`, `sentiment`, and `syntax`) are
* enabled. By overriding any of these values, all defaults are switched to
* `false`.
* `false`. (Alias for `options.extractEntities`)
* @param {number} options.sentiment - Detect the sentiment from this document.
* By default, all features (`entities`, `sentiment`, and `syntax`) are
* enabled. By overriding any of these values, all defaults are switched to
* `false`.
* `false`. (Alias for `options.extractSentiment`)
* @param {boolean} options.syntax - Detect the syntax from this document. By
* default, all features (`entities`, `sentiment`, and `syntax`) are
* enabled. By overriding any of these values, all defaults are switched to
* `false`.
* `false`. (Alias for `options.extractDocumentSyntax`)
* @param {boolean} options.verbose - Enable verbose mode for more detailed
* results. Default: `false`
* @param {function} callback - The callback function.
Expand Down Expand Up @@ -522,9 +522,12 @@ Document.prototype.annotate = function(options, callback) {
};

var featuresRequested = {
extractDocumentSentiment: options.sentiment === true,
extractEntities: options.entities === true,
extractSyntax: options.syntax === true
extractDocumentSentiment:
(options.extractDocumentSentiment || options.sentiment) === true,
extractEntities:
(options.extractEntities || options.entities) === true,
extractSyntax:
(options.extractSyntax || options.syntax) === true
};

var numFeaturesRequested = 0;
Expand Down Expand Up @@ -557,7 +560,7 @@ Document.prototype.annotate = function(options, callback) {
language: resp.language
};

if (resp.documentSentiment) {
if (resp.documentSentiment && features.extractDocumentSentiment) {
var sentiment = resp.documentSentiment;
annotation.sentiment = Document.formatSentiment_(sentiment, verbose);
}
Expand All @@ -568,7 +571,7 @@ Document.prototype.annotate = function(options, callback) {

// This prevents empty `sentences` and `tokens` arrays being returned to
// users who never wanted sentences or tokens to begin with.
if (numFeaturesRequested === 0 || featuresRequested.syntax) {
if (numFeaturesRequested === 0 || featuresRequested.extractSyntax) {
annotation.sentences = Document.formatSentences_(resp.sentences, verbose);
annotation.tokens = Document.formatTokens_(resp.tokens, verbose);
}
Expand Down
20 changes: 20 additions & 0 deletions packages/language/test/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,26 @@ describe('Document', function() {
}, assert.ifError);
});

it('should honor raw API terminology', function(done) {
document.api.Language = {
annotateText: function(reqOpts) {
assert.deepEqual(reqOpts.features, {
extractDocumentSentiment: true,
extractEntities: true,
extractSyntax: true
});

done();
}
};

document.annotate({
extractDocumentSentiment: true,
extractEntities: true,
extractSyntax: true
}, assert.ifError);
});

describe('error', function() {
var apiResponse = {};
var error = new Error('Error.');
Expand Down