Skip to content

Commit e440097

Browse files
kornoskKornraphop (Ken) Kawintiranondandhlee
authored
feat(language): update code example for Cloud NL v2 - python (GoogleCloudPlatform#10544)
* Update API v2 example code for analyze_sentiment * Update library client version to 2.11.0 * Update API v2 example code for analyze_entities * Update API v2 example code for analyze_entities * Revert back example code for analyze_sentiment to v1 * Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update based on comments * Update based on comments * Update based on comments * Update based on comments * Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update language/v2/language_entities_text.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update language/v2/language_entities_text.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Update language/v2/language_entities_gcs.py Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com> * Add code samples for language_classify * Add code samples for language_sentiment * Fix minor bugs * Update based on comments. * Update based on the style guide * Update based on comments --------- Co-authored-by: Kornraphop (Ken) Kawintiranon <kornraphop@google.com> Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
1 parent e498387 commit e440097

15 files changed

Lines changed: 669 additions & 0 deletions
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# To install the latest published package dependency, execute the following:
17+
# pip install google-cloud-language
18+
19+
# sample-metadata
20+
# title: Classify Content (GCS)
21+
# description: Classifying Content in text file stored in Cloud Storage
22+
23+
# [START language_classify_gcs]
24+
from google.cloud import language_v2
25+
26+
27+
def sample_classify_text(
28+
gcs_content_uri: str = "gs://cloud-samples-data/language/classify-entertainment.txt",
29+
) -> None:
30+
"""
31+
Classifies Content in text file stored in Cloud Storage.
32+
33+
Args:
34+
gcs_content_uri: Google Cloud Storage URI where the file content is located.
35+
e.g. gs://[Your Bucket]/[Path to File].
36+
"""
37+
38+
client = language_v2.LanguageServiceClient()
39+
40+
# Available types: PLAIN_TEXT, HTML
41+
document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT
42+
43+
# Optional. If not specified, the language is automatically detected.
44+
# For list of supported languages:
45+
# https://cloud.google.com/natural-language/docs/languages
46+
language_code = "en"
47+
document = {
48+
"gcs_content_uri": gcs_content_uri,
49+
"type_": document_type_in_plain_text,
50+
"language_code": language_code,
51+
}
52+
53+
response = client.classify_text(request={"document": document})
54+
# Loop through classified categories returned from the API
55+
for category in response.categories:
56+
# Get the name of the category representing the document.
57+
# See the predefined taxonomy of categories:
58+
# https://cloud.google.com/natural-language/docs/categories
59+
print(f"Category name: {category.name}")
60+
# Get the confidence. Number representing how certain the classifier
61+
# is that this category represents the provided text.
62+
print(f"Confidence: {category.confidence}")
63+
# [END language_classify_gcs]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import os
17+
18+
import language_classify_gcs
19+
20+
21+
def test_sample_classify_text_gcs(capsys: ...) -> None:
22+
assert os.environ["GOOGLE_CLOUD_PROJECT"] != ""
23+
24+
language_classify_gcs.sample_classify_text()
25+
captured = capsys.readouterr()
26+
assert "Category name: " in captured.out
27+
assert "Confidence: " in captured.out
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# To install the latest published package dependency, execute the following:
17+
# pip install google-cloud-language
18+
19+
# sample-metadata
20+
# title: Classify Content
21+
# description: Classifying Content in a String
22+
23+
# [START language_classify_text]
24+
from google.cloud import language_v2
25+
26+
27+
def sample_classify_text(
28+
text_content: str = "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.",
29+
) -> None:
30+
"""
31+
Classifies Content in a string.
32+
33+
Args:
34+
text_content: The text content to analyze.
35+
"""
36+
37+
client = language_v2.LanguageServiceClient()
38+
39+
# Available types: PLAIN_TEXT, HTML
40+
document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT
41+
42+
# Optional. If not specified, the language is automatically detected.
43+
# For list of supported languages:
44+
# https://cloud.google.com/natural-language/docs/languages
45+
language_code = "en"
46+
document = {
47+
"content": text_content,
48+
"type_": document_type_in_plain_text,
49+
"language_code": language_code,
50+
}
51+
52+
response = client.classify_text(request={"document": document})
53+
# Loop through classified categories returned from the API
54+
for category in response.categories:
55+
# Get the name of the category representing the document.
56+
# See the predefined taxonomy of categories:
57+
# https://cloud.google.com/natural-language/docs/categories
58+
print(f"Category name: {category.name}")
59+
# Get the confidence. Number representing how certain the classifier
60+
# is that this category represents the provided text.
61+
print(f"Confidence: {category.confidence}")
62+
# [END language_classify_text]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import os
17+
18+
import language_classify_text
19+
20+
21+
def test_sample_classify_text(capsys: ...) -> None:
22+
assert os.environ["GOOGLE_CLOUD_PROJECT"] != ""
23+
24+
language_classify_text.sample_classify_text()
25+
captured = capsys.readouterr()
26+
assert "Category name: " in captured.out
27+
assert "Confidence: " in captured.out
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# To install the latest published package dependency, execute the following:
17+
# pip install google-cloud-language
18+
19+
# sample-metadata
20+
# title: Analyzing Entities (GCS)
21+
# description: Analyzing Entities in text file stored in Cloud Storage
22+
23+
# [START language_entities_gcs]
24+
from google.cloud import language_v2
25+
26+
27+
def sample_analyze_entities(
28+
gcs_content_uri: str = "gs://cloud-samples-data/language/entity.txt",
29+
) -> None:
30+
"""
31+
Analyzes Entities in text file stored in Cloud Storage.
32+
33+
Args:
34+
gcs_content_uri: Google Cloud Storage URI where the file content is located.
35+
e.g. gs://[Your Bucket]/[Path to File]
36+
"""
37+
38+
client = language_v2.LanguageServiceClient()
39+
40+
# Available types: PLAIN_TEXT, HTML
41+
document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT
42+
43+
# Optional. If not specified, the language is automatically detected.
44+
# For list of supported languages:
45+
# https://cloud.google.com/natural-language/docs/languages
46+
language_code = "en"
47+
document = {
48+
"gcs_content_uri": gcs_content_uri,
49+
"type_": document_type_in_plain_text,
50+
"language_code": language_code,
51+
}
52+
53+
# Available values: NONE, UTF8, UTF16, UTF32.
54+
# See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType.
55+
encoding_type = language_v2.EncodingType.UTF8
56+
57+
response = client.analyze_entities(
58+
request={"document": document, "encoding_type": encoding_type}
59+
)
60+
61+
for entity in response.entities:
62+
print(f"Representative name for the entity: {entity.name}")
63+
64+
# Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al.
65+
# See https://cloud.google.com/natural-language/docs/reference/rest/v2/Entity#type.
66+
print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}")
67+
68+
# Loop over the metadata associated with entity.
69+
# Some entity types may have additional metadata, e.g. ADDRESS entities
70+
# may have metadata for the address street_name, postal_code, et al.
71+
for metadata_name, metadata_value in entity.metadata.items():
72+
print(f"{metadata_name}: {metadata_value}")
73+
74+
# Loop over the mentions of this entity in the input document.
75+
# The API currently supports proper noun mentions.
76+
for mention in entity.mentions:
77+
print(f"Mention text: {mention.text.content}")
78+
79+
# Get the mention type, e.g. PROPER for proper noun
80+
print(
81+
"Mention type:" f" {language_v2.EntityMention.Type(mention.type_).name}"
82+
)
83+
84+
# Get the probability score associated with the first mention of the entity in the (0, 1.0] range.
85+
print(f"Probability score: {mention.probability}")
86+
87+
# Get the language of the text, which will be the same as
88+
# the language specified in the request or, if not specified,
89+
# the automatically-detected language.
90+
print(f"Language of the text: {response.language_code}")
91+
# [END language_entities_gcs]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import os
17+
18+
import language_entities_gcs
19+
20+
21+
def test_sample_analyze_entities_gcs(capsys: ...) -> None:
22+
assert os.environ["GOOGLE_CLOUD_PROJECT"] != ""
23+
24+
language_entities_gcs.sample_analyze_entities()
25+
captured = capsys.readouterr()
26+
assert "Representative name for the entity: " in captured.out
27+
assert "Entity type: " in captured.out
28+
assert "Mention text: " in captured.out
29+
assert "Mention type: " in captured.out
30+
assert "Probability score: " in captured.out
31+
assert "Language of the text: " in captured.out
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# To install the latest published package dependency, execute the following:
17+
# pip install google-cloud-language
18+
19+
# sample-metadata
20+
# title: Analyzing Entities
21+
# description: Analyzing Entities in a String
22+
23+
# [START language_entities_text]
24+
from google.cloud import language_v2
25+
26+
27+
def sample_analyze_entities(text_content: str = "California is a state.") -> None:
28+
"""
29+
Analyzes Entities in a string.
30+
31+
Args:
32+
text_content: The text content to analyze
33+
"""
34+
35+
client = language_v2.LanguageServiceClient()
36+
37+
# Available types: PLAIN_TEXT, HTML
38+
document_type_in_plain_text = language_v2.Document.Type.PLAIN_TEXT
39+
40+
# Optional. If not specified, the language is automatically detected.
41+
# For list of supported languages:
42+
# https://cloud.google.com/natural-language/docs/languages
43+
language_code = "en"
44+
document = {
45+
"content": text_content,
46+
"type_": document_type_in_plain_text,
47+
"language_code": language_code,
48+
}
49+
50+
# Available values: NONE, UTF8, UTF16, UTF32.
51+
# See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType.
52+
encoding_type = language_v2.EncodingType.UTF8
53+
54+
response = client.analyze_entities(
55+
request={"document": document, "encoding_type": encoding_type}
56+
)
57+
58+
for entity in response.entities:
59+
print(f"Representative name for the entity: {entity.name}")
60+
61+
# Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al.
62+
# See https://cloud.google.com/natural-language/docs/reference/rest/v2/Entity#type.
63+
print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}")
64+
65+
# Loop over the metadata associated with entity.
66+
# Some entity types may have additional metadata, e.g. ADDRESS entities
67+
# may have metadata for the address street_name, postal_code, et al.
68+
for metadata_name, metadata_value in entity.metadata.items():
69+
print(f"{metadata_name}: {metadata_value}")
70+
71+
# Loop over the mentions of this entity in the input document.
72+
# The API currently supports proper noun mentions.
73+
for mention in entity.mentions:
74+
print(f"Mention text: {mention.text.content}")
75+
76+
# Get the mention type, e.g. PROPER for proper noun
77+
print(f"Mention type: {language_v2.EntityMention.Type(mention.type_).name}")
78+
79+
# Get the probability score associated with the first mention of the entity in the (0, 1.0] range.
80+
print(f"Probability score: {mention.probability}")
81+
82+
# Get the language of the text, which will be the same as
83+
# the language specified in the request or, if not specified,
84+
# the automatically-detected language.
85+
print(f"Language of the text: {response.language_code}")
86+
# [END language_entities_text]

0 commit comments

Comments
 (0)