CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-azure-ai-translation-text

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

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

dictionary-operations.mddocs/

Dictionary Operations

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.

Capabilities

Dictionary Entry Lookup

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 tracking

Returns: List of dictionary lookup results with translations and metadata

Dictionary Usage Examples

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 lookup
  • from_language: Source language code
  • to_language: Target language code
  • client_trace_id: Client-generated GUID for request tracking

Returns: List of usage examples for each translation pair

Usage Examples

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})")

Input Types

Dictionary Lookup Input

class InputTextItem:
    text: str  # Term to look up in dictionary

Example Lookup Input

class DictionaryExampleTextItem:
    text: str         # Source term (normalized form from lookup)
    translation: str  # Target translation (normalized_target from lookup)

Response Types

Dictionary Lookup Results

class DictionaryLookupItem:
    display_source: str  # Source term formatted for display
    normalized_source: str  # Normalized form for example lookups
    translations: List[DictionaryTranslation]  # Available translations

Translation Information

class 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 translations

Back-Translation Data

class 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 data

Usage Example Results

class DictionaryExampleItem:
    normalized_source: str  # Source term (matches input)
    normalized_target: str  # Target term (matches input)
    examples: List[DictionaryExample]  # Contextual examples

Contextual Examples

class 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 term

Dictionary Language Support

Check 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})")

Best Practices

Lookup Workflow

  1. Use lookup_dictionary_entries() to find translation options
  2. Select appropriate translations based on confidence and context
  3. Use lookup_dictionary_examples() with normalized forms for usage examples
  4. Consider part-of-speech tags for disambiguation

Term Normalization

  • Dictionary lookups are case-insensitive
  • Use normalized forms from lookup results for example queries
  • Handle multi-word terms and phrases appropriately

Frequency and Confidence

  • Higher confidence scores indicate more reliable translations
  • Frequency counts reflect usage patterns in training data
  • Back-translations provide additional context for term selection

Error Handling

from 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

docs

dictionary-operations.md

index.md

language-support.md

script-transliteration.md

sentence-boundaries.md

text-translation.md

tile.json