|
| 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] |
0 commit comments