Azure Text Translation client library for Python that provides neural machine translation technology for quick and accurate source-to-target text translation in real time across all supported languages
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Bilingual dictionary functionality providing alternative translations, part-of-speech information, usage frequency data, back-translations, and contextual usage examples for translation pairs between supported language combinations.
Returns alternative translations and linguistic information for source terms, including part-of-speech tags, confidence scores, and back-translation data.
def lookup_dictionary_entries(
body: Union[List[str], List[InputTextItem], IO[bytes]],
*,
from_language: str,
to_language: str,
client_trace_id: Optional[str] = None,
**kwargs: Any
) -> List[DictionaryLookupItem]Parameters:
body: Terms to look up (strings, InputTextItem objects, or binary data)from_language: Source language code (must be supported for dictionary operations)to_language: Target language code (must be supported for dictionary operations)client_trace_id: Client-generated GUID for request trackingReturns: List of dictionary lookup results with translations and metadata
Returns contextual usage examples for specific translation pairs discovered through dictionary entry lookup.
def lookup_dictionary_examples(
body: Union[List[DictionaryExampleTextItem], IO[bytes]],
*,
from_language: str,
to_language: str,
client_trace_id: Optional[str] = None,
**kwargs: Any
) -> List[DictionaryExampleItem]Parameters:
body: Translation pairs for example lookupfrom_language: Source language codeto_language: Target language codeclient_trace_id: Client-generated GUID for request trackingReturns: List of usage examples for each translation pair
from azure.ai.translation.text import TextTranslationClient
from azure.ai.translation.text.models import DictionaryExampleTextItem
from azure.core.credentials import AzureKeyCredential
client = TextTranslationClient(
credential=AzureKeyCredential("your-api-key"),
region="your-region"
)
# Dictionary lookup for English to Spanish
lookup_response = client.lookup_dictionary_entries(
body=["fly"],
from_language="en",
to_language="es"
)
entry = lookup_response[0]
print(f"Source term: {entry.display_source}")
print(f"Normalized form: {entry.normalized_source}")
for translation in entry.translations:
print(f"\nTranslation: {translation.display_target}")
print(f"Part of speech: {translation.pos_tag}")
print(f"Confidence: {translation.confidence}")
print(f"Prefix word: '{translation.prefix_word}'")
print("Back-translations:")
for back_trans in translation.back_translations:
print(f" {back_trans.display_text} (frequency: {back_trans.frequency_count})")
# Get usage examples for specific translation pairs
examples_response = client.lookup_dictionary_examples(
body=[
DictionaryExampleTextItem(text="fly", translation="volar"),
DictionaryExampleTextItem(text="fly", translation="mosca")
],
from_language="en",
to_language="es"
)
for example_item in examples_response:
print(f"\nExamples for: {example_item.normalized_source} -> {example_item.normalized_target}")
for example in example_item.examples:
source_sentence = f"{example.source_prefix}{example.source_term}{example.source_suffix}"
target_sentence = f"{example.target_prefix}{example.target_term}{example.target_suffix}"
print(f" EN: {source_sentence.strip()}")
print(f" ES: {target_sentence.strip()}")
# Multi-term lookup
multi_lookup = client.lookup_dictionary_entries(
body=["house", "car", "book"],
from_language="en",
to_language="fr"
)
for i, entry in enumerate(multi_lookup):
print(f"\nTerm {i+1}: {entry.display_source}")
print(f"Translations: {len(entry.translations)}")
# Show most confident translation
if entry.translations:
best_translation = max(entry.translations, key=lambda t: t.confidence)
print(f"Best match: {best_translation.display_target} ({best_translation.confidence:.2f})")class InputTextItem:
text: str # Term to look up in dictionaryclass DictionaryExampleTextItem:
text: str # Source term (normalized form from lookup)
translation: str # Target translation (normalized_target from lookup)class DictionaryLookupItem:
display_source: str # Source term formatted for display
normalized_source: str # Normalized form for example lookups
translations: List[DictionaryTranslation] # Available translationsclass DictionaryTranslation:
normalized_target: str # Normalized target term for examples
display_target: str # Target term formatted for display
pos_tag: str # Part-of-speech tag
confidence: float # Translation confidence (0.0 to 1.0)
prefix_word: str # Grammatical prefix (e.g., article)
back_translations: List[BackTranslation] # Reverse translationsclass BackTranslation:
normalized_text: str # Normalized back-translation
display_text: str # Back-translation formatted for display
num_examples: int # Available usage examples count
frequency_count: int # Usage frequency in training dataclass DictionaryExampleItem:
normalized_source: str # Source term (matches input)
normalized_target: str # Target term (matches input)
examples: List[DictionaryExample] # Contextual examplesclass DictionaryExample:
source_prefix: str # Text before source term
source_term: str # Source term in context
source_suffix: str # Text after source term
target_prefix: str # Text before target term
target_term: str # Target term in context
target_suffix: str # Text after target termCheck which language pairs support dictionary operations:
# Get dictionary-supported language pairs
response = client.get_supported_languages(scope="dictionary")
if response.dictionary:
for source_lang, lang_info in response.dictionary.items():
print(f"\nSource: {source_lang} ({lang_info.name})")
print("Available targets:")
for target in lang_info.translations:
print(f" -> {target.code} ({target.name})")lookup_dictionary_entries() to find translation optionslookup_dictionary_examples() with normalized forms for usage examplesfrom azure.core.exceptions import HttpResponseError
try:
response = client.lookup_dictionary_entries(
body=["nonexistent"],
from_language="en",
to_language="unsupported" # Unsupported language pair
)
except HttpResponseError as error:
if error.error:
print(f"Error Code: {error.error.code}")
print(f"Message: {error.error.message}")
# Handle missing translations gracefully
lookup_result = client.lookup_dictionary_entries(
body=["raretechnicalterm"],
from_language="en",
to_language="es"
)
entry = lookup_result[0]
if not entry.translations:
print(f"No dictionary translations found for: {entry.display_source}")
else:
print(f"Found {len(entry.translations)} translation(s)")Install with Tessl CLI
npx tessl i tessl/pypi-azure-ai-translation-text