From d0f1b6ebd9b4f0fcde5e65304345d84b76261b93 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Mon, 21 Aug 2023 16:37:50 -0700 Subject: [PATCH 01/23] Update API v2 example code for analyze_sentiment --- .../generated-samples/v1/language_sentiment_text.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/language/snippets/generated-samples/v1/language_sentiment_text.py b/language/snippets/generated-samples/v1/language_sentiment_text.py index 81b738f1395..203f4b2605f 100644 --- a/language/snippets/generated-samples/v1/language_sentiment_text.py +++ b/language/snippets/generated-samples/v1/language_sentiment_text.py @@ -23,18 +23,18 @@ # isort: split # [START language_sentiment_text] -from google.cloud import language_v1 +from google.cloud import language_v2 def sample_analyze_sentiment(content): - client = language_v1.LanguageServiceClient() + client = language_v2.LanguageServiceClient() # content = 'Your text to analyze, e.g. Hello, world!' if isinstance(content, bytes): content = content.decode("utf-8") - type_ = language_v1.Document.Type.PLAIN_TEXT + type_ = language_v2.Document.Type.PLAIN_TEXT document = {"type_": type_, "content": content} response = client.analyze_sentiment(request={"document": document}) From 691283b492a31a764383b7ed82c1a28e475e9379 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Mon, 21 Aug 2023 17:00:24 -0700 Subject: [PATCH 02/23] Update library client version to 2.11.0 --- language/snippets/generated-samples/v1/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/snippets/generated-samples/v1/requirements.txt b/language/snippets/generated-samples/v1/requirements.txt index 8f4b5cb471e..35c9e5e4dc6 100644 --- a/language/snippets/generated-samples/v1/requirements.txt +++ b/language/snippets/generated-samples/v1/requirements.txt @@ -1 +1 @@ -google-cloud-language==2.9.1 +google-cloud-language==2.11.0 From dbb09f20b662c050ce51739326cd76d2c65c7102 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Tue, 22 Aug 2023 12:08:25 -0700 Subject: [PATCH 03/23] Update API v2 example code for analyze_entities --- language/v2/language_entities_gcs.py | 115 ++++++++++++++++++ language/v2/language_entities_text.py | 106 ++++++++++++++++ language/v2/requirements-test.txt | 1 + language/v2/requirements.txt | 1 + .../v2/tests/analyzing_entities.test.yaml | 111 +++++++++++++++++ 5 files changed, 334 insertions(+) create mode 100644 language/v2/language_entities_gcs.py create mode 100644 language/v2/language_entities_text.py create mode 100644 language/v2/requirements-test.txt create mode 100644 language/v2/requirements.txt create mode 100644 language/v2/tests/analyzing_entities.test.yaml diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py new file mode 100644 index 00000000000..68420e68258 --- /dev/null +++ b/language/v2/language_entities_gcs.py @@ -0,0 +1,115 @@ +# +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# DO NOT EDIT! This is a generated sample ("Request", "language_entities_gcs") + +# To install the latest published package dependency, execute the following: +# pip install google-cloud-language + +# sample-metadata +# title: Analyzing Entities (GCS) +# description: Analyzing Entities in text file stored in Cloud Storage +# usage: python3 samples/v2/language_entities_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/entity.txt"] + +# [START language_entities_gcs] +from google.cloud import language_v2 + + +def sample_analyze_entities(gcs_content_uri): + """ + Analyzing Entities in text file stored in Cloud Storage + + Args: + gcs_content_uri Google Cloud Storage URI where the file content is located. + e.g. gs://[Your Bucket]/[Path to File] + """ + + client = language_v2.LanguageServiceClient() + + # gcs_content_uri = 'gs://cloud-samples-data/language/entity.txt' + + # Available types: PLAIN_TEXT, HTML + type_ = language_v2.Document.Type.PLAIN_TEXT + + # Optional. If not specified, the language is automatically detected. + # For list of supported languages: + # https://cloud.google.com/natural-language/docs/languages + language_code = "en" + document = { + "gcs_content_uri": gcs_content_uri, + "type_": type_, + "language_code": language_code, + } + + # Available values: NONE, UTF8, UTF16, UTF32 + encoding_type = language_v2.EncodingType.UTF8 + + response = client.analyze_entities( + request={"document": document, "encoding_type": encoding_type} + ) + + # Loop through entitites returned from the API + for entity in response.entities: + print(f"Representative name for the entity: {entity.name}") + + # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al + print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}") + + # Loop over the metadata associated with entity. + # Some entity types may have additional metadata, e.g. ADDRESS entities + # may have metadata for the address street_name, postal_code, et al. + for metadata_name, metadata_value in entity.metadata.items(): + print(f"{metadata_name}: {metadata_value}") + + # Loop over the mentions of this entity in the input document. + # The API currently supports proper noun mentions. + for mention in entity.mentions: + print(f"Mention text: {mention.text.content}") + + # Get the mention type, e.g. PROPER for proper noun + print( + "Mention type: {}".format( + language_v2.EntityMention.Type(mention.type_).name + ) + ) + + # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. + print(f"Probability score: {mention.probability}") + + # Get the language of the text, which will be the same as + # the language specified in the request or, if not specified, + # the automatically-detected language. + print(f"Language code of the text: {response.language_code}") + + +# [END language_entities_gcs] + + +def main(): + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument( + "--gcs_content_uri", + type=str, + default="gs://cloud-samples-data/language/entity.txt", + ) + args = parser.parse_args() + + sample_analyze_entities(args.gcs_content_uri) + + +if __name__ == "__main__": + main() diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py new file mode 100644 index 00000000000..6047c313d9c --- /dev/null +++ b/language/v2/language_entities_text.py @@ -0,0 +1,106 @@ +# +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# DO NOT EDIT! This is a generated sample ("Request", "language_entities_text") + +# To install the latest published package dependency, execute the following: +# pip install google-cloud-language + +# sample-metadata +# title: Analyzing Entities +# description: Analyzing Entities in a String +# usage: python3 samples/v2/language_entities_text.py [--text_content "California is a state."] + +# [START language_entities_text] +from google.cloud import language_v2 + + +def sample_analyze_entities(text_content): + """ + Analyzing Entities in a String + + Args: + text_content The text content to analyze + """ + + client = language_v2.LanguageServiceClient() + + # text_content = 'California is a state.' + + # Available types: PLAIN_TEXT, HTML + type_ = language_v2.Document.Type.PLAIN_TEXT + + # Optional. If not specified, the language is automatically detected. + # For list of supported languages: + # https://cloud.google.com/natural-language/docs/languages + language_code = "en" + document = {"content": text_content, "type_": type_, "language_code": language_code} + + # Available values: NONE, UTF8, UTF16, UTF32 + encoding_type = language_v2.EncodingType.UTF8 + + response = client.analyze_entities( + request={"document": document, "encoding_type": encoding_type} + ) + + # Loop through entitites returned from the API + for entity in response.entities: + print(f"Representative name for the entity: {entity.name}") + + # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al + print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}") + + # Loop over the metadata associated with entity. + # Some entity types may have additional metadata, e.g. ADDRESS entities + # may have metadata for the address street_name, postal_code, et al. + for metadata_name, metadata_value in entity.metadata.items(): + print(f"{metadata_name}: {metadata_value}") + + # Loop over the mentions of this entity in the input document. + # The API currently supports proper noun mentions. + for mention in entity.mentions: + print(f"Mention text: {mention.text.content}") + + # Get the mention type, e.g. PROPER for proper noun + print( + "Mention type: {}".format( + language_v2.EntityMention.Type(mention.type_).name + ) + ) + + # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. + print(f"Probability score: {mention.probability}") + + # Get the language code of the text, which will be the same as + # the language specified in the request or, if not specified, + # the automatically-detected language. + print(f"Language code of the text: {response.language_code}") + + +# [END language_entities_text] + + +def main(): + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument("--text_content", type=str, default="California is a state.") + args = parser.parse_args() + + sample_analyze_entities(args.text_content) + + +if __name__ == "__main__": + main() diff --git a/language/v2/requirements-test.txt b/language/v2/requirements-test.txt new file mode 100644 index 00000000000..49780e03569 --- /dev/null +++ b/language/v2/requirements-test.txt @@ -0,0 +1 @@ +pytest==7.2.0 diff --git a/language/v2/requirements.txt b/language/v2/requirements.txt new file mode 100644 index 00000000000..35c9e5e4dc6 --- /dev/null +++ b/language/v2/requirements.txt @@ -0,0 +1 @@ +google-cloud-language==2.11.0 diff --git a/language/v2/tests/analyzing_entities.test.yaml b/language/v2/tests/analyzing_entities.test.yaml new file mode 100644 index 00000000000..a7acbaaf194 --- /dev/null +++ b/language/v2/tests/analyzing_entities.test.yaml @@ -0,0 +1,111 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +type: test/samples +schema_version: 1 +test: + suites: + - name: "Analyzing Entities [code sample tests]" + cases: + + - name: language_entities_text - Analyzing the Entities of a text string (default value) + spec: + # Default value: "California is a state." + - call: {sample: language_entities_text} + - assert_contains: + - {literal: "Representative name for the entity: California"} + - {literal: "Entity type: LOCATION"} + - {literal: "Probability score:"} + - {literal: "mid: /m/01n7q"} + - {literal: "Mention text: California"} + - {literal: "Mention type: PROPER"} + - {literal: "Mention text: state"} + - {literal: "Mention type: COMMON"} + - {literal: "Language of the text: en"} + + - name: language_entities_text - Analyzing the Entities of a text string (*custom value*) + spec: + # Custom value: "Alice is a person. She lives in California." + - call: + sample: language_entities_text + params: + text_content: {literal: "Alice is a person. She lives in California."} + - assert_contains: + - {literal: "Representative name for the entity: Alice"} + - {literal: "Entity type: PERSON"} + - {literal: "Mention text: Alice"} + - {literal: "Mention type: PROPER"} + - {literal: "Mention text: person"} + - {literal: "Mention type: COMMON"} + - {literal: "Representative name for the entity: California"} + - {literal: "Entity type: LOCATION"} + - {literal: "mid: /m/01n7q"} + - {literal: "Language of the text: en"} + + - name: language_entities_text - Analyzing the Entities of a text string (*metadata attributes*) + spec: + # Try out some of the metadata attributes which should be available for dates, addresses, etc. + # In case fake (555) area code numbers don't work, using United States Naval Observatory number. + # Custom value: "I called 202-762-1401 on January 31, 2019 from 1600 Amphitheatre Parkway, Mountain View, CA." + - call: + sample: language_entities_text + params: + text_content: + literal: "I called 202-762-1401 on January 31, 2019 from 1600 Amphitheatre Parkway, Mountain View, CA." + # The results may change, but it's fair to say that at least one of the following types were detected: + - assert_contains_any: + - literal: "Entity type: DATE" + - literal: "Entity type: ADDRESS" + - literal: "Entity type: PHONE_NUMBER" + # Check that at least some of the supporting metadata for an entity was present in the response + - assert_contains_any: + - literal: "month: 1" + - literal: "day: 31" + - literal: "year: 2019" + - literal: "street_number: 1600" + - literal: "street_name: Amphitheatre Parkway" + - literal: "area_code: 202" + - literal: "number: 7621401" + + - name: language_entities_gcs - Analyzing the Entities of text file in GCS (default value) + spec: + # Default value: gs://cloud-samples-data/language/entity.txt + # => "California is a state." + - call: {sample: language_entities_gcs} + - assert_contains: + - {literal: "Representative name for the entity: California"} + - {literal: "Entity type: LOCATION"} + - {literal: "Probability score:"} + - {literal: "mid: /m/01n7q"} + - {literal: "Mention text: California"} + - {literal: "Mention type: PROPER"} + - {literal: "Mention text: state"} + - {literal: "Mention type: COMMON"} + - {literal: "Language of the text: en"} + + - name: language_entities_gcs - Analyzing the Entities of text file in GCS (*custom value*) + spec: + # Use different file: gs://cloud-samples-data/language/entity-sentiment.txt + # => "Grapes are good. Bananas are bad." + - call: + sample: language_entities_gcs + params: + gcs_content_uri: + literal: "gs://cloud-samples-data/language/entity-sentiment.txt" + - assert_contains: + - {literal: "Representative name for the entity: Grapes"} + - {literal: "Mention text: Grapes"} + - {literal: "Mention type: COMMON"} + - {literal: "Representative name for the entity: Bananas"} + - {literal: "Mention text: Bananas"} + - {literal: "Language of the text: en"} From 3fcb0245b987883e850982f489d5637e11037c68 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Tue, 22 Aug 2023 12:11:12 -0700 Subject: [PATCH 04/23] Update API v2 example code for analyze_entities --- language/v2/language_entities_gcs.py | 2 +- language/v2/language_entities_text.py | 4 ++-- language/v2/tests/analyzing_entities.test.yaml | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index 68420e68258..2061edabe76 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -91,7 +91,7 @@ def sample_analyze_entities(gcs_content_uri): # Get the language of the text, which will be the same as # the language specified in the request or, if not specified, # the automatically-detected language. - print(f"Language code of the text: {response.language_code}") + print(f"Language of the text: {response.language_code}") # [END language_entities_gcs] diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index 6047c313d9c..2a413c52625 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -83,10 +83,10 @@ def sample_analyze_entities(text_content): # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. print(f"Probability score: {mention.probability}") - # Get the language code of the text, which will be the same as + # Get the language of the text, which will be the same as # the language specified in the request or, if not specified, # the automatically-detected language. - print(f"Language code of the text: {response.language_code}") + print(f"Language of the text: {response.language_code}") # [END language_entities_text] diff --git a/language/v2/tests/analyzing_entities.test.yaml b/language/v2/tests/analyzing_entities.test.yaml index a7acbaaf194..5de21bbd689 100644 --- a/language/v2/tests/analyzing_entities.test.yaml +++ b/language/v2/tests/analyzing_entities.test.yaml @@ -26,7 +26,6 @@ test: - {literal: "Representative name for the entity: California"} - {literal: "Entity type: LOCATION"} - {literal: "Probability score:"} - - {literal: "mid: /m/01n7q"} - {literal: "Mention text: California"} - {literal: "Mention type: PROPER"} - {literal: "Mention text: state"} @@ -49,7 +48,6 @@ test: - {literal: "Mention type: COMMON"} - {literal: "Representative name for the entity: California"} - {literal: "Entity type: LOCATION"} - - {literal: "mid: /m/01n7q"} - {literal: "Language of the text: en"} - name: language_entities_text - Analyzing the Entities of a text string (*metadata attributes*) @@ -86,7 +84,6 @@ test: - {literal: "Representative name for the entity: California"} - {literal: "Entity type: LOCATION"} - {literal: "Probability score:"} - - {literal: "mid: /m/01n7q"} - {literal: "Mention text: California"} - {literal: "Mention type: PROPER"} - {literal: "Mention text: state"} From 81e7ac83437cbf49984d66f1eb7c196244e35f5b Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Tue, 22 Aug 2023 12:17:14 -0700 Subject: [PATCH 05/23] Revert back example code for analyze_sentiment to v1 --- .../generated-samples/v1/language_sentiment_text.py | 6 +++--- language/snippets/generated-samples/v1/requirements.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/language/snippets/generated-samples/v1/language_sentiment_text.py b/language/snippets/generated-samples/v1/language_sentiment_text.py index 203f4b2605f..81b738f1395 100644 --- a/language/snippets/generated-samples/v1/language_sentiment_text.py +++ b/language/snippets/generated-samples/v1/language_sentiment_text.py @@ -23,18 +23,18 @@ # isort: split # [START language_sentiment_text] -from google.cloud import language_v2 +from google.cloud import language_v1 def sample_analyze_sentiment(content): - client = language_v2.LanguageServiceClient() + client = language_v1.LanguageServiceClient() # content = 'Your text to analyze, e.g. Hello, world!' if isinstance(content, bytes): content = content.decode("utf-8") - type_ = language_v2.Document.Type.PLAIN_TEXT + type_ = language_v1.Document.Type.PLAIN_TEXT document = {"type_": type_, "content": content} response = client.analyze_sentiment(request={"document": document}) diff --git a/language/snippets/generated-samples/v1/requirements.txt b/language/snippets/generated-samples/v1/requirements.txt index 35c9e5e4dc6..8f4b5cb471e 100644 --- a/language/snippets/generated-samples/v1/requirements.txt +++ b/language/snippets/generated-samples/v1/requirements.txt @@ -1 +1 @@ -google-cloud-language==2.11.0 +google-cloud-language==2.9.1 From 3b552c8ca64b42fceebab0e6c2b4d4d213bf18a5 Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 15:23:21 -0700 Subject: [PATCH 06/23] Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_gcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index 2061edabe76..a8726a501fb 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -29,7 +29,7 @@ def sample_analyze_entities(gcs_content_uri): """ - Analyzing Entities in text file stored in Cloud Storage + Analyzes Entities in text file stored in Cloud Storage. Args: gcs_content_uri Google Cloud Storage URI where the file content is located. From 6ed14e20bbc6c8419319f38bc1b7ec1f909e7bb9 Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 15:23:30 -0700 Subject: [PATCH 07/23] Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_gcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index a8726a501fb..f68349e518d 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -1,5 +1,5 @@ # -# Copyright 2020 Google LLC +# Copyright 2023 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From 9310d679e2ea024c9d26038772d9a329256a834f Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 15:30:00 -0700 Subject: [PATCH 08/23] Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_gcs.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index f68349e518d..a23a7d3d340 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -80,9 +80,8 @@ def sample_analyze_entities(gcs_content_uri): # Get the mention type, e.g. PROPER for proper noun print( - "Mention type: {}".format( - language_v2.EntityMention.Type(mention.type_).name - ) + 'Mention type:' + f' {language_v2.EntityMention.Type(mention.type_).name}' ) # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. From 2398a41962ce002987b5f75398ac0dedd1b7e129 Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 15:23:30 -0700 Subject: [PATCH 09/23] Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_gcs.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index a23a7d3d340..c83451b7755 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -32,14 +32,12 @@ def sample_analyze_entities(gcs_content_uri): Analyzes Entities in text file stored in Cloud Storage. Args: - gcs_content_uri Google Cloud Storage URI where the file content is located. + gcs_content_uri: Google Cloud Storage URI where the file content is located. e.g. gs://[Your Bucket]/[Path to File] """ client = language_v2.LanguageServiceClient() - # gcs_content_uri = 'gs://cloud-samples-data/language/entity.txt' - # Available types: PLAIN_TEXT, HTML type_ = language_v2.Document.Type.PLAIN_TEXT From 9841f661ed239f4b32f14bed587f2f448ee3db92 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Tue, 22 Aug 2023 15:33:47 -0700 Subject: [PATCH 10/23] Update based on comments --- language/v2/language_entities_text.py | 2 +- language/v2/tests/analyzing_entities.test.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index 2a413c52625..ffb52f70055 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -1,5 +1,5 @@ # -# Copyright 2020 Google LLC +# Copyright 2023 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/language/v2/tests/analyzing_entities.test.yaml b/language/v2/tests/analyzing_entities.test.yaml index 5de21bbd689..e75f5002f44 100644 --- a/language/v2/tests/analyzing_entities.test.yaml +++ b/language/v2/tests/analyzing_entities.test.yaml @@ -1,4 +1,4 @@ -# Copyright 2022 Google LLC +# Copyright 2023 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From eb7810fe539c4675d382bb04a6db045395a31de1 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Tue, 22 Aug 2023 15:38:19 -0700 Subject: [PATCH 11/23] Update based on comments --- language/v2/language_entities_gcs.py | 1 - language/v2/language_entities_text.py | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index c83451b7755..227334950e8 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -58,7 +58,6 @@ def sample_analyze_entities(gcs_content_uri): request={"document": document, "encoding_type": encoding_type} ) - # Loop through entitites returned from the API for entity in response.entities: print(f"Representative name for the entity: {entity.name}") diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index ffb52f70055..d104dcc60a7 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -29,16 +29,14 @@ def sample_analyze_entities(text_content): """ - Analyzing Entities in a String + Analyzes Entities in a String Args: - text_content The text content to analyze + text_content: The text content to analyze """ client = language_v2.LanguageServiceClient() - # text_content = 'California is a state.' - # Available types: PLAIN_TEXT, HTML type_ = language_v2.Document.Type.PLAIN_TEXT @@ -55,7 +53,6 @@ def sample_analyze_entities(text_content): request={"document": document, "encoding_type": encoding_type} ) - # Loop through entitites returned from the API for entity in response.entities: print(f"Representative name for the entity: {entity.name}") From ed5451c213054ece81ec47a6c15c67fba7d599bf Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Tue, 22 Aug 2023 15:58:33 -0700 Subject: [PATCH 12/23] Update based on comments --- language/v2/language_entities_gcs.py | 2 +- language/v2/language_entities_text.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index 227334950e8..139fc79e8ec 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -27,7 +27,7 @@ from google.cloud import language_v2 -def sample_analyze_entities(gcs_content_uri): +def sample_analyze_entities(gcs_content_uri: str) -> None: """ Analyzes Entities in text file stored in Cloud Storage. diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index d104dcc60a7..7c5542eb7e0 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -27,7 +27,7 @@ from google.cloud import language_v2 -def sample_analyze_entities(text_content): +def sample_analyze_entities(text_content: str) -> None: """ Analyzes Entities in a String From 8403af1802b226ac8f55770952ce32d488e27967 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Tue, 22 Aug 2023 16:06:43 -0700 Subject: [PATCH 13/23] Update based on comments --- language/v2/language_entities_gcs.py | 10 ++++++---- language/v2/language_entities_text.py | 16 +++++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index 139fc79e8ec..35e12a41c3c 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -39,7 +39,7 @@ def sample_analyze_entities(gcs_content_uri: str) -> None: client = language_v2.LanguageServiceClient() # Available types: PLAIN_TEXT, HTML - type_ = language_v2.Document.Type.PLAIN_TEXT + document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT # Optional. If not specified, the language is automatically detected. # For list of supported languages: @@ -47,11 +47,12 @@ def sample_analyze_entities(gcs_content_uri: str) -> None: language_code = "en" document = { "gcs_content_uri": gcs_content_uri, - "type_": type_, + "type_": document_type_in_plain_text, "language_code": language_code, } - # Available values: NONE, UTF8, UTF16, UTF32 + # Available values: NONE, UTF8, UTF16, UTF32. + # See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType. encoding_type = language_v2.EncodingType.UTF8 response = client.analyze_entities( @@ -61,7 +62,8 @@ def sample_analyze_entities(gcs_content_uri: str) -> None: for entity in response.entities: print(f"Representative name for the entity: {entity.name}") - # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al + # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al. + # See https://cloud.google.com/natural-language/docs/reference/rest/v2/Entity#type. print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}") # Loop over the metadata associated with entity. diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index 7c5542eb7e0..bba1feea315 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -38,15 +38,20 @@ def sample_analyze_entities(text_content: str) -> None: client = language_v2.LanguageServiceClient() # Available types: PLAIN_TEXT, HTML - type_ = language_v2.Document.Type.PLAIN_TEXT + document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT # Optional. If not specified, the language is automatically detected. # For list of supported languages: # https://cloud.google.com/natural-language/docs/languages language_code = "en" - document = {"content": text_content, "type_": type_, "language_code": language_code} - - # Available values: NONE, UTF8, UTF16, UTF32 + document = { + "content": text_content, + "type_": document_type_in_plain_text, + "language_code": language_code, + } + + # Available values: NONE, UTF8, UTF16, UTF32. + # See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType. encoding_type = language_v2.EncodingType.UTF8 response = client.analyze_entities( @@ -56,7 +61,8 @@ def sample_analyze_entities(text_content: str) -> None: for entity in response.entities: print(f"Representative name for the entity: {entity.name}") - # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al + # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al. + # See https://cloud.google.com/natural-language/docs/reference/rest/v2/Entity#type. print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}") # Loop over the metadata associated with entity. From 7d9c234e50062ce6472bc1d975a19ca2b99c4644 Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 22:10:01 -0700 Subject: [PATCH 14/23] Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_gcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index 35e12a41c3c..d3b589ee5b8 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -33,7 +33,7 @@ def sample_analyze_entities(gcs_content_uri: str) -> None: Args: gcs_content_uri: Google Cloud Storage URI where the file content is located. - e.g. gs://[Your Bucket]/[Path to File] + e.g. gs://[Your Bucket]/[Path to File] """ client = language_v2.LanguageServiceClient() From f59aca38aac7e350f26c68082b354f4ee403ada0 Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 22:10:13 -0700 Subject: [PATCH 15/23] Update language/v2/language_entities_text.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index bba1feea315..0552357eec6 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -29,7 +29,7 @@ def sample_analyze_entities(text_content: str) -> None: """ - Analyzes Entities in a String + Analyzes Entities in a string. Args: text_content: The text content to analyze From f009cfd8db49e78efd1e546327052c9a4c530299 Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 22:10:47 -0700 Subject: [PATCH 16/23] Update language/v2/language_entities_text.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_text.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index 0552357eec6..f20d7c75dd4 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -78,9 +78,8 @@ def sample_analyze_entities(text_content: str) -> None: # Get the mention type, e.g. PROPER for proper noun print( - "Mention type: {}".format( - language_v2.EntityMention.Type(mention.type_).name - ) + "Mention type:" + "{language_v2.EntityMention.Type(mention.type_).name}" ) # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. From 712082454d1305161669d1df54cf107a5c8bc7c0 Mon Sep 17 00:00:00 2001 From: Kornraphop K Date: Tue, 22 Aug 2023 22:10:55 -0700 Subject: [PATCH 17/23] Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> --- language/v2/language_entities_gcs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index d3b589ee5b8..628eb5cad1f 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -79,8 +79,8 @@ def sample_analyze_entities(gcs_content_uri: str) -> None: # Get the mention type, e.g. PROPER for proper noun print( - 'Mention type:' - f' {language_v2.EntityMention.Type(mention.type_).name}' + "Mention type:" + f" {language_v2.EntityMention.Type(mention.type_).name}" ) # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. From 1b8926d9891f935391177f515cbcebfba68d916c Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Wed, 23 Aug 2023 11:40:11 -0700 Subject: [PATCH 18/23] Add code samples for language_classify --- language/v2/language_classify_gcs.py | 84 ++++++++++++++++++ language/v2/language_classify_text.py | 85 +++++++++++++++++++ .../v2/tests/classifying_content.test.yaml | 64 ++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 language/v2/language_classify_gcs.py create mode 100644 language/v2/language_classify_text.py create mode 100644 language/v2/tests/classifying_content.test.yaml diff --git a/language/v2/language_classify_gcs.py b/language/v2/language_classify_gcs.py new file mode 100644 index 00000000000..205f8b494a3 --- /dev/null +++ b/language/v2/language_classify_gcs.py @@ -0,0 +1,84 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# DO NOT EDIT! This is a generated sample ("Request", "language_classify_gcs") + +# To install the latest published package dependency, execute the following: +# pip install google-cloud-language + +# sample-metadata +# title: Classify Content (GCS) +# description: Classifying Content in text file stored in Cloud Storage +# usage: python3 samples/v2/language_classify_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/classify-entertainment.txt"] + +# [START language_classify_gcs] +from google.cloud import language_v2 + + +def sample_classify_text(gcs_content_uri: str) -> None: + """ + Classifies Content in text file stored in Cloud Storage + + Args: + gcs_content_uri: Google Cloud Storage URI where the file content is located. + e.g. gs://[Your Bucket]/[Path to File]. The text file must include at least 20 words. + """ + + client = language_v2.LanguageServiceClient() + + # Available types: PLAIN_TEXT, HTML + document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT + + # Optional. If not specified, the language is automatically detected. + # For list of supported languages: + # https://cloud.google.com/natural-language/docs/languages + language_code = "en" + document = { + "gcs_content_uri": gcs_content_uri, + "type_": document_type_in_plain_text, + "language_code": language_code, + } + + response = client.classify_text(request={"document": document}) + # Loop through classified categories returned from the API + for category in response.categories: + # Get the name of the category representing the document. + # See the predefined taxonomy of categories: + # https://cloud.google.com/natural-language/docs/categories + print(f"Category name: {category.name}") + # Get the confidence. Number representing how certain the classifier + # is that this category represents the provided text. + print(f"Confidence: {category.confidence}") + + +# [END language_classify_gcs] + + +def main(): + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument( + "--gcs_content_uri", + type=str, + default="gs://cloud-samples-data/language/classify-entertainment.txt", + ) + args = parser.parse_args() + + sample_classify_text(args.gcs_content_uri) + + +if __name__ == "__main__": + main() diff --git a/language/v2/language_classify_text.py b/language/v2/language_classify_text.py new file mode 100644 index 00000000000..caaeecff5be --- /dev/null +++ b/language/v2/language_classify_text.py @@ -0,0 +1,85 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# DO NOT EDIT! This is a generated sample ("Request", "language_classify_text") + +# To install the latest published package dependency, execute the following: +# pip install google-cloud-language + +# sample-metadata +# title: Classify Content +# description: Classifying Content in a String +# usage: python3 samples/v2/language_classify_text.py [--text_content "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows."] + +# [START language_classify_text] +from google.cloud import language_v2 + + +def sample_classify_text(text_content): + """ + Classifies Content in a String + + Args: + text_content: The text content to analyze. + """ + + client = language_v2.LanguageServiceClient() + + # Available types: PLAIN_TEXT, HTML + document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT + + # Optional. If not specified, the language is automatically detected. + # For list of supported languages: + # https://cloud.google.com/natural-language/docs/languages + language_code = "en" + document = { + "content": text_content, + "type_": document_type_in_plain_text, + "language_code": language_code, + } + + response = client.classify_text( + request={"document": document} + ) + # Loop through classified categories returned from the API + for category in response.categories: + # Get the name of the category representing the document. + # See the predefined taxonomy of categories: + # https://cloud.google.com/natural-language/docs/categories + print(f"Category name: {category.name}") + # Get the confidence. Number representing how certain the classifier + # is that this category represents the provided text. + print(f"Confidence: {category.confidence}") + + +# [END language_classify_text] + + +def main(): + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument( + "--text_content", + type=str, + default="That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.", + ) + args = parser.parse_args() + + sample_classify_text(args.text_content) + + +if __name__ == "__main__": + main() diff --git a/language/v2/tests/classifying_content.test.yaml b/language/v2/tests/classifying_content.test.yaml new file mode 100644 index 00000000000..75b802a5006 --- /dev/null +++ b/language/v2/tests/classifying_content.test.yaml @@ -0,0 +1,64 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +type: test/samples +schema_version: 1 +test: + suites: + - name: "Classifying Content [code sample tests]" + cases: + + - name: language_classify_text - Classifying Content of a text string (default value) + spec: + # Default value: "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows." + - call: {sample: language_classify_text} + - assert_contains_any: + - {literal: "TV"} + - {literal: "Movies"} + - {literal: "Entertainment"} + + - name: language_classify_text - Classifying Content of a text string (*custom value*) + spec: + # Custom value: "Dungeons and dragons and loot, oh my!" + - call: + sample: language_classify_text + params: + text_content: {literal: "Dungeons and dragons and loot, oh my!"} + - assert_contains_any: + - {literal: "Games"} + - {literal: "Roleplaying"} + - {literal: "Computer"} + + - name: language_classify_gcs - Classifying Content of text file in GCS (default value) + spec: + # Default value: gs://cloud-samples-data/language/classify-entertainment.txt + # => "This is about film and movies and television and acting and movie theatres and theatre and drama and entertainment and the arts." + - call: {sample: language_classify_gcs} + - assert_contains_any: + - {literal: "TV"} + - {literal: "Movies"} + - {literal: "Entertainment"} + + - name: language_classify_gcs - Classifying Content of text file in GCS (*custom value*) + spec: + # Use different file: gs://cloud-samples-data/language/android.txt + # => "Android is a mobile operating system developed by Google, based on the Linux kernel and..." + - call: + sample: language_classify_gcs + params: + gcs_content_uri: + literal: "gs://cloud-samples-data/language/android.txt" + - assert_contains_any: + - {literal: "Mobile"} + - {literal: "Phone"} + - {literal: "Internet"} From 1e2057143a157eda63ba473e9d11305bc5f5a8ed Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Wed, 23 Aug 2023 11:45:16 -0700 Subject: [PATCH 19/23] Add code samples for language_sentiment --- language/v2/language_classify_gcs.py | 2 +- language/v2/language_classify_text.py | 4 +- language/v2/language_sentiment_gcs.py | 94 +++++++++++++++++++ language/v2/language_sentiment_text.py | 91 ++++++++++++++++++ .../v2/tests/analyzing_sentiment.test.yaml | 87 +++++++++++++++++ 5 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 language/v2/language_sentiment_gcs.py create mode 100644 language/v2/language_sentiment_text.py create mode 100644 language/v2/tests/analyzing_sentiment.test.yaml diff --git a/language/v2/language_classify_gcs.py b/language/v2/language_classify_gcs.py index 205f8b494a3..7603cf8717e 100644 --- a/language/v2/language_classify_gcs.py +++ b/language/v2/language_classify_gcs.py @@ -29,7 +29,7 @@ def sample_classify_text(gcs_content_uri: str) -> None: """ - Classifies Content in text file stored in Cloud Storage + Classifies Content in text file stored in Cloud Storage. Args: gcs_content_uri: Google Cloud Storage URI where the file content is located. diff --git a/language/v2/language_classify_text.py b/language/v2/language_classify_text.py index caaeecff5be..d18577f0c20 100644 --- a/language/v2/language_classify_text.py +++ b/language/v2/language_classify_text.py @@ -27,9 +27,9 @@ from google.cloud import language_v2 -def sample_classify_text(text_content): +def sample_classify_text(text_content: str) -> None: """ - Classifies Content in a String + Classifies Content in a string. Args: text_content: The text content to analyze. diff --git a/language/v2/language_sentiment_gcs.py b/language/v2/language_sentiment_gcs.py new file mode 100644 index 00000000000..ad368c79a2a --- /dev/null +++ b/language/v2/language_sentiment_gcs.py @@ -0,0 +1,94 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# DO NOT EDIT! This is a generated sample ("Request", "language_sentiment_gcs") + +# To install the latest published package dependency, execute the following: +# pip install google-cloud-language + +# sample-metadata +# title: Analyzing Sentiment (GCS) +# description: Analyzing Sentiment in text file stored in Cloud Storage +# usage: python3 samples/v2/language_sentiment_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/sentiment-positive.txt"] + +# [START language_sentiment_gcs] +from google.cloud import language_v2 + + +def sample_analyze_sentiment(gcs_content_uri: str) -> None: + """ + Analyzes Sentiment in text file stored in Cloud Storage. + + Args: + gcs_content_uri: Google Cloud Storage URI where the file content is located. + e.g. gs://[Your Bucket]/[Path to File] + """ + + client = language_v2.LanguageServiceClient() + + # Available types: PLAIN_TEXT, HTML + document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT + + # Optional. If not specified, the language is automatically detected. + # For list of supported languages: + # https://cloud.google.com/natural-language/docs/languages + language_code = "en" + document = { + "gcs_content_uri": gcs_content_uri, + "type_": document_type_in_plain_text, + "language_code": language_code, + } + + # Available values: NONE, UTF8, UTF16, UTF32 + # See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType. + encoding_type = language_v2.EncodingType.UTF8 + + response = client.analyze_sentiment( + request={"document": document, "encoding_type": encoding_type} + ) + # Get overall sentiment of the input document + print(f"Document sentiment score: {response.document_sentiment.score}") + print(f"Document sentiment magnitude: {response.document_sentiment.magnitude}") + # Get sentiment for all sentences in the document + for sentence in response.sentences: + print(f"Sentence text: {sentence.text.content}") + print(f"Sentence sentiment score: {sentence.sentiment.score}") + print(f"Sentence sentiment magnitude: {sentence.sentiment.magnitude}") + + # Get the language of the text, which will be the same as + # the language specified in the request or, if not specified, + # the automatically-detected language. + print(f"Language of the text: {response.language}") + + +# [END language_sentiment_gcs] + + +def main(): + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument( + "--gcs_content_uri", + type=str, + default="gs://cloud-samples-data/language/sentiment-positive.txt", + ) + args = parser.parse_args() + + sample_analyze_sentiment(args.gcs_content_uri) + + +if __name__ == "__main__": + main() diff --git a/language/v2/language_sentiment_text.py b/language/v2/language_sentiment_text.py new file mode 100644 index 00000000000..5ba26de72b8 --- /dev/null +++ b/language/v2/language_sentiment_text.py @@ -0,0 +1,91 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# DO NOT EDIT! This is a generated sample ("Request", "language_sentiment_text") + +# To install the latest published package dependency, execute the following: +# pip install google-cloud-language + +# sample-metadata +# title: Analyzing Sentiment +# description: Analyzing Sentiment in a String +# usage: python3 samples/v2/language_sentiment_text.py [--text_content "I am so happy and joyful."] + +# [START language_sentiment_text] +from google.cloud import language_v2 + + +def sample_analyze_sentiment(text_content: str) -> None: + """ + Analyzes Sentiment in a string. + + Args: + text_content: The text content to analyze. + """ + + client = language_v2.LanguageServiceClient() + + # text_content = 'I am so happy and joyful.' + + # Available types: PLAIN_TEXT, HTML + document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT + + # Optional. If not specified, the language is automatically detected. + # For list of supported languages: + # https://cloud.google.com/natural-language/docs/languages + language_code = "en" + document = { + "content": text_content, + "type_": document_type_in_plain_text, + "language_code": language_code, + } + + # Available values: NONE, UTF8, UTF16, UTF32 + # See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType. + encoding_type = language_v2.EncodingType.UTF8 + + response = client.analyze_sentiment( + request={"document": document, "encoding_type": encoding_type} + ) + # Get overall sentiment of the input document + print(f"Document sentiment score: {response.document_sentiment.score}") + print(f"Document sentiment magnitude: {response.document_sentiment.magnitude}") + # Get sentiment for all sentences in the document + for sentence in response.sentences: + print(f"Sentence text: {sentence.text.content}") + print(f"Sentence sentiment score: {sentence.sentiment.score}") + print(f"Sentence sentiment magnitude: {sentence.sentiment.magnitude}") + + # Get the language of the text, which will be the same as + # the language specified in the request or, if not specified, + # the automatically-detected language. + print(f"Language of the text: {response.language}") + + +# [END language_sentiment_text] + + +def main(): + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument("--text_content", type=str, default="I am so happy and joyful.") + args = parser.parse_args() + + sample_analyze_sentiment(args.text_content) + + +if __name__ == "__main__": + main() diff --git a/language/v2/tests/analyzing_sentiment.test.yaml b/language/v2/tests/analyzing_sentiment.test.yaml new file mode 100644 index 00000000000..63417692698 --- /dev/null +++ b/language/v2/tests/analyzing_sentiment.test.yaml @@ -0,0 +1,87 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +type: test/samples +schema_version: 1 +test: + suites: + - name: "Analyzing Sentiment [code sample tests]" + cases: + + - name: language_sentiment_text - Analyzing the sentiment of a text string (default value) + spec: + # Default value: "I am so happy and joyful." + - call: {sample: language_sentiment_text} + - assert_contains: + - {literal: "Document sentiment score: 0."} + - {literal: "Document sentiment magnitude: 0."} + - {literal: "Sentence text: I am so happy and joyful."} + - {literal: "Sentence sentiment score: 0."} + - {literal: "Sentence sentiment magnitude: 0."} + - {literal: "Language of the text: en"} + # There should be no negative sentiment scores for this value. + - assert_not_contains: + - {literal: "Document sentiment score: -0."} + - {literal: "Sentence sentiment score: -0."} + + - name: language_sentiment_text - Analyzing the sentiment of a text string (*custom value*) + spec: + # Custom value: "I am very happy. I am angry and sad." + - call: + sample: language_sentiment_text + params: + text_content: {literal: "I am very happy. I am angry and sad."} + - assert_contains: + - {literal: "Sentence text: I am very happy"} + - {literal: "Sentence sentiment score: 0."} + - {literal: "Sentence text: I am angry and sad"} + - {literal: "Sentence sentiment score: -0."} + - {literal: "Language of the text: en"} + + - name: language_sentiment_gcs - Analyzing the sentiment of text file in GCS (default value) + spec: + # Default value: gs://cloud-samples-data/language/sentiment-positive.txt + # => "I am so happy and joyful." + - call: {sample: language_sentiment_gcs} + - assert_contains: + - {literal: "Document sentiment score: 0."} + - {literal: "Document sentiment magnitude: 0."} + - {literal: "Sentence text: I am so happy and joyful."} + - {literal: "Sentence sentiment score: 0."} + - {literal: "Sentence sentiment magnitude: 0."} + - {literal: "Language of the text: en"} + # There should be no negative sentiment scores for this value. + - assert_not_contains: + - {literal: "Document sentiment score: -0."} + - {literal: "Sentence sentiment score: -0."} + + - name: language_sentiment_gcs - Analyzing the sentiment of text file in GCS (*custom value*) + spec: + # Use different file: gs://cloud-samples-data/language/sentiment-negative.txt + # => "I am so sad and upset." + - call: + sample: language_sentiment_gcs + params: + gcs_content_uri: + literal: "gs://cloud-samples-data/language/sentiment-negative.txt" + - assert_contains: + - {literal: "Document sentiment score: -0."} + - {literal: "Document sentiment magnitude: 0."} + - {literal: "Sentence text: I am so sad and upset."} + - {literal: "Sentence sentiment score: -0."} + - {literal: "Sentence sentiment magnitude: 0."} + - {literal: "Language of the text: en"} + # There should be no positive sentiment scores for this value. + - assert_not_contains: + - {literal: "Document sentiment score: 0."} + - {literal: "Sentence sentiment score: 0."} From a3ca4b2f81eda544dc2cfbca70bce275d98e0d44 Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Wed, 23 Aug 2023 14:07:44 -0700 Subject: [PATCH 20/23] Fix minor bugs --- language/v2/language_entities_text.py | 2 +- language/v2/language_sentiment_gcs.py | 2 +- language/v2/language_sentiment_text.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index f20d7c75dd4..6d4f1243e7b 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -79,7 +79,7 @@ def sample_analyze_entities(text_content: str) -> None: # Get the mention type, e.g. PROPER for proper noun print( "Mention type:" - "{language_v2.EntityMention.Type(mention.type_).name}" + f"{language_v2.EntityMention.Type(mention.type_).name}" ) # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. diff --git a/language/v2/language_sentiment_gcs.py b/language/v2/language_sentiment_gcs.py index ad368c79a2a..98eafd02164 100644 --- a/language/v2/language_sentiment_gcs.py +++ b/language/v2/language_sentiment_gcs.py @@ -70,7 +70,7 @@ def sample_analyze_sentiment(gcs_content_uri: str) -> None: # Get the language of the text, which will be the same as # the language specified in the request or, if not specified, # the automatically-detected language. - print(f"Language of the text: {response.language}") + print(f"Language of the text: {response.language_code}") # [END language_sentiment_gcs] diff --git a/language/v2/language_sentiment_text.py b/language/v2/language_sentiment_text.py index 5ba26de72b8..1ba25128f44 100644 --- a/language/v2/language_sentiment_text.py +++ b/language/v2/language_sentiment_text.py @@ -71,7 +71,7 @@ def sample_analyze_sentiment(text_content: str) -> None: # Get the language of the text, which will be the same as # the language specified in the request or, if not specified, # the automatically-detected language. - print(f"Language of the text: {response.language}") + print(f"Language of the text: {response.language_code}") # [END language_sentiment_text] From d94641518d9ab6130a9be10812d6cd424b28899b Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Wed, 23 Aug 2023 15:05:30 -0700 Subject: [PATCH 21/23] Update based on comments. --- language/v2/language_classify_gcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/language/v2/language_classify_gcs.py b/language/v2/language_classify_gcs.py index 7603cf8717e..31b7633d3dd 100644 --- a/language/v2/language_classify_gcs.py +++ b/language/v2/language_classify_gcs.py @@ -33,7 +33,7 @@ def sample_classify_text(gcs_content_uri: str) -> None: Args: gcs_content_uri: Google Cloud Storage URI where the file content is located. - e.g. gs://[Your Bucket]/[Path to File]. The text file must include at least 20 words. + e.g. gs://[Your Bucket]/[Path to File]. """ client = language_v2.LanguageServiceClient() From 6be6119ce6fa3e00f1dacbd70f7d1f84ff30667a Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Thu, 24 Aug 2023 11:54:56 -0700 Subject: [PATCH 22/23] Update based on the style guide --- language/v2/language_classify_gcs.py | 25 +--- language/v2/language_classify_gcs_test.py | 30 +++++ language/v2/language_classify_text.py | 29 +---- language/v2/language_classify_text_test.py | 30 +++++ language/v2/language_entities_gcs.py | 28 +---- language/v2/language_entities_gcs_test.py | 33 ++++++ language/v2/language_entities_text.py | 24 +--- language/v2/language_entities_text_test.py | 33 ++++++ language/v2/language_sentiment_gcs.py | 25 +--- language/v2/language_sentiment_gcs_test.py | 33 ++++++ language/v2/language_sentiment_text.py | 19 +-- language/v2/language_sentiment_text_test.py | 33 ++++++ language/v2/noxfile_config.py | 42 +++++++ .../v2/tests/analyzing_entities.test.yaml | 108 ------------------ .../v2/tests/analyzing_sentiment.test.yaml | 87 -------------- .../v2/tests/classifying_content.test.yaml | 64 ----------- 16 files changed, 251 insertions(+), 392 deletions(-) create mode 100644 language/v2/language_classify_gcs_test.py create mode 100644 language/v2/language_classify_text_test.py create mode 100644 language/v2/language_entities_gcs_test.py create mode 100644 language/v2/language_entities_text_test.py create mode 100644 language/v2/language_sentiment_gcs_test.py create mode 100644 language/v2/language_sentiment_text_test.py create mode 100644 language/v2/noxfile_config.py delete mode 100644 language/v2/tests/analyzing_entities.test.yaml delete mode 100644 language/v2/tests/analyzing_sentiment.test.yaml delete mode 100644 language/v2/tests/classifying_content.test.yaml diff --git a/language/v2/language_classify_gcs.py b/language/v2/language_classify_gcs.py index 31b7633d3dd..2e2cb99d0b2 100644 --- a/language/v2/language_classify_gcs.py +++ b/language/v2/language_classify_gcs.py @@ -13,21 +13,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -# DO NOT EDIT! This is a generated sample ("Request", "language_classify_gcs") - # To install the latest published package dependency, execute the following: # pip install google-cloud-language # sample-metadata # title: Classify Content (GCS) # description: Classifying Content in text file stored in Cloud Storage -# usage: python3 samples/v2/language_classify_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/classify-entertainment.txt"] # [START language_classify_gcs] from google.cloud import language_v2 -def sample_classify_text(gcs_content_uri: str) -> None: +def sample_classify_text( + gcs_content_uri: str = "gs://cloud-samples-data/language/classify-entertainment.txt", +) -> None: """ Classifies Content in text file stored in Cloud Storage. @@ -64,21 +63,3 @@ def sample_classify_text(gcs_content_uri: str) -> None: # [END language_classify_gcs] - - -def main(): - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument( - "--gcs_content_uri", - type=str, - default="gs://cloud-samples-data/language/classify-entertainment.txt", - ) - args = parser.parse_args() - - sample_classify_text(args.gcs_content_uri) - - -if __name__ == "__main__": - main() diff --git a/language/v2/language_classify_gcs_test.py b/language/v2/language_classify_gcs_test.py new file mode 100644 index 00000000000..aae7bdf24a3 --- /dev/null +++ b/language/v2/language_classify_gcs_test.py @@ -0,0 +1,30 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import language_classify_gcs + + +def test_sample_classify_text_gcs(capsys: ...) -> None: + assert os.environ["GOOGLE_CLOUD_PROJECT"] != "" + + language_classify_gcs.sample_classify_text() + captured = capsys.readouterr() + assert ( + "TV" in captured.out + or "Movies" in captured.out + or "Entertainment" in captured.out + ) diff --git a/language/v2/language_classify_text.py b/language/v2/language_classify_text.py index d18577f0c20..8c51d03ee5d 100644 --- a/language/v2/language_classify_text.py +++ b/language/v2/language_classify_text.py @@ -13,21 +13,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -# DO NOT EDIT! This is a generated sample ("Request", "language_classify_text") - # To install the latest published package dependency, execute the following: # pip install google-cloud-language # sample-metadata # title: Classify Content # description: Classifying Content in a String -# usage: python3 samples/v2/language_classify_text.py [--text_content "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows."] # [START language_classify_text] from google.cloud import language_v2 -def sample_classify_text(text_content: str) -> None: +def sample_classify_text( + text_content: str = "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.", +) -> None: """ Classifies Content in a string. @@ -50,9 +49,7 @@ def sample_classify_text(text_content: str) -> None: "language_code": language_code, } - response = client.classify_text( - request={"document": document} - ) + response = client.classify_text(request={"document": document}) # Loop through classified categories returned from the API for category in response.categories: # Get the name of the category representing the document. @@ -65,21 +62,3 @@ def sample_classify_text(text_content: str) -> None: # [END language_classify_text] - - -def main(): - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument( - "--text_content", - type=str, - default="That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.", - ) - args = parser.parse_args() - - sample_classify_text(args.text_content) - - -if __name__ == "__main__": - main() diff --git a/language/v2/language_classify_text_test.py b/language/v2/language_classify_text_test.py new file mode 100644 index 00000000000..75fd9a3acbe --- /dev/null +++ b/language/v2/language_classify_text_test.py @@ -0,0 +1,30 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import language_classify_text + + +def test_sample_classify_text(capsys: ...) -> None: + assert os.environ["GOOGLE_CLOUD_PROJECT"] != "" + + language_classify_text.sample_classify_text() + captured = capsys.readouterr() + assert ( + "TV" in captured.out + or "Movies" in captured.out + or "Entertainment" in captured.out + ) diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index 628eb5cad1f..ab26dfecbe8 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -13,21 +13,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -# DO NOT EDIT! This is a generated sample ("Request", "language_entities_gcs") - # To install the latest published package dependency, execute the following: # pip install google-cloud-language # sample-metadata # title: Analyzing Entities (GCS) # description: Analyzing Entities in text file stored in Cloud Storage -# usage: python3 samples/v2/language_entities_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/entity.txt"] # [START language_entities_gcs] from google.cloud import language_v2 -def sample_analyze_entities(gcs_content_uri: str) -> None: +def sample_analyze_entities( + gcs_content_uri: str = "gs://cloud-samples-data/language/entity.txt", +) -> None: """ Analyzes Entities in text file stored in Cloud Storage. @@ -79,8 +78,7 @@ def sample_analyze_entities(gcs_content_uri: str) -> None: # Get the mention type, e.g. PROPER for proper noun print( - "Mention type:" - f" {language_v2.EntityMention.Type(mention.type_).name}" + "Mention type:" f" {language_v2.EntityMention.Type(mention.type_).name}" ) # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. @@ -93,21 +91,3 @@ def sample_analyze_entities(gcs_content_uri: str) -> None: # [END language_entities_gcs] - - -def main(): - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument( - "--gcs_content_uri", - type=str, - default="gs://cloud-samples-data/language/entity.txt", - ) - args = parser.parse_args() - - sample_analyze_entities(args.gcs_content_uri) - - -if __name__ == "__main__": - main() diff --git a/language/v2/language_entities_gcs_test.py b/language/v2/language_entities_gcs_test.py new file mode 100644 index 00000000000..572b5104c56 --- /dev/null +++ b/language/v2/language_entities_gcs_test.py @@ -0,0 +1,33 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import language_entities_gcs + + +def test_sample_analyze_entities_gcs(capsys: ...) -> None: + assert os.environ["GOOGLE_CLOUD_PROJECT"] != "" + + language_entities_gcs.sample_analyze_entities() + captured = capsys.readouterr() + assert ( + "Representative name for the entity: California" in captured.out + and "Entity type: LOCATION" in captured.out + and "Mention text: California" in captured.out + and "Mention type: PROPER" in captured.out + and "Probability score: 0." in captured.out + and "Language of the text: en" in captured.out + ) diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index 6d4f1243e7b..587985d04f3 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -13,21 +13,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -# DO NOT EDIT! This is a generated sample ("Request", "language_entities_text") - # To install the latest published package dependency, execute the following: # pip install google-cloud-language # sample-metadata # title: Analyzing Entities # description: Analyzing Entities in a String -# usage: python3 samples/v2/language_entities_text.py [--text_content "California is a state."] # [START language_entities_text] from google.cloud import language_v2 -def sample_analyze_entities(text_content: str) -> None: +def sample_analyze_entities(text_content: str = "California is a state.") -> None: """ Analyzes Entities in a string. @@ -77,10 +74,7 @@ def sample_analyze_entities(text_content: str) -> None: print(f"Mention text: {mention.text.content}") # Get the mention type, e.g. PROPER for proper noun - print( - "Mention type:" - f"{language_v2.EntityMention.Type(mention.type_).name}" - ) + print(f"Mention type: {language_v2.EntityMention.Type(mention.type_).name}") # Get the probability score associated with the first mention of the entity in the (0, 1.0] range. print(f"Probability score: {mention.probability}") @@ -92,17 +86,3 @@ def sample_analyze_entities(text_content: str) -> None: # [END language_entities_text] - - -def main(): - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument("--text_content", type=str, default="California is a state.") - args = parser.parse_args() - - sample_analyze_entities(args.text_content) - - -if __name__ == "__main__": - main() diff --git a/language/v2/language_entities_text_test.py b/language/v2/language_entities_text_test.py new file mode 100644 index 00000000000..3c1982874ab --- /dev/null +++ b/language/v2/language_entities_text_test.py @@ -0,0 +1,33 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import language_entities_text + + +def test_sample_analyze_entities_text(capsys: ...) -> None: + assert os.environ["GOOGLE_CLOUD_PROJECT"] != "" + + language_entities_text.sample_analyze_entities() + captured = capsys.readouterr() + assert ( + "Representative name for the entity: California" in captured.out + and "Entity type: LOCATION" in captured.out + and "Mention text: California" in captured.out + and "Mention type: PROPER" in captured.out + and "Probability score: 0." in captured.out + and "Language of the text: en" in captured.out + ) diff --git a/language/v2/language_sentiment_gcs.py b/language/v2/language_sentiment_gcs.py index 98eafd02164..19a7cb878b6 100644 --- a/language/v2/language_sentiment_gcs.py +++ b/language/v2/language_sentiment_gcs.py @@ -13,21 +13,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -# DO NOT EDIT! This is a generated sample ("Request", "language_sentiment_gcs") - # To install the latest published package dependency, execute the following: # pip install google-cloud-language # sample-metadata # title: Analyzing Sentiment (GCS) # description: Analyzing Sentiment in text file stored in Cloud Storage -# usage: python3 samples/v2/language_sentiment_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/sentiment-positive.txt"] # [START language_sentiment_gcs] from google.cloud import language_v2 -def sample_analyze_sentiment(gcs_content_uri: str) -> None: +def sample_analyze_sentiment( + gcs_content_uri: str = "gs://cloud-samples-data/language/sentiment-positive.txt", +) -> None: """ Analyzes Sentiment in text file stored in Cloud Storage. @@ -74,21 +73,3 @@ def sample_analyze_sentiment(gcs_content_uri: str) -> None: # [END language_sentiment_gcs] - - -def main(): - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument( - "--gcs_content_uri", - type=str, - default="gs://cloud-samples-data/language/sentiment-positive.txt", - ) - args = parser.parse_args() - - sample_analyze_sentiment(args.gcs_content_uri) - - -if __name__ == "__main__": - main() diff --git a/language/v2/language_sentiment_gcs_test.py b/language/v2/language_sentiment_gcs_test.py new file mode 100644 index 00000000000..0d6ae673905 --- /dev/null +++ b/language/v2/language_sentiment_gcs_test.py @@ -0,0 +1,33 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import language_sentiment_gcs + + +def test_sample_analyze_sentiment_gcs(capsys: ...) -> None: + assert os.environ["GOOGLE_CLOUD_PROJECT"] != "" + + language_sentiment_gcs.sample_analyze_sentiment() + captured = capsys.readouterr() + assert ( + "Document sentiment score: 0." in captured.out + and "Document sentiment magnitude: 0." in captured.out + and "Sentence text: I am so happy and joyful." in captured.out + and "Sentence sentiment score: 0." in captured.out + and "Sentence sentiment magnitude: 0." in captured.out + and "Language of the text: en" in captured.out + ) diff --git a/language/v2/language_sentiment_text.py b/language/v2/language_sentiment_text.py index 1ba25128f44..9aba0640cfb 100644 --- a/language/v2/language_sentiment_text.py +++ b/language/v2/language_sentiment_text.py @@ -13,21 +13,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -# DO NOT EDIT! This is a generated sample ("Request", "language_sentiment_text") - # To install the latest published package dependency, execute the following: # pip install google-cloud-language # sample-metadata # title: Analyzing Sentiment # description: Analyzing Sentiment in a String -# usage: python3 samples/v2/language_sentiment_text.py [--text_content "I am so happy and joyful."] # [START language_sentiment_text] from google.cloud import language_v2 -def sample_analyze_sentiment(text_content: str) -> None: +def sample_analyze_sentiment(text_content: str = "I am so happy and joyful.") -> None: """ Analyzes Sentiment in a string. @@ -75,17 +72,3 @@ def sample_analyze_sentiment(text_content: str) -> None: # [END language_sentiment_text] - - -def main(): - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument("--text_content", type=str, default="I am so happy and joyful.") - args = parser.parse_args() - - sample_analyze_sentiment(args.text_content) - - -if __name__ == "__main__": - main() diff --git a/language/v2/language_sentiment_text_test.py b/language/v2/language_sentiment_text_test.py new file mode 100644 index 00000000000..7d8ad6c51ce --- /dev/null +++ b/language/v2/language_sentiment_text_test.py @@ -0,0 +1,33 @@ +# +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import language_sentiment_text + + +def test_sample_analyze_sentiment_text(capsys: ...) -> None: + assert os.environ["GOOGLE_CLOUD_PROJECT"] != "" + + language_sentiment_text.sample_analyze_sentiment() + captured = capsys.readouterr() + assert ( + "Document sentiment score: 0." in captured.out + and "Document sentiment magnitude: 0." in captured.out + and "Sentence text: I am so happy and joyful." in captured.out + and "Sentence sentiment score: 0." in captured.out + and "Sentence sentiment magnitude: 0." in captured.out + and "Language of the text: en" in captured.out + ) diff --git a/language/v2/noxfile_config.py b/language/v2/noxfile_config.py new file mode 100644 index 00000000000..38a32880121 --- /dev/null +++ b/language/v2/noxfile_config.py @@ -0,0 +1,42 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be imported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.7", "3.8", "3.10"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/language/v2/tests/analyzing_entities.test.yaml b/language/v2/tests/analyzing_entities.test.yaml deleted file mode 100644 index e75f5002f44..00000000000 --- a/language/v2/tests/analyzing_entities.test.yaml +++ /dev/null @@ -1,108 +0,0 @@ -# Copyright 2023 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -type: test/samples -schema_version: 1 -test: - suites: - - name: "Analyzing Entities [code sample tests]" - cases: - - - name: language_entities_text - Analyzing the Entities of a text string (default value) - spec: - # Default value: "California is a state." - - call: {sample: language_entities_text} - - assert_contains: - - {literal: "Representative name for the entity: California"} - - {literal: "Entity type: LOCATION"} - - {literal: "Probability score:"} - - {literal: "Mention text: California"} - - {literal: "Mention type: PROPER"} - - {literal: "Mention text: state"} - - {literal: "Mention type: COMMON"} - - {literal: "Language of the text: en"} - - - name: language_entities_text - Analyzing the Entities of a text string (*custom value*) - spec: - # Custom value: "Alice is a person. She lives in California." - - call: - sample: language_entities_text - params: - text_content: {literal: "Alice is a person. She lives in California."} - - assert_contains: - - {literal: "Representative name for the entity: Alice"} - - {literal: "Entity type: PERSON"} - - {literal: "Mention text: Alice"} - - {literal: "Mention type: PROPER"} - - {literal: "Mention text: person"} - - {literal: "Mention type: COMMON"} - - {literal: "Representative name for the entity: California"} - - {literal: "Entity type: LOCATION"} - - {literal: "Language of the text: en"} - - - name: language_entities_text - Analyzing the Entities of a text string (*metadata attributes*) - spec: - # Try out some of the metadata attributes which should be available for dates, addresses, etc. - # In case fake (555) area code numbers don't work, using United States Naval Observatory number. - # Custom value: "I called 202-762-1401 on January 31, 2019 from 1600 Amphitheatre Parkway, Mountain View, CA." - - call: - sample: language_entities_text - params: - text_content: - literal: "I called 202-762-1401 on January 31, 2019 from 1600 Amphitheatre Parkway, Mountain View, CA." - # The results may change, but it's fair to say that at least one of the following types were detected: - - assert_contains_any: - - literal: "Entity type: DATE" - - literal: "Entity type: ADDRESS" - - literal: "Entity type: PHONE_NUMBER" - # Check that at least some of the supporting metadata for an entity was present in the response - - assert_contains_any: - - literal: "month: 1" - - literal: "day: 31" - - literal: "year: 2019" - - literal: "street_number: 1600" - - literal: "street_name: Amphitheatre Parkway" - - literal: "area_code: 202" - - literal: "number: 7621401" - - - name: language_entities_gcs - Analyzing the Entities of text file in GCS (default value) - spec: - # Default value: gs://cloud-samples-data/language/entity.txt - # => "California is a state." - - call: {sample: language_entities_gcs} - - assert_contains: - - {literal: "Representative name for the entity: California"} - - {literal: "Entity type: LOCATION"} - - {literal: "Probability score:"} - - {literal: "Mention text: California"} - - {literal: "Mention type: PROPER"} - - {literal: "Mention text: state"} - - {literal: "Mention type: COMMON"} - - {literal: "Language of the text: en"} - - - name: language_entities_gcs - Analyzing the Entities of text file in GCS (*custom value*) - spec: - # Use different file: gs://cloud-samples-data/language/entity-sentiment.txt - # => "Grapes are good. Bananas are bad." - - call: - sample: language_entities_gcs - params: - gcs_content_uri: - literal: "gs://cloud-samples-data/language/entity-sentiment.txt" - - assert_contains: - - {literal: "Representative name for the entity: Grapes"} - - {literal: "Mention text: Grapes"} - - {literal: "Mention type: COMMON"} - - {literal: "Representative name for the entity: Bananas"} - - {literal: "Mention text: Bananas"} - - {literal: "Language of the text: en"} diff --git a/language/v2/tests/analyzing_sentiment.test.yaml b/language/v2/tests/analyzing_sentiment.test.yaml deleted file mode 100644 index 63417692698..00000000000 --- a/language/v2/tests/analyzing_sentiment.test.yaml +++ /dev/null @@ -1,87 +0,0 @@ -# Copyright 2023 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -type: test/samples -schema_version: 1 -test: - suites: - - name: "Analyzing Sentiment [code sample tests]" - cases: - - - name: language_sentiment_text - Analyzing the sentiment of a text string (default value) - spec: - # Default value: "I am so happy and joyful." - - call: {sample: language_sentiment_text} - - assert_contains: - - {literal: "Document sentiment score: 0."} - - {literal: "Document sentiment magnitude: 0."} - - {literal: "Sentence text: I am so happy and joyful."} - - {literal: "Sentence sentiment score: 0."} - - {literal: "Sentence sentiment magnitude: 0."} - - {literal: "Language of the text: en"} - # There should be no negative sentiment scores for this value. - - assert_not_contains: - - {literal: "Document sentiment score: -0."} - - {literal: "Sentence sentiment score: -0."} - - - name: language_sentiment_text - Analyzing the sentiment of a text string (*custom value*) - spec: - # Custom value: "I am very happy. I am angry and sad." - - call: - sample: language_sentiment_text - params: - text_content: {literal: "I am very happy. I am angry and sad."} - - assert_contains: - - {literal: "Sentence text: I am very happy"} - - {literal: "Sentence sentiment score: 0."} - - {literal: "Sentence text: I am angry and sad"} - - {literal: "Sentence sentiment score: -0."} - - {literal: "Language of the text: en"} - - - name: language_sentiment_gcs - Analyzing the sentiment of text file in GCS (default value) - spec: - # Default value: gs://cloud-samples-data/language/sentiment-positive.txt - # => "I am so happy and joyful." - - call: {sample: language_sentiment_gcs} - - assert_contains: - - {literal: "Document sentiment score: 0."} - - {literal: "Document sentiment magnitude: 0."} - - {literal: "Sentence text: I am so happy and joyful."} - - {literal: "Sentence sentiment score: 0."} - - {literal: "Sentence sentiment magnitude: 0."} - - {literal: "Language of the text: en"} - # There should be no negative sentiment scores for this value. - - assert_not_contains: - - {literal: "Document sentiment score: -0."} - - {literal: "Sentence sentiment score: -0."} - - - name: language_sentiment_gcs - Analyzing the sentiment of text file in GCS (*custom value*) - spec: - # Use different file: gs://cloud-samples-data/language/sentiment-negative.txt - # => "I am so sad and upset." - - call: - sample: language_sentiment_gcs - params: - gcs_content_uri: - literal: "gs://cloud-samples-data/language/sentiment-negative.txt" - - assert_contains: - - {literal: "Document sentiment score: -0."} - - {literal: "Document sentiment magnitude: 0."} - - {literal: "Sentence text: I am so sad and upset."} - - {literal: "Sentence sentiment score: -0."} - - {literal: "Sentence sentiment magnitude: 0."} - - {literal: "Language of the text: en"} - # There should be no positive sentiment scores for this value. - - assert_not_contains: - - {literal: "Document sentiment score: 0."} - - {literal: "Sentence sentiment score: 0."} diff --git a/language/v2/tests/classifying_content.test.yaml b/language/v2/tests/classifying_content.test.yaml deleted file mode 100644 index 75b802a5006..00000000000 --- a/language/v2/tests/classifying_content.test.yaml +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2023 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -type: test/samples -schema_version: 1 -test: - suites: - - name: "Classifying Content [code sample tests]" - cases: - - - name: language_classify_text - Classifying Content of a text string (default value) - spec: - # Default value: "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows." - - call: {sample: language_classify_text} - - assert_contains_any: - - {literal: "TV"} - - {literal: "Movies"} - - {literal: "Entertainment"} - - - name: language_classify_text - Classifying Content of a text string (*custom value*) - spec: - # Custom value: "Dungeons and dragons and loot, oh my!" - - call: - sample: language_classify_text - params: - text_content: {literal: "Dungeons and dragons and loot, oh my!"} - - assert_contains_any: - - {literal: "Games"} - - {literal: "Roleplaying"} - - {literal: "Computer"} - - - name: language_classify_gcs - Classifying Content of text file in GCS (default value) - spec: - # Default value: gs://cloud-samples-data/language/classify-entertainment.txt - # => "This is about film and movies and television and acting and movie theatres and theatre and drama and entertainment and the arts." - - call: {sample: language_classify_gcs} - - assert_contains_any: - - {literal: "TV"} - - {literal: "Movies"} - - {literal: "Entertainment"} - - - name: language_classify_gcs - Classifying Content of text file in GCS (*custom value*) - spec: - # Use different file: gs://cloud-samples-data/language/android.txt - # => "Android is a mobile operating system developed by Google, based on the Linux kernel and..." - - call: - sample: language_classify_gcs - params: - gcs_content_uri: - literal: "gs://cloud-samples-data/language/android.txt" - - assert_contains_any: - - {literal: "Mobile"} - - {literal: "Phone"} - - {literal: "Internet"} From c99ec1717d06de6ccb1214d3e4495572cfbae32d Mon Sep 17 00:00:00 2001 From: "Kornraphop (Ken) Kawintiranon" Date: Thu, 24 Aug 2023 12:24:20 -0700 Subject: [PATCH 23/23] Update based on comments --- language/v2/language_classify_gcs.py | 2 -- language/v2/language_classify_gcs_test.py | 7 ++----- language/v2/language_classify_text.py | 2 -- language/v2/language_classify_text_test.py | 7 ++----- language/v2/language_entities_gcs.py | 2 -- language/v2/language_entities_gcs_test.py | 14 ++++++-------- language/v2/language_entities_text.py | 2 -- language/v2/language_entities_text_test.py | 14 ++++++-------- language/v2/language_sentiment_gcs.py | 2 -- language/v2/language_sentiment_gcs_test.py | 14 ++++++-------- language/v2/language_sentiment_text.py | 2 -- language/v2/language_sentiment_text_test.py | 14 ++++++-------- 12 files changed, 28 insertions(+), 54 deletions(-) diff --git a/language/v2/language_classify_gcs.py b/language/v2/language_classify_gcs.py index 2e2cb99d0b2..176ecf265a5 100644 --- a/language/v2/language_classify_gcs.py +++ b/language/v2/language_classify_gcs.py @@ -60,6 +60,4 @@ def sample_classify_text( # Get the confidence. Number representing how certain the classifier # is that this category represents the provided text. print(f"Confidence: {category.confidence}") - - # [END language_classify_gcs] diff --git a/language/v2/language_classify_gcs_test.py b/language/v2/language_classify_gcs_test.py index aae7bdf24a3..7daf31426b5 100644 --- a/language/v2/language_classify_gcs_test.py +++ b/language/v2/language_classify_gcs_test.py @@ -23,8 +23,5 @@ def test_sample_classify_text_gcs(capsys: ...) -> None: language_classify_gcs.sample_classify_text() captured = capsys.readouterr() - assert ( - "TV" in captured.out - or "Movies" in captured.out - or "Entertainment" in captured.out - ) + assert "Category name: " in captured.out + assert "Confidence: " in captured.out diff --git a/language/v2/language_classify_text.py b/language/v2/language_classify_text.py index 8c51d03ee5d..eea28b9c643 100644 --- a/language/v2/language_classify_text.py +++ b/language/v2/language_classify_text.py @@ -59,6 +59,4 @@ def sample_classify_text( # Get the confidence. Number representing how certain the classifier # is that this category represents the provided text. print(f"Confidence: {category.confidence}") - - # [END language_classify_text] diff --git a/language/v2/language_classify_text_test.py b/language/v2/language_classify_text_test.py index 75fd9a3acbe..bb0f4577e0e 100644 --- a/language/v2/language_classify_text_test.py +++ b/language/v2/language_classify_text_test.py @@ -23,8 +23,5 @@ def test_sample_classify_text(capsys: ...) -> None: language_classify_text.sample_classify_text() captured = capsys.readouterr() - assert ( - "TV" in captured.out - or "Movies" in captured.out - or "Entertainment" in captured.out - ) + assert "Category name: " in captured.out + assert "Confidence: " in captured.out diff --git a/language/v2/language_entities_gcs.py b/language/v2/language_entities_gcs.py index ab26dfecbe8..62af92f3abf 100644 --- a/language/v2/language_entities_gcs.py +++ b/language/v2/language_entities_gcs.py @@ -88,6 +88,4 @@ def sample_analyze_entities( # the language specified in the request or, if not specified, # the automatically-detected language. print(f"Language of the text: {response.language_code}") - - # [END language_entities_gcs] diff --git a/language/v2/language_entities_gcs_test.py b/language/v2/language_entities_gcs_test.py index 572b5104c56..5e7653b5ed5 100644 --- a/language/v2/language_entities_gcs_test.py +++ b/language/v2/language_entities_gcs_test.py @@ -23,11 +23,9 @@ def test_sample_analyze_entities_gcs(capsys: ...) -> None: language_entities_gcs.sample_analyze_entities() captured = capsys.readouterr() - assert ( - "Representative name for the entity: California" in captured.out - and "Entity type: LOCATION" in captured.out - and "Mention text: California" in captured.out - and "Mention type: PROPER" in captured.out - and "Probability score: 0." in captured.out - and "Language of the text: en" in captured.out - ) + assert "Representative name for the entity: " in captured.out + assert "Entity type: " in captured.out + assert "Mention text: " in captured.out + assert "Mention type: " in captured.out + assert "Probability score: " in captured.out + assert "Language of the text: " in captured.out diff --git a/language/v2/language_entities_text.py b/language/v2/language_entities_text.py index 587985d04f3..8f07aa49ca4 100644 --- a/language/v2/language_entities_text.py +++ b/language/v2/language_entities_text.py @@ -83,6 +83,4 @@ def sample_analyze_entities(text_content: str = "California is a state.") -> Non # the language specified in the request or, if not specified, # the automatically-detected language. print(f"Language of the text: {response.language_code}") - - # [END language_entities_text] diff --git a/language/v2/language_entities_text_test.py b/language/v2/language_entities_text_test.py index 3c1982874ab..48933347ae6 100644 --- a/language/v2/language_entities_text_test.py +++ b/language/v2/language_entities_text_test.py @@ -23,11 +23,9 @@ def test_sample_analyze_entities_text(capsys: ...) -> None: language_entities_text.sample_analyze_entities() captured = capsys.readouterr() - assert ( - "Representative name for the entity: California" in captured.out - and "Entity type: LOCATION" in captured.out - and "Mention text: California" in captured.out - and "Mention type: PROPER" in captured.out - and "Probability score: 0." in captured.out - and "Language of the text: en" in captured.out - ) + assert "Representative name for the entity: " in captured.out + assert "Entity type: " in captured.out + assert "Mention text: " in captured.out + assert "Mention type: " in captured.out + assert "Probability score: " in captured.out + assert "Language of the text: " in captured.out diff --git a/language/v2/language_sentiment_gcs.py b/language/v2/language_sentiment_gcs.py index 19a7cb878b6..27b24fccd94 100644 --- a/language/v2/language_sentiment_gcs.py +++ b/language/v2/language_sentiment_gcs.py @@ -70,6 +70,4 @@ def sample_analyze_sentiment( # the language specified in the request or, if not specified, # the automatically-detected language. print(f"Language of the text: {response.language_code}") - - # [END language_sentiment_gcs] diff --git a/language/v2/language_sentiment_gcs_test.py b/language/v2/language_sentiment_gcs_test.py index 0d6ae673905..837ee400928 100644 --- a/language/v2/language_sentiment_gcs_test.py +++ b/language/v2/language_sentiment_gcs_test.py @@ -23,11 +23,9 @@ def test_sample_analyze_sentiment_gcs(capsys: ...) -> None: language_sentiment_gcs.sample_analyze_sentiment() captured = capsys.readouterr() - assert ( - "Document sentiment score: 0." in captured.out - and "Document sentiment magnitude: 0." in captured.out - and "Sentence text: I am so happy and joyful." in captured.out - and "Sentence sentiment score: 0." in captured.out - and "Sentence sentiment magnitude: 0." in captured.out - and "Language of the text: en" in captured.out - ) + assert "Document sentiment score: " in captured.out + assert "Document sentiment magnitude: " in captured.out + assert "Sentence text: " in captured.out + assert "Sentence sentiment score: " in captured.out + assert "Sentence sentiment magnitude: " in captured.out + assert "Language of the text: " in captured.out diff --git a/language/v2/language_sentiment_text.py b/language/v2/language_sentiment_text.py index 9aba0640cfb..bf50a0df1cb 100644 --- a/language/v2/language_sentiment_text.py +++ b/language/v2/language_sentiment_text.py @@ -69,6 +69,4 @@ def sample_analyze_sentiment(text_content: str = "I am so happy and joyful.") -> # the language specified in the request or, if not specified, # the automatically-detected language. print(f"Language of the text: {response.language_code}") - - # [END language_sentiment_text] diff --git a/language/v2/language_sentiment_text_test.py b/language/v2/language_sentiment_text_test.py index 7d8ad6c51ce..613135dfcad 100644 --- a/language/v2/language_sentiment_text_test.py +++ b/language/v2/language_sentiment_text_test.py @@ -23,11 +23,9 @@ def test_sample_analyze_sentiment_text(capsys: ...) -> None: language_sentiment_text.sample_analyze_sentiment() captured = capsys.readouterr() - assert ( - "Document sentiment score: 0." in captured.out - and "Document sentiment magnitude: 0." in captured.out - and "Sentence text: I am so happy and joyful." in captured.out - and "Sentence sentiment score: 0." in captured.out - and "Sentence sentiment magnitude: 0." in captured.out - and "Language of the text: en" in captured.out - ) + assert "Document sentiment score: " in captured.out + assert "Document sentiment magnitude: " in captured.out + assert "Sentence text: " in captured.out + assert "Sentence sentiment score: " in captured.out + assert "Sentence sentiment magnitude: " in captured.out + assert "Language of the text: " in captured.out