Skip to content

Commit

Permalink
Access Display Names of enum fields via enum object (#1738)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
beccasaurus authored Oct 15, 2018
1 parent a68245f commit 8d79fd1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 31 deletions.
2 changes: 1 addition & 1 deletion language/cloud-client/v1/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
google-cloud-language==1.0.2
google-cloud-language==1.1.0
28 changes: 8 additions & 20 deletions language/cloud-client/v1/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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]
Expand All @@ -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]

Expand Down
8 changes: 3 additions & 5 deletions texttospeech/cloud-client/list_voices.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
def list_voices():
"""Lists the available voices."""
from google.cloud import texttospeech
from google.cloud.texttospeech import enums
client = texttospeech.TextToSpeechClient()

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

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

# Display the SSML Voice Gender
print('SSML Voice Gender: {}'.format(
ssml_voice_genders[voice.ssml_gender]))
print('SSML Voice Gender: {}'.format(ssml_gender.name))

# Display the natural sample rate hertz for this voice. Example: 24000
print('Natural Sample Rate Hertz: {}\n'.format(
Expand Down
8 changes: 3 additions & 5 deletions video/cloud-client/analyze/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io

from google.cloud import videointelligence
from google.cloud.videointelligence import enums


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

likely_string = ("Unknown", "Very unlikely", "Unlikely", "Possible",
"Likely", "Very likely")

# first result is retrieved because a single video was processed
for frame in result.annotation_results[0].explicit_annotation.frames:
likelihood = enums.Likelihood(frame.pornography_likelihood)
frame_time = frame.time_offset.seconds + frame.time_offset.nanos / 1e9
print('Time: {}s'.format(frame_time))
print('\tpornography: {}'.format(
likely_string[frame.pornography_likelihood]))
print('\tpornography: {}'.format(likelihood.name))
# [END video_analyze_explicit_content]


Expand Down

0 comments on commit 8d79fd1

Please sign in to comment.