Skip to content

Commit

Permalink
added model in the sample [(#650)](GoogleCloudPlatform/python-docs-sa…
Browse files Browse the repository at this point in the history
  • Loading branch information
puneith authored and danoscarmike committed Jul 31, 2020
1 parent 0f425c4 commit a403293
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 8 additions & 2 deletions packages/google-cloud-translate/samples/snippets/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ def run_quickstart():
api_key = 'YOUR_API_KEY'

# Instantiates a client
translate_client = translate.Client(api_key)
translate_client = translate.Client(api_key=api_key)

# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'

# MT model type `base` or `nmt`
model = translate.BASE

# Translates some text into Russian
translation = translate_client.translate(text, target_language=target)
translation = translate_client.translate(
text,
target_language=target,
model=model)

print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
Expand Down
18 changes: 11 additions & 7 deletions packages/google-cloud-translate/samples/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

def detect_language(api_key, text):
"""Detects the text's language."""
translate_client = translate.Client(api_key)
translate_client = translate.Client(api_key=api_key)

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
Expand All @@ -41,7 +41,7 @@ def detect_language(api_key, text):

def list_languages(api_key):
"""Lists all available languages."""
translate_client = translate.Client(api_key)
translate_client = translate.Client(api_key=api_key)

results = translate_client.get_languages()

Expand All @@ -55,25 +55,28 @@ def list_languages_with_target(api_key, target):
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
translate_client = translate.Client(api_key)
translate_client = translate.Client(api_key=api_key)

results = translate_client.get_languages(target_language=target)

for language in results:
print(u'{name} ({language})'.format(**language))


def translate_text(api_key, target, text):
def translate_text(api_key, target, text, model=translate.BASE):
"""Translates text into the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
translate_client = translate.Client(api_key)
translate_client = translate.Client(api_key=api_key)

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language=target)
result = translate_client.translate(
text,
target_language=target,
model=model)

print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
Expand Down Expand Up @@ -103,6 +106,7 @@ def translate_text(api_key, target, text):
'translate-text', help=translate_text.__doc__)
translate_text_parser.add_argument('target')
translate_text_parser.add_argument('text')
translate_text_parser.add_argument('model')

args = parser.parse_args()

Expand All @@ -113,4 +117,4 @@ def translate_text(api_key, target, text):
elif args.command == 'list-languages-with-target':
list_languages_with_target(args.api_key, args.target)
elif args.command == 'translate-text':
translate_text(args.api_key, args.target, args.text)
translate_text(args.api_key, args.target, args.text, args.model)

0 comments on commit a403293

Please sign in to comment.