Azure Text Translation Client Library for Python
Project description
Azure Text Translation client library for Python
Text Translation is a cloud-based REST API feature of the Translator service that uses neural machine translation technology to enable quick and accurate source-to-target text translation in real time across all supported languages.
Use the Text Translation client library for Python to:
-
Return a list of languages supported by Translate, Transliterate, and Dictionary operations.
-
Render single source-language text to multiple target-language texts with a single request.
-
Convert text of a source language in letters of a different script.
-
Return equivalent words for the source term in the target language.
-
Return grammatical structure and context examples for the source term and target term pair.
Source code | Package (PyPI) | API reference documentation | Product documentation | Samples
Getting started
Prerequisites
- Python 3.7 or later is required to use this package.
- An existing Translator service or Cognitive Services resource.
Install the package
Install the Azure Text Translation client library for Python with pip:
pip install azure-ai-translation-text
Create a Translator service resource
You can create Translator resource following Create a Translator resource.
Authenticate the client
Interaction with the service using the client library begins with creating an instance of the TextTranslationClient class. You will need an API key or TokenCredential
to instantiate a client object. For more information regarding authenticating with cognitive services, see Authenticate requests to Translator Service.
Get an API key
You can get the endpoint
, API key
and Region
from the Cognitive Services resource or Translator service resource information in the Azure Portal.
Alternatively, use the Azure CLI snippet below to get the API key from the Translator service resource.
az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>
Create a TextTranslationClient
using an API key and Region credential
Once you have the value for the API key and Region, create an TranslatorCredential
. This will allow you to
update the API key without creating a new client.
With the value of the endpoint, TranslatorCredential
and a Region
, you can create the TextTranslationClient:
text_translator = TextTranslationClient(credential = TranslatorCredential("<apiKey>", "<apiRegion>"));
Key concepts
TextTranslationClient
A TextTranslationClient
is the primary interface for developers using the Text Translation client library. It provides both synchronous and asynchronous operations to access a specific use of text translator, such as get supported languages detection or text translation.
Input
A text element (string
), is a single unit of input to be processed by the translation models in the Translator service. Operations on TextTranslationClient
may take a single text element or a collection of text elements.
For text element length limits, maximum requests size, and supported text encoding see here.
Examples
The following section provides several code snippets using the client
created above, and covers the main features present in this client library. Although most of the snippets below make use of synchronous service calls, keep in mind that the Text Translation for Python library package supports both synchronous and asynchronous APIs.
Get Supported Languages
Gets the set of languages currently supported by other operations of the Translator.
try:
response = text_translator.get_languages()
print(f"Number of supported languages for translate operation: {len(response.translation) if response.translation is not None else 0}")
print(f"Number of supported languages for transliterate operation: {len(response.transliteration) if response.transliteration is not None else 0}")
print(f"Number of supported languages for dictionary operations: {len(response.dictionary) if response.dictionary is not None else 0}")
if response.translation is not None:
print("Translation Languages:")
for key, value in response.translation.items():
print(f"{key} -- name: {value.name} ({value.native_name})")
if response.transliteration is not None:
print("Transliteration Languages:")
for key, value in response.transliteration.items():
print(f"{key} -- name: {value.name}, supported script count: {len(value.scripts)}")
if response.dictionary is not None:
print("Dictionary Languages:")
for key, value in response.dictionary.items():
print(f"{key} -- name: {value.name}, supported target languages count: {len(value.translations)}")
except HttpResponseError as exception:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
For samples on using the languages
endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of languages.
Translate
Renders single source-language text to multiple target-language texts with a single request.
try:
source_language = "en"
target_languages = ["cs"]
input_text_elements = [ InputTextItem(text = "This is a test") ]
response = text_translator.translate(content = input_text_elements, to = target_languages, from_parameter = source_language)
translation = response[0] if response else None
if translation:
for translated_text in translation.translations:
print(f"Text was translated to: '{translated_text.to}' and the result is: '{translated_text.text}'.")
except HttpResponseError as exception:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
For samples on using the translate
endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of translate.
Transliterate
Converts characters or letters of a source language to the corresponding characters or letters of a target language.
try:
language = "zh-Hans"
from_script = "Hans"
to_script = "Latn"
input_text_elements = [ InputTextItem(text = "这是个测试。") ]
response = text_translator.transliterate(content = input_text_elements, language = language, from_script = from_script, to_script = to_script)
transliteration = response[0] if response else None
if transliteration:
print(f"Input text was transliterated to '{transliteration.script}' script. Transliterated text: '{transliteration.text}'.")
except HttpResponseError as exception:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
For samples on using the transliterate
endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of transliterate.
Break Sentence
Identifies the positioning of sentence boundaries in a piece of text.
try:
source_language = "zh-Hans"
source_script = "Latn"
input_text_elements = [ InputTextItem(text = "zhè shì gè cè shì。") ]
response = text_translator.find_sentence_boundaries(content = input_text_elements, language = source_language, script = source_script)
sentence_boundaries = response[0] if response else None
if sentence_boundaries:
detected_language = sentence_boundaries.detected_language
if detected_language:
print(f"Detected languages of the input text: {detected_language.language} with score: {detected_language.score}.")
print(f"The detected sentence boundaries:")
for boundary in sentence_boundaries.sent_len:
print(boundary)
except HttpResponseError as exception:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
For samples on using the break sentence
endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of break sentence.
Dictionary Lookup
Returns equivalent words for the source term in the target language.
try:
source_language = "en"
target_language = "es"
input_text_elements = [ InputTextItem(text = "fly") ]
response = text_translator.lookup_dictionary_entries(content = input_text_elements, from_parameter = source_language, to = target_language)
dictionary_entry = response[0] if response else None
if dictionary_entry:
print(f"For the given input {len(dictionary_entry.translations)} entries were found in the dictionary.")
print(f"First entry: '{dictionary_entry.translations[0].display_target}', confidence: {dictionary_entry.translations[0].confidence}.")
except HttpResponseError as exception:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
For samples on using the dictionary lookup
endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of dictionary lookup.
Dictionary Examples
Returns grammatical structure and context examples for the source term and target term pair.
from azure.ai.translation.text.models import DictionaryExampleTextItem
try:
source_language = "en"
target_language = "es"
input_text_elements = [ DictionaryExampleTextItem(text = "fly", translation = "volar") ]
response = text_translator.lookup_dictionary_examples(content = input_text_elements, from_parameter = source_language, to = target_language)
dictionary_entry = response[0] if response else None
if dictionary_entry:
print(f"For the given input {len(dictionary_entry.examples)} entries were found in the dictionary.")
print(f"First example: '{dictionary_entry.examples[0].target_prefix}{dictionary_entry.examples[0].target_term}{dictionary_entry.examples[0].target_suffix}'.")
except HttpResponseError as exception:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
For samples on using the dictionary examples
endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of dictionary examples.
Troubleshooting
When you interact with the Translator Service using the TextTranslator client library, errors returned by the Translator service correspond to the same HTTP status codes returned for REST API requests.
For example, if you submit a translation request without a target translate language, a 400
error is returned, indicating "Bad Request".
You can find the different error codes returned by the service in the Service Documentation.
Provide Feedback
If you encounter any bugs or have suggestions, please file an issue in the Issues section of the project.
Next steps
More samples can be found under the samples directory.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Release History
1.0.0b1 (2023-04-19)
- Initial Release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file azure-ai-translation-text-1.0.0b1.zip
.
File metadata
- Download URL: azure-ai-translation-text-1.0.0b1.zip
- Upload date:
- Size: 99.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c9eeb292f669f7e4592903ba0a39c2ce5343f61c6db2a688f67be228846b4d5 |
|
MD5 | 9a41692ffedf02cbe940eb36eb18bf42 |
|
BLAKE2b-256 | 3f6d8c2c29058b2f987d462cb46e91f3220f96627086afb3864345262e45f3a3 |
File details
Details for the file azure_ai_translation_text-1.0.0b1-py3-none-any.whl
.
File metadata
- Download URL: azure_ai_translation_text-1.0.0b1-py3-none-any.whl
- Upload date:
- Size: 65.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af700c3cd3253100c563bcc26ecdc9ab993c85297e790f0c526e801335127a8d |
|
MD5 | 1022eef69ece2565be443b3eff5e6626 |
|
BLAKE2b-256 | 42ed6752793aba2150f3841dcad6b1a4ecfda22f7610314bdb1f078c48338aa3 |