Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added with and without model separate functions #657

Merged
merged 1 commit into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions translate/cloud-client/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@ def run_quickstart():
# 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,
model=model)
target_language=target)

print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
Expand Down
27 changes: 24 additions & 3 deletions translate/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ def list_languages_with_target(api_key, target):
print(u'{name} ({language})'.format(**language))


def translate_text(api_key, target, text, model=translate.BASE):
def translate_text_with_model(api_key, target, text, model=translate.BASE):
"""Translates text into the target language.

Make sure your project is whitelisted.

Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
Expand All @@ -84,6 +86,26 @@ def translate_text(api_key, target, text, model=translate.BASE):
result['detectedSourceLanguage']))


def translate_text(api_key, target, text):
"""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=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)

print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
result['detectedSourceLanguage']))


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
Expand All @@ -106,7 +128,6 @@ def translate_text(api_key, target, text, model=translate.BASE):
'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 @@ -117,4 +138,4 @@ def translate_text(api_key, target, text, model=translate.BASE):
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, args.model)
translate_text(args.api_key, args.target, args.text)