Skip to content

Commit 8d79fd1

Browse files
authored
Access Display Names of enum fields via enum object (#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 39d9bff.
1 parent a68245f commit 8d79fd1

File tree

4 files changed

+15
-31
lines changed

4 files changed

+15
-31
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-cloud-language==1.0.2
1+
google-cloud-language==1.1.0

language/cloud-client/v1/snippets.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,11 @@ def entities_text(text):
9595
# document.type == enums.Document.Type.HTML
9696
entities = client.analyze_entities(document).entities
9797

98-
# entity types from enums.Entity.Type
99-
entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION',
100-
'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER')
101-
10298
for entity in entities:
99+
entity_type = enums.Entity.Type(entity.type)
103100
print('=' * 20)
104101
print(u'{:<16}: {}'.format('name', entity.name))
105-
print(u'{:<16}: {}'.format('type', entity_type[entity.type]))
102+
print(u'{:<16}: {}'.format('type', entity_type.name))
106103
print(u'{:<16}: {}'.format('metadata', entity.metadata))
107104
print(u'{:<16}: {}'.format('salience', entity.salience))
108105
print(u'{:<16}: {}'.format('wikipedia_url',
@@ -125,14 +122,11 @@ def entities_file(gcs_uri):
125122
# document.type == enums.Document.Type.HTML
126123
entities = client.analyze_entities(document).entities
127124

128-
# entity types from enums.Entity.Type
129-
entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION',
130-
'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER')
131-
132125
for entity in entities:
126+
entity_type = enums.Entity.Type(entity.type)
133127
print('=' * 20)
134128
print(u'{:<16}: {}'.format('name', entity.name))
135-
print(u'{:<16}: {}'.format('type', entity_type[entity.type]))
129+
print(u'{:<16}: {}'.format('type', entity_type.name))
136130
print(u'{:<16}: {}'.format('metadata', entity.metadata))
137131
print(u'{:<16}: {}'.format('salience', entity.salience))
138132
print(u'{:<16}: {}'.format('wikipedia_url',
@@ -158,12 +152,9 @@ def syntax_text(text):
158152
# document.type == enums.Document.Type.HTML
159153
tokens = client.analyze_syntax(document).tokens
160154

161-
# part-of-speech tags from enums.PartOfSpeech.Tag
162-
pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM',
163-
'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX')
164-
165155
for token in tokens:
166-
print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag],
156+
part_of_speech_tag = enums.PartOfSpeech.Tag(token.part_of_speech.tag)
157+
print(u'{}: {}'.format(part_of_speech_tag.name,
167158
token.text.content))
168159
# [END language_python_migration_syntax_text]
169160
# [END language_syntax_text]
@@ -183,12 +174,9 @@ def syntax_file(gcs_uri):
183174
# document.type == enums.Document.Type.HTML
184175
tokens = client.analyze_syntax(document).tokens
185176

186-
# part-of-speech tags from enums.PartOfSpeech.Tag
187-
pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM',
188-
'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX')
189-
190177
for token in tokens:
191-
print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag],
178+
part_of_speech_tag = enums.PartOfSpeech.Tag(token.part_of_speech.tag)
179+
print(u'{}: {}'.format(part_of_speech_tag.name,
192180
token.text.content))
193181
# [END language_syntax_gcs]
194182

texttospeech/cloud-client/list_voices.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
def list_voices():
2626
"""Lists the available voices."""
2727
from google.cloud import texttospeech
28+
from google.cloud.texttospeech import enums
2829
client = texttospeech.TextToSpeechClient()
2930

3031
# Performs the list voices request
@@ -38,13 +39,10 @@ def list_voices():
3839
for language_code in voice.language_codes:
3940
print('Supported language: {}'.format(language_code))
4041

41-
# SSML Voice Gender values from google.cloud.texttospeech.enums
42-
ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE',
43-
'FEMALE', 'NEUTRAL']
42+
ssml_gender = enums.SsmlVoiceGender(voice.ssml_gender)
4443

4544
# Display the SSML Voice Gender
46-
print('SSML Voice Gender: {}'.format(
47-
ssml_voice_genders[voice.ssml_gender]))
45+
print('SSML Voice Gender: {}'.format(ssml_gender.name))
4846

4947
# Display the natural sample rate hertz for this voice. Example: 24000
5048
print('Natural Sample Rate Hertz: {}\n'.format(

video/cloud-client/analyze/analyze.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io
3131

3232
from google.cloud import videointelligence
33+
from google.cloud.videointelligence import enums
3334

3435

3536
def analyze_explicit_content(path):
@@ -44,15 +45,12 @@ def analyze_explicit_content(path):
4445
result = operation.result(timeout=90)
4546
print('\nFinished processing.')
4647

47-
likely_string = ("Unknown", "Very unlikely", "Unlikely", "Possible",
48-
"Likely", "Very likely")
49-
5048
# first result is retrieved because a single video was processed
5149
for frame in result.annotation_results[0].explicit_annotation.frames:
50+
likelihood = enums.Likelihood(frame.pornography_likelihood)
5251
frame_time = frame.time_offset.seconds + frame.time_offset.nanos / 1e9
5352
print('Time: {}s'.format(frame_time))
54-
print('\tpornography: {}'.format(
55-
likely_string[frame.pornography_likelihood]))
53+
print('\tpornography: {}'.format(likelihood.name))
5654
# [END video_analyze_explicit_content]
5755

5856

0 commit comments

Comments
 (0)