-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Basic implementation of Translate API. #1772
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
Changes from all commits
6e566c6
039ae5e
8a3a97d
01e8f9a
13fb593
92e92a2
2a4eaf8
694384a
beda548
fcd6fee
b759a76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Translate Client | ||
| ================ | ||
|
|
||
| .. automodule:: gcloud.translate.client | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
|
|
||
| Connection | ||
| ~~~~~~~~~~ | ||
|
|
||
| .. automodule:: gcloud.translate.connection | ||
| :members: | ||
| :undoc-members: | ||
| :show-inheritance: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| Using the API | ||
| ============= | ||
|
|
||
| With `Google Translate`_, you can dynamically translate text | ||
| between thousands of language pairs. The Google Translate API | ||
| lets websites and programs integrate with Google Translate | ||
| programmatically. Google Translate API is available as a | ||
| paid service. See the `Pricing`_ and `FAQ`_ pages for details. | ||
|
|
||
| Authentication / Configuration | ||
| ------------------------------ | ||
|
|
||
| - Use :class:`~gcloud.translate.client.Client` objects to configure | ||
| your applications. | ||
|
|
||
| - :class:`~gcloud.translate.client.Client` objects hold both a ``key`` | ||
| and a connection to the Translate service. | ||
|
|
||
| - **An API key is required for Translate.** See | ||
| `Identifying your application to Google`_ for details. This is | ||
| significantly different than the other clients in ``gcloud-python``. | ||
|
|
||
| Methods | ||
| ------- | ||
|
|
||
| To create a client: | ||
|
|
||
| .. code:: | ||
|
|
||
| >>> from gcloud import translate | ||
| >>> client = translate.Client('my-api-key') | ||
|
|
||
| By default, the client targets English when doing detections | ||
| and translations, but a non-default value can be used as | ||
| well: | ||
|
|
||
| .. code:: | ||
|
|
||
| >>> from gcloud import translate | ||
| >>> client = translate.Client('my-api-key', target_language='es') | ||
|
|
||
| The Google Translate API has three supported methods, and they | ||
| map to three methods on a client: | ||
| :meth:`~gcloud.translate.client.Client.get_languages`, | ||
| :meth:`~gcloud.translate.client.Client.detect_language` and | ||
| :meth:`~gcloud.translate.client.Client.translate`. | ||
|
|
||
| To get a list of languages supported by Google Translate | ||
|
|
||
| .. code:: | ||
|
|
||
| >>> from gcloud import translate | ||
| >>> client = translate.Client('my-api-key') | ||
| >>> client.get_languages() | ||
| [ | ||
| { | ||
| 'language': 'af', | ||
| 'name': 'Afrikaans', | ||
| }, | ||
| ... | ||
| ] | ||
|
|
||
| To detect the language that some given text is written in: | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| .. code:: | ||
|
|
||
| >>> from gcloud import translate | ||
| >>> client = translate.Client('my-api-key') | ||
| >>> client.detect_language(['Me llamo', 'I am']) | ||
| [ | ||
| { | ||
| 'confidence': 0.25830904, | ||
| 'input': 'Me llamo', | ||
| 'language': 'es', | ||
| }, { | ||
| 'confidence': 0.17112699, | ||
| 'input': 'I am', | ||
| 'language': 'en', | ||
| }, | ||
| ] | ||
|
|
||
| The `confidence`_ value is an optional floating point value between 0 and 1. | ||
| The closer this value is to 1, the higher the confidence level for the | ||
| language detection. This member is not always available. | ||
|
|
||
| To translate text: | ||
|
|
||
| .. code:: | ||
|
|
||
| >>> from gcloud import translate | ||
| >>> client = translate.Client('my-api-key') | ||
| >>> client.translate('koszula') | ||
| { | ||
| 'translatedText': 'shirt', | ||
| 'detectedSourceLanguage': 'pl', | ||
| 'input': 'koszula', | ||
| } | ||
|
|
||
| or to use a non-default target language: | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| .. code:: | ||
|
|
||
| >>> from gcloud import translate | ||
| >>> client = translate.Client('my-api-key') | ||
| >>> client.translate(['Me llamo Jeff', 'My name is Jeff'], | ||
| ... target_language='de') | ||
| [ | ||
| { | ||
| 'translatedText': 'Mein Name ist Jeff', | ||
| 'detectedSourceLanguage': 'es', | ||
| 'input': 'Me llamo Jeff', | ||
| }, { | ||
| 'translatedText': 'Mein Name ist Jeff', | ||
| 'detectedSourceLanguage': 'en', | ||
| 'input': 'My name is Jeff', | ||
| }, | ||
| ] | ||
|
|
||
| .. _Google Translate: https://cloud.google.com/translate | ||
| .. _Pricing: https://cloud.google.com/translate/v2/pricing.html | ||
| .. _FAQ: https://cloud.google.com/translate/v2/faq.html | ||
| .. _Identifying your application to Google: https://cloud.google.com/translate/v2/using_rest#auth | ||
| .. _confidence: https://cloud.google.com/translate/v2/detecting-language-with-rest | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright 2016 Google Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Google Cloud Translate API wrapper.""" | ||
|
|
||
| from gcloud.translate.client import Client | ||
| from gcloud.translate.connection import Connection |
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.