From 9b7c0ed9386eb6fe558157297aeec2df0f9bb5e0 Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Mon, 15 Oct 2018 13:53:04 -0700 Subject: [PATCH] Access Display Names of enum fields via enum object [(#1738)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1738) * Get display name of enums using IntEnum Requires updating google-cloud-language to 1.1.0 * Add note about gs://demomaker for video test files * Get display name of enums using IntEnum * Get display name of enums using IntEnum * Revert "Add note about gs://demomaker for video test files" This reverts commit 39d9bfff03201f7c6dcb38fee3856dd537ab4b62. --- .../snippets/cloud-client/v1/requirements.txt | 2 +- samples/snippets/cloud-client/v1/snippets.py | 28 ++++++------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/samples/snippets/cloud-client/v1/requirements.txt b/samples/snippets/cloud-client/v1/requirements.txt index 2cbc37eb..7029093e 100644 --- a/samples/snippets/cloud-client/v1/requirements.txt +++ b/samples/snippets/cloud-client/v1/requirements.txt @@ -1 +1 @@ -google-cloud-language==1.0.2 +google-cloud-language==1.1.0 diff --git a/samples/snippets/cloud-client/v1/snippets.py b/samples/snippets/cloud-client/v1/snippets.py index a41c7cb3..826c28c5 100644 --- a/samples/snippets/cloud-client/v1/snippets.py +++ b/samples/snippets/cloud-client/v1/snippets.py @@ -95,14 +95,11 @@ def entities_text(text): # document.type == enums.Document.Type.HTML entities = client.analyze_entities(document).entities - # entity types from enums.Entity.Type - entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', - 'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER') - for entity in entities: + entity_type = enums.Entity.Type(entity.type) print('=' * 20) print(u'{:<16}: {}'.format('name', entity.name)) - print(u'{:<16}: {}'.format('type', entity_type[entity.type])) + print(u'{:<16}: {}'.format('type', entity_type.name)) print(u'{:<16}: {}'.format('metadata', entity.metadata)) print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', @@ -125,14 +122,11 @@ def entities_file(gcs_uri): # document.type == enums.Document.Type.HTML entities = client.analyze_entities(document).entities - # entity types from enums.Entity.Type - entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', - 'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER') - for entity in entities: + entity_type = enums.Entity.Type(entity.type) print('=' * 20) print(u'{:<16}: {}'.format('name', entity.name)) - print(u'{:<16}: {}'.format('type', entity_type[entity.type])) + print(u'{:<16}: {}'.format('type', entity_type.name)) print(u'{:<16}: {}'.format('metadata', entity.metadata)) print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', @@ -158,12 +152,9 @@ def syntax_text(text): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens - # part-of-speech tags from enums.PartOfSpeech.Tag - pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', - 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') - for token in tokens: - print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], + part_of_speech_tag = enums.PartOfSpeech.Tag(token.part_of_speech.tag) + print(u'{}: {}'.format(part_of_speech_tag.name, token.text.content)) # [END language_python_migration_syntax_text] # [END language_syntax_text] @@ -183,12 +174,9 @@ def syntax_file(gcs_uri): # document.type == enums.Document.Type.HTML tokens = client.analyze_syntax(document).tokens - # part-of-speech tags from enums.PartOfSpeech.Tag - pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', - 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') - for token in tokens: - print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], + part_of_speech_tag = enums.PartOfSpeech.Tag(token.part_of_speech.tag) + print(u'{}: {}'.format(part_of_speech_tag.name, token.text.content)) # [END language_syntax_gcs]