Skip to content

Commit

Permalink
Add the classifyText method to language (v1). (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored Nov 13, 2017
1 parent 068ca25 commit 5474d05
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down
99 changes: 74 additions & 25 deletions packages/google-cloud-language/src/v1/language_service_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -124,6 +124,7 @@ class LanguageServiceClient {
'analyzeEntities',
'analyzeEntitySentiment',
'analyzeSyntax',
'classifyText',
'annotateText',
];
for (let methodName of languageServiceStubMethods) {
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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.
Expand All @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 5474d05

Please sign in to comment.