diff --git a/packages/google-cloud-language/protos/google/cloud/language/v1/language_service.proto b/packages/google-cloud-language/protos/google/cloud/language/v1/language_service.proto index 6620c2c632f..6895b0d82d4 100644 --- a/packages/google-cloud-language/protos/google/cloud/language/v1/language_service.proto +++ b/packages/google-cloud-language/protos/google/cloud/language/v1/language_service.proto @@ -52,6 +52,11 @@ service LanguageService { option (google.api.http) = { post: "/v1/documents:analyzeSyntax" body: "*" }; } + // Classifies a document into categories. + rpc ClassifyText(ClassifyTextRequest) returns (ClassifyTextResponse) { + option (google.api.http) = { post: "/v1/documents:classifyText" body: "*" }; + } + // A convenience method that provides all the features that analyzeSentiment, // analyzeEntities, and analyzeSyntax provide in one call. rpc AnnotateText(AnnotateTextRequest) returns (AnnotateTextResponse) { @@ -839,6 +844,16 @@ message TextSpan { int32 begin_offset = 2; } +// Represents a category returned from the text classifier. +message ClassificationCategory { + // The name of the category representing the document. + string name = 1; + + // The classifier's confidence of the category. Number represents how certain + // the classifier is that this category represents the given text. + float confidence = 2; +} + // The sentiment analysis request message. message AnalyzeSentimentRequest { // Input document. @@ -925,6 +940,18 @@ message AnalyzeSyntaxResponse { string language = 3; } +// The document classification request message. +message ClassifyTextRequest { + // Input document. + Document document = 1; +} + +// The document classification response message. +message ClassifyTextResponse { + // Categories representing the input document. + repeated ClassificationCategory categories = 1; +} + // The request message for the text annotation API, which can perform multiple // analysis types (sentiment, entities, and syntax) in one call. message AnnotateTextRequest { @@ -942,6 +969,9 @@ message AnnotateTextRequest { // Extract entities and their associated sentiment. bool extract_entity_sentiment = 4; + + // Classify the full document into categories. + bool classify_text = 6; } // Input document. @@ -978,6 +1008,9 @@ message AnnotateTextResponse { // in the request or, if not specified, the automatically-detected language. // See [Document.language][google.cloud.language.v1.Document.language] field for more details. string language = 5; + + // Categories identified in the input document. + repeated ClassificationCategory categories = 6; } // Represents the text encoding that the caller uses to process the output. diff --git a/packages/google-cloud-language/src/v1/doc/google/cloud/language/v1/doc_language_service.js b/packages/google-cloud-language/src/v1/doc/google/cloud/language/v1/doc_language_service.js index a28e27464b6..eec9ea7fc9d 100644 --- a/packages/google-cloud-language/src/v1/doc/google/cloud/language/v1/doc_language_service.js +++ b/packages/google-cloud-language/src/v1/doc/google/cloud/language/v1/doc_language_service.js @@ -1367,6 +1367,24 @@ var TextSpan = { // This is for documentation. Actual contents will be loaded by gRPC. }; +/** + * Represents a category returned from the text classifier. + * + * @property {string} name + * The name of the category representing the document. + * + * @property {number} confidence + * The classifier's confidence of the category. Number represents how certain + * the classifier is that this category represents the given text. + * + * @typedef ClassificationCategory + * @memberof google.cloud.language.v1 + * @see [google.cloud.language.v1.ClassificationCategory definition in proto format]{@link https://github.com/googleapis/googleapis/blob/master/google/cloud/language/v1/language_service.proto} + */ +var ClassificationCategory = { + // This is for documentation. Actual contents will be loaded by gRPC. +}; + /** * The sentiment analysis request message. * @@ -1545,6 +1563,38 @@ var AnalyzeSyntaxResponse = { // This is for documentation. Actual contents will be loaded by gRPC. }; +/** + * The document classification request message. + * + * @property {Object} document + * Input document. + * + * This object should have the same structure as [Document]{@link google.cloud.language.v1.Document} + * + * @typedef ClassifyTextRequest + * @memberof google.cloud.language.v1 + * @see [google.cloud.language.v1.ClassifyTextRequest definition in proto format]{@link https://github.com/googleapis/googleapis/blob/master/google/cloud/language/v1/language_service.proto} + */ +var ClassifyTextRequest = { + // This is for documentation. Actual contents will be loaded by gRPC. +}; + +/** + * The document classification response message. + * + * @property {Object[]} categories + * Categories representing the input document. + * + * This object should have the same structure as [ClassificationCategory]{@link google.cloud.language.v1.ClassificationCategory} + * + * @typedef ClassifyTextResponse + * @memberof google.cloud.language.v1 + * @see [google.cloud.language.v1.ClassifyTextResponse definition in proto format]{@link https://github.com/googleapis/googleapis/blob/master/google/cloud/language/v1/language_service.proto} + */ +var ClassifyTextResponse = { + // This is for documentation. Actual contents will be loaded by gRPC. +}; + /** * The request message for the text annotation API, which can perform multiple * analysis types (sentiment, entities, and syntax) in one call. @@ -1587,6 +1637,9 @@ var AnnotateTextRequest = { * @property {boolean} extractEntitySentiment * Extract entities and their associated sentiment. * + * @property {boolean} classifyText + * Classify the full document into categories. + * * @typedef Features * @memberof google.cloud.language.v1 * @see [google.cloud.language.v1.AnnotateTextRequest.Features definition in proto format]{@link https://github.com/googleapis/googleapis/blob/master/google/cloud/language/v1/language_service.proto} @@ -1630,6 +1683,11 @@ var AnnotateTextRequest = { * in the request or, if not specified, the automatically-detected language. * See Document.language field for more details. * + * @property {Object[]} categories + * Categories identified in the input document. + * + * This object should have the same structure as [ClassificationCategory]{@link google.cloud.language.v1.ClassificationCategory} + * * @typedef AnnotateTextResponse * @memberof google.cloud.language.v1 * @see [google.cloud.language.v1.AnnotateTextResponse definition in proto format]{@link https://github.com/googleapis/googleapis/blob/master/google/cloud/language/v1/language_service.proto} diff --git a/packages/google-cloud-language/src/v1/language_service_client.js b/packages/google-cloud-language/src/v1/language_service_client.js index aa5654e24c3..bf9568229a6 100644 --- a/packages/google-cloud-language/src/v1/language_service_client.js +++ b/packages/google-cloud-language/src/v1/language_service_client.js @@ -32,28 +32,28 @@ class LanguageServiceClient { /** * Construct an instance of LanguageServiceClient. * - * @param {object=} options - The configuration object. See the subsequent + * @param {object} [options] - The configuration object. See the subsequent * parameters for more details. - * @param {object=} options.credentials - Credentials object. - * @param {string=} options.credentials.client_email - * @param {string=} options.credentials.private_key - * @param {string=} options.email - Account email address. Required when + * @param {object} [options.credentials] - Credentials object. + * @param {string} [options.credentials.client_email] + * @param {string} [options.credentials.private_key] + * @param {string} [options.email] - Account email address. Required when * usaing a .pem or .p12 keyFilename. - * @param {string=} options.keyFilename - Full path to the a .json, .pem, or + * @param {string} [options.keyFilename] - Full path to the a .json, .pem, or * .p12 key downloaded from the Google Developers Console. If you provide * a path to a JSON file, the projectId option above is not necessary. * NOTE: .pem and .p12 require you to specify options.email as well. - * @param {number=} options.port - The port on which to connect to + * @param {number} [options.port] - The port on which to connect to * the remote host. - * @param {string=} options.projectId - The project ID from the Google + * @param {string} [options.projectId] - The project ID from the Google * Developer's Console, e.g. 'grape-spaceship-123'. We will also check * the environment variable GCLOUD_PROJECT for your project ID. If your * app is running in an environment which supports * {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials}, * your project ID will be detected automatically. - * @param {function=} options.promise - Custom promise module to use instead + * @param {function} [options.promise] - Custom promise module to use instead * of native Promises. - * @param {string=} options.servicePath - The domain name of the + * @param {string} [options.servicePath] - The domain name of the * API remote host. */ constructor(opts) { @@ -124,6 +124,7 @@ class LanguageServiceClient { 'analyzeEntities', 'analyzeEntitySentiment', 'analyzeSyntax', + 'classifyText', 'annotateText', ]; for (let methodName of languageServiceStubMethods) { @@ -185,14 +186,14 @@ class LanguageServiceClient { * Input document. * * This object should have the same structure as [Document]{@link google.cloud.language.v1.Document} - * @param {number=} request.encodingType + * @param {number} [request.encodingType] * The encoding type used by the API to calculate sentence offsets. * * The number should be among the values of [EncodingType]{@link google.cloud.language.v1.EncodingType} - * @param {Object=} options + * @param {Object} [options] * Optional parameters. You can override the default settings for this call, e.g, timeout, * retries, paginations, etc. See [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} for the details. - * @param {function(?Error, ?Object)=} callback + * @param {function(?Error, ?Object)} [callback] * The function which will be called with the result of the API call. * * The second parameter to the callback is an object representing [AnalyzeSentimentResponse]{@link google.cloud.language.v1.AnalyzeSentimentResponse}. @@ -239,14 +240,14 @@ class LanguageServiceClient { * Input document. * * This object should have the same structure as [Document]{@link google.cloud.language.v1.Document} - * @param {number=} request.encodingType + * @param {number} [request.encodingType] * The encoding type used by the API to calculate offsets. * * The number should be among the values of [EncodingType]{@link google.cloud.language.v1.EncodingType} - * @param {Object=} options + * @param {Object} [options] * Optional parameters. You can override the default settings for this call, e.g, timeout, * retries, paginations, etc. See [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} for the details. - * @param {function(?Error, ?Object)=} callback + * @param {function(?Error, ?Object)} [callback] * The function which will be called with the result of the API call. * * The second parameter to the callback is an object representing [AnalyzeEntitiesResponse]{@link google.cloud.language.v1.AnalyzeEntitiesResponse}. @@ -292,14 +293,14 @@ class LanguageServiceClient { * Input document. * * This object should have the same structure as [Document]{@link google.cloud.language.v1.Document} - * @param {number=} request.encodingType + * @param {number} [request.encodingType] * The encoding type used by the API to calculate offsets. * * The number should be among the values of [EncodingType]{@link google.cloud.language.v1.EncodingType} - * @param {Object=} options + * @param {Object} [options] * Optional parameters. You can override the default settings for this call, e.g, timeout, * retries, paginations, etc. See [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} for the details. - * @param {function(?Error, ?Object)=} callback + * @param {function(?Error, ?Object)} [callback] * The function which will be called with the result of the API call. * * The second parameter to the callback is an object representing [AnalyzeEntitySentimentResponse]{@link google.cloud.language.v1.AnalyzeEntitySentimentResponse}. @@ -350,14 +351,14 @@ class LanguageServiceClient { * Input document. * * This object should have the same structure as [Document]{@link google.cloud.language.v1.Document} - * @param {number=} request.encodingType + * @param {number} [request.encodingType] * The encoding type used by the API to calculate offsets. * * The number should be among the values of [EncodingType]{@link google.cloud.language.v1.EncodingType} - * @param {Object=} options + * @param {Object} [options] * Optional parameters. You can override the default settings for this call, e.g, timeout, * retries, paginations, etc. See [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} for the details. - * @param {function(?Error, ?Object)=} callback + * @param {function(?Error, ?Object)} [callback] * The function which will be called with the result of the API call. * * The second parameter to the callback is an object representing [AnalyzeSyntaxResponse]{@link google.cloud.language.v1.AnalyzeSyntaxResponse}. @@ -393,6 +394,54 @@ class LanguageServiceClient { return this._innerApiCalls.analyzeSyntax(request, options, callback); } + /** + * Classifies a document into categories. + * + * @param {Object} request + * The request object that will be sent. + * @param {Object} request.document + * Input document. + * + * This object should have the same structure as [Document]{@link google.cloud.language.v1.Document} + * @param {Object} [options] + * Optional parameters. You can override the default settings for this call, e.g, timeout, + * retries, paginations, etc. See [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} for the details. + * @param {function(?Error, ?Object)} [callback] + * The function which will be called with the result of the API call. + * + * The second parameter to the callback is an object representing [ClassifyTextResponse]{@link google.cloud.language.v1.ClassifyTextResponse}. + * @returns {Promise} - The promise which resolves to an array. + * The first element of the array is an object representing [ClassifyTextResponse]{@link google.cloud.language.v1.ClassifyTextResponse}. + * The promise has a method named "cancel" which cancels the ongoing API call. + * + * @example + * + * const language = require('@google-cloud/language'); + * + * var client = new language.v1.LanguageServiceClient({ + * // optional auth parameters. + * }); + * + * var document = {}; + * client.classifyText({document: document}) + * .then(responses => { + * var response = responses[0]; + * // doThingsWith(response) + * }) + * .catch(err => { + * console.error(err); + * }); + */ + classifyText(request, options, callback) { + if (options instanceof Function && callback === undefined) { + callback = options; + options = {}; + } + options = options || {}; + + return this._innerApiCalls.classifyText(request, options, callback); + } + /** * A convenience method that provides all the features that analyzeSentiment, * analyzeEntities, and analyzeSyntax provide in one call. @@ -407,14 +456,14 @@ class LanguageServiceClient { * The enabled features. * * This object should have the same structure as [Features]{@link google.cloud.language.v1.Features} - * @param {number=} request.encodingType + * @param {number} [request.encodingType] * The encoding type used by the API to calculate offsets. * * The number should be among the values of [EncodingType]{@link google.cloud.language.v1.EncodingType} - * @param {Object=} options + * @param {Object} [options] * Optional parameters. You can override the default settings for this call, e.g, timeout, * retries, paginations, etc. See [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions} for the details. - * @param {function(?Error, ?Object)=} callback + * @param {function(?Error, ?Object)} [callback] * The function which will be called with the result of the API call. * * The second parameter to the callback is an object representing [AnnotateTextResponse]{@link google.cloud.language.v1.AnnotateTextResponse}. diff --git a/packages/google-cloud-language/src/v1/language_service_client_config.json b/packages/google-cloud-language/src/v1/language_service_client_config.json index 7c00b67d111..0f2f69be6c1 100644 --- a/packages/google-cloud-language/src/v1/language_service_client_config.json +++ b/packages/google-cloud-language/src/v1/language_service_client_config.json @@ -40,6 +40,11 @@ "retry_codes_name": "idempotent", "retry_params_name": "default" }, + "ClassifyText": { + "timeout_millis": 30000, + "retry_codes_name": "idempotent", + "retry_params_name": "default" + }, "AnnotateText": { "timeout_millis": 30000, "retry_codes_name": "idempotent", diff --git a/packages/google-cloud-language/test/gapic-v1.js b/packages/google-cloud-language/test/gapic-v1.js index 4b9a0e33050..550c0ad5300 100644 --- a/packages/google-cloud-language/test/gapic-v1.js +++ b/packages/google-cloud-language/test/gapic-v1.js @@ -25,7 +25,10 @@ error.code = FAKE_STATUS_CODE; describe('LanguageServiceClient', () => { describe('analyzeSentiment', () => { it('invokes analyzeSentiment without error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -53,7 +56,10 @@ describe('LanguageServiceClient', () => { }); it('invokes analyzeSentiment with error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -79,7 +85,10 @@ describe('LanguageServiceClient', () => { describe('analyzeEntities', () => { it('invokes analyzeEntities without error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -107,7 +116,10 @@ describe('LanguageServiceClient', () => { }); it('invokes analyzeEntities with error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -133,7 +145,10 @@ describe('LanguageServiceClient', () => { describe('analyzeEntitySentiment', () => { it('invokes analyzeEntitySentiment without error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -161,7 +176,10 @@ describe('LanguageServiceClient', () => { }); it('invokes analyzeEntitySentiment with error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -187,7 +205,10 @@ describe('LanguageServiceClient', () => { describe('analyzeSyntax', () => { it('invokes analyzeSyntax without error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -215,7 +236,10 @@ describe('LanguageServiceClient', () => { }); it('invokes analyzeSyntax with error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -239,9 +263,69 @@ describe('LanguageServiceClient', () => { }); }); + describe('classifyText', () => { + it('invokes classifyText without error', done => { + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + + // Mock request + var document = {}; + var request = { + document: document, + }; + + // Mock response + var expectedResponse = {}; + + // Mock Grpc layer + client._innerApiCalls.classifyText = mockSimpleGrpcMethod( + request, + expectedResponse + ); + + client.classifyText(request, (err, response) => { + assert.ifError(err); + assert.deepStrictEqual(response, expectedResponse); + done(); + }); + }); + + it('invokes classifyText with error', done => { + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + + // Mock request + var document = {}; + var request = { + document: document, + }; + + // Mock Grpc layer + client._innerApiCalls.classifyText = mockSimpleGrpcMethod( + request, + null, + error + ); + + client.classifyText(request, (err, response) => { + assert(err instanceof Error); + assert.equal(err.code, FAKE_STATUS_CODE); + assert(typeof response === 'undefined'); + done(); + }); + }); + }); + describe('annotateText', () => { it('invokes annotateText without error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {}; @@ -271,7 +355,10 @@ describe('LanguageServiceClient', () => { }); it('invokes annotateText with error', done => { - var client = new languageModule.v1.LanguageServiceClient(); + var client = new languageModule.v1.LanguageServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); // Mock request var document = {};