Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

feat: Add support for V1 and V2 classification models #376

Merged
merged 6 commits into from
Sep 26, 2022
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
2 changes: 2 additions & 0 deletions google/cloud/language/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
AnnotateTextRequest,
AnnotateTextResponse,
ClassificationCategory,
ClassificationModelOptions,
ClassifyTextRequest,
ClassifyTextResponse,
DependencyEdge,
Expand Down Expand Up @@ -60,6 +61,7 @@
"AnnotateTextRequest",
"AnnotateTextResponse",
"ClassificationCategory",
"ClassificationModelOptions",
"ClassifyTextRequest",
"ClassifyTextResponse",
"DependencyEdge",
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/language_v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
AnnotateTextRequest,
AnnotateTextResponse,
ClassificationCategory,
ClassificationModelOptions,
ClassifyTextRequest,
ClassifyTextResponse,
DependencyEdge,
Expand Down Expand Up @@ -54,6 +55,7 @@
"AnnotateTextRequest",
"AnnotateTextResponse",
"ClassificationCategory",
"ClassificationModelOptions",
"ClassifyTextRequest",
"ClassifyTextResponse",
"DependencyEdge",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ async def sample_analyze_sentiment():
The request object. The sentiment analysis request
message.
document (:class:`google.cloud.language_v1.types.Document`):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -367,7 +367,7 @@ async def sample_analyze_entities():
request (Union[google.cloud.language_v1.types.AnalyzeEntitiesRequest, dict]):
The request object. The entity analysis request message.
document (:class:`google.cloud.language_v1.types.Document`):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -485,7 +485,7 @@ async def sample_analyze_entity_sentiment():
The request object. The entity-level sentiment analysis
request message.
document (:class:`google.cloud.language_v1.types.Document`):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -603,7 +603,7 @@ async def sample_analyze_syntax():
request (Union[google.cloud.language_v1.types.AnalyzeSyntaxRequest, dict]):
The request object. The syntax analysis request message.
document (:class:`google.cloud.language_v1.types.Document`):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -717,7 +717,7 @@ async def sample_classify_text():
The request object. The document classification request
message.
document (:class:`google.cloud.language_v1.types.Document`):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -829,12 +829,12 @@ async def sample_annotate_text():
annotation API, which can perform multiple analysis
types (sentiment, entities, and syntax) in one call.
document (:class:`google.cloud.language_v1.types.Document`):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
features (:class:`google.cloud.language_v1.types.AnnotateTextRequest.Features`):
The enabled features.
Required. The enabled features.
This corresponds to the ``features`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down
14 changes: 7 additions & 7 deletions google/cloud/language_v1/services/language_service/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def sample_analyze_sentiment():
The request object. The sentiment analysis request
message.
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -562,7 +562,7 @@ def sample_analyze_entities():
request (Union[google.cloud.language_v1.types.AnalyzeEntitiesRequest, dict]):
The request object. The entity analysis request message.
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -670,7 +670,7 @@ def sample_analyze_entity_sentiment():
The request object. The entity-level sentiment analysis
request message.
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -778,7 +778,7 @@ def sample_analyze_syntax():
request (Union[google.cloud.language_v1.types.AnalyzeSyntaxRequest, dict]):
The request object. The syntax analysis request message.
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -882,7 +882,7 @@ def sample_classify_text():
The request object. The document classification request
message.
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down Expand Up @@ -984,12 +984,12 @@ def sample_annotate_text():
annotation API, which can perform multiple analysis
types (sentiment, entities, and syntax) in one call.
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
This corresponds to the ``document`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
features (google.cloud.language_v1.types.AnnotateTextRequest.Features):
The enabled features.
Required. The enabled features.
This corresponds to the ``features`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/language_v1/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
AnnotateTextRequest,
AnnotateTextResponse,
ClassificationCategory,
ClassificationModelOptions,
ClassifyTextRequest,
ClassifyTextResponse,
DependencyEdge,
Expand All @@ -51,6 +52,7 @@
"AnnotateTextRequest",
"AnnotateTextResponse",
"ClassificationCategory",
"ClassificationModelOptions",
"ClassifyTextRequest",
"ClassifyTextResponse",
"DependencyEdge",
Expand Down
96 changes: 89 additions & 7 deletions google/cloud/language_v1/types/language_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"EntityMention",
"TextSpan",
"ClassificationCategory",
"ClassificationModelOptions",
"AnalyzeSentimentRequest",
"AnalyzeSentimentResponse",
"AnalyzeEntitySentimentRequest",
Expand Down Expand Up @@ -728,12 +729,76 @@ class ClassificationCategory(proto.Message):
)


class ClassificationModelOptions(proto.Message):
r"""Model options available for classification requests.

This message has `oneof`_ fields (mutually exclusive fields).
For each oneof, at most one member field can be set at the same time.
Setting any member of the oneof automatically clears all other
members.

.. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields

Attributes:
v1_model (google.cloud.language_v1.types.ClassificationModelOptions.V1Model):
Setting this field will use the V1 model and
V1 content categories version. The V1 model is a
legacy model; support for this will be
discontinued in the future.

This field is a member of `oneof`_ ``model_type``.
v2_model (google.cloud.language_v1.types.ClassificationModelOptions.V2Model):
Setting this field will use the V2 model with
the appropriate content categories version. The
V2 model is a better performing model.

This field is a member of `oneof`_ ``model_type``.
"""

class V1Model(proto.Message):
r"""Options for the V1 model."""

class V2Model(proto.Message):
r"""Options for the V2 model.

Attributes:
content_categories_version (google.cloud.language_v1.types.ClassificationModelOptions.V2Model.ContentCategoriesVersion):
The content categories used for
classification.
"""

class ContentCategoriesVersion(proto.Enum):
r"""The content categories used for classification."""
CONTENT_CATEGORIES_VERSION_UNSPECIFIED = 0
V1 = 1
V2 = 2

content_categories_version = proto.Field(
proto.ENUM,
number=1,
enum="ClassificationModelOptions.V2Model.ContentCategoriesVersion",
)

v1_model = proto.Field(
proto.MESSAGE,
number=1,
oneof="model_type",
message=V1Model,
)
v2_model = proto.Field(
proto.MESSAGE,
number=2,
oneof="model_type",
message=V2Model,
)


class AnalyzeSentimentRequest(proto.Message):
r"""The sentiment analysis request message.

Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
encoding_type (google.cloud.language_v1.types.EncodingType):
The encoding type used by the API to
calculate sentence offsets.
Expand Down Expand Up @@ -789,7 +854,7 @@ class AnalyzeEntitySentimentRequest(proto.Message):

Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
encoding_type (google.cloud.language_v1.types.EncodingType):
The encoding type used by the API to
calculate offsets.
Expand Down Expand Up @@ -838,7 +903,7 @@ class AnalyzeEntitiesRequest(proto.Message):

Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
encoding_type (google.cloud.language_v1.types.EncodingType):
The encoding type used by the API to
calculate offsets.
Expand Down Expand Up @@ -887,7 +952,7 @@ class AnalyzeSyntaxRequest(proto.Message):

Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
encoding_type (google.cloud.language_v1.types.EncodingType):
The encoding type used by the API to
calculate offsets.
Expand Down Expand Up @@ -943,14 +1008,22 @@ class ClassifyTextRequest(proto.Message):

Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
classification_model_options (google.cloud.language_v1.types.ClassificationModelOptions):
Model options to use for classification.
Defaults to v1 options if not specified.
"""

document = proto.Field(
proto.MESSAGE,
number=1,
message="Document",
)
classification_model_options = proto.Field(
proto.MESSAGE,
number=3,
message="ClassificationModelOptions",
)


class ClassifyTextResponse(proto.Message):
Expand All @@ -975,9 +1048,9 @@ class AnnotateTextRequest(proto.Message):

Attributes:
document (google.cloud.language_v1.types.Document):
Input document.
Required. Input document.
features (google.cloud.language_v1.types.AnnotateTextRequest.Features):
The enabled features.
Required. The enabled features.
encoding_type (google.cloud.language_v1.types.EncodingType):
The encoding type used by the API to
calculate offsets.
Expand All @@ -1000,6 +1073,10 @@ class Features(proto.Message):
sentiment.
classify_text (bool):
Classify the full document into categories.
classification_model_options (google.cloud.language_v1.types.ClassificationModelOptions):
The model options to use for classification. Defaults to v1
options if not specified. Only used if ``classify_text`` is
set to true.
"""

extract_syntax = proto.Field(
Expand All @@ -1022,6 +1099,11 @@ class Features(proto.Message):
proto.BOOL,
number=6,
)
classification_model_options = proto.Field(
proto.MESSAGE,
number=10,
message="ClassificationModelOptions",
)

document = proto.Field(
proto.MESSAGE,
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/language_v1beta2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
AnnotateTextRequest,
AnnotateTextResponse,
ClassificationCategory,
ClassificationModelOptions,
ClassifyTextRequest,
ClassifyTextResponse,
DependencyEdge,
Expand Down Expand Up @@ -54,6 +55,7 @@
"AnnotateTextRequest",
"AnnotateTextResponse",
"ClassificationCategory",
"ClassificationModelOptions",
"ClassifyTextRequest",
"ClassifyTextResponse",
"DependencyEdge",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ async def analyze_syntax(
metadata: Sequence[Tuple[str, str]] = (),
) -> language_service.AnalyzeSyntaxResponse:
r"""Analyzes the syntax of the text and provides sentence
boundaries and tokenization along with part-of-speech
boundaries and tokenization along with part of speech
tags, dependency trees, and other properties.

.. code-block:: python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ def analyze_syntax(
metadata: Sequence[Tuple[str, str]] = (),
) -> language_service.AnalyzeSyntaxResponse:
r"""Analyzes the syntax of the text and provides sentence
boundaries and tokenization along with part-of-speech
boundaries and tokenization along with part of speech
tags, dependency trees, and other properties.

.. code-block:: python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def analyze_syntax(
r"""Return a callable for the analyze syntax method over gRPC.

Analyzes the syntax of the text and provides sentence
boundaries and tokenization along with part-of-speech
boundaries and tokenization along with part of speech
tags, dependency trees, and other properties.

Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def analyze_syntax(
r"""Return a callable for the analyze syntax method over gRPC.

Analyzes the syntax of the text and provides sentence
boundaries and tokenization along with part-of-speech
boundaries and tokenization along with part of speech
tags, dependency trees, and other properties.

Returns:
Expand Down
Loading