-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_translation.py
More file actions
134 lines (116 loc) · 5.92 KB
/
text_translation.py
File metadata and controls
134 lines (116 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from lara_sdk import AccessKey, Translator, TextBlock
import os
"""
Complete text translation examples for the Lara Python SDK
This example demonstrates:
- Single string translation
- Multiple strings translation
- Translation with instructions
- TextBlocks translation (mixed translatable/non-translatable content)
- Auto-detect source language
- Advanced options
- Get available languages
"""
def main():
# All examples can use environment variables for credentials:
# export LARA_ACCESS_KEY_ID="your-access-key-id"
# export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
# Falls back to placeholders if not set
access_key_id = os.getenv("LARA_ACCESS_KEY_ID", "your-access-key-id")
access_key_secret = os.getenv("LARA_ACCESS_KEY_SECRET", "your-access-key-secret")
credentials = AccessKey(access_key_id, access_key_secret)
lara = Translator(credentials)
try:
# Example 1: Basic single string translation
print("=== Basic Single String Translation ===")
result1 = lara.translate("Hello, world!", target="fr-FR", source="en-US")
print("Original: Hello, world!")
print(f"French: {result1.translation}\n")
# Example 2: Multiple strings translation
print("=== Multiple Strings Translation ===")
texts = ["Hello", "How are you?", "Goodbye"]
result2 = lara.translate(texts, target="es-ES", source="en-US")
print(f"Original: {texts}")
print(f"Spanish: {result2.translation}\n")
# Example 3: TextBlocks translation (mixed translatable/non-translatable content)
print("=== TextBlocks Translation ===")
text_blocks = [
TextBlock(text="Adventure novels, mysteries, cookbooks—wait, who packed those?", translatable=True),
TextBlock(text="<br>", translatable=False), # Non-translatable HTML
TextBlock(text="Suddenly, it doesn't feel so deserted after all.", translatable=True),
TextBlock(text='<div class="separator"></div>', translatable=False), # Non-translatable HTML
TextBlock(text="Every page you turn is a new journey, and the best part?", translatable=True)
]
result3 = lara.translate(text_blocks, target="it-IT", source="en-US")
print(f"Original TextBlocks: {len(text_blocks)} blocks")
print(f"Translated blocks: {len(result3.translation)}")
for i, translation in enumerate(result3.translation):
print(f"Block {i + 1}: {translation.text}")
print()
# Example 4: Translation with instructions
print("=== Translation with Instructions ===")
result4 = lara.translate(
"Could you send me the report by tomorrow morning?",
target="de-DE",
source="en-US",
instructions=["Be formal", "Use technical terminology"]
)
print("Original: Could you send me the report by tomorrow morning?")
print(f"German (formal): {result4.translation}\n")
# Example 5: Auto-detecting source language
print("=== Auto-detect Source Language ===")
result5 = lara.translate("Bonjour le monde!", target="en-US")
print("Original: Bonjour le monde!")
print(f"Detected source: {result5.source_language}")
print(f"English: {result5.translation}\n")
# Example 6: Advanced options with comprehensive settings
print("=== Translation with Advanced Options ===")
result6 = lara.translate(
"This is a comprehensive translation example",
target="it-IT",
source="en-US",
adapt_to=["mem_1A2b3C4d5E6f7G8h9I0jKl", "mem_2XyZ9AbC8dEf7GhI6jKlMn"], # Replace with actual memory IDs
glossaries=["gls_1A2b3C4d5E6f7G8h9I0jKl", "gls_2XyZ9AbC8dEf7GhI6jKlMn"], # Replace with actual glossary IDs
instructions=["Be professional"],
style="fluid",
content_type="text/plain",
timeout_ms=10000,
)
print("Original: This is a comprehensive translation example")
print(f"Italian (with all options): {result6.translation}\n")
# Example 7: Profanities detection and handling options
print("=== Translation with Profanities Detection and Handling Options ===")
profanity_text = "Don't be such a tool."
detect_result = lara.translate(profanity_text, target="it-IT", source="en-US",
profanities_detect="source_target",
profanities_handling="detect", verbose=True)
hide_result = lara.translate(profanity_text, target="it-IT", source="en-US",
profanities_detect="target",
profanities_handling="hide", verbose=True)
print(f"Original: {profanity_text}")
print(f"Detect mode translation: {detect_result.translation}")
print(f"Hide mode translation: {hide_result.translation}")
# Example 8: Get available languages
print("=== Available Languages ===")
languages = lara.languages()
print(f"Supported languages: {languages}")
# Example 9: Quality estimation for a single sentence pair
print("\n=== Quality Estimation: single sentence ===")
qe_single = lara.quality_estimation(
source="en-US", target="it-IT",
sentence="Hello, how are you today?",
translation="Ciao, come stai oggi?",
)
print(f"Score: {qe_single.score}")
# Example 10: Quality estimation for a batch of sentence pairs
print("\n=== Quality Estimation: batch ===")
qe_batch = lara.quality_estimation(
source="en-US", target="it-IT",
sentence=["Good morning.", "The weather is nice."],
translation=["Buongiorno.", "Il tempo è bello."],
)
print(f"Scores: {[r.score for r in qe_batch]}")
except Exception as error:
print(f"Error: {error}")
if __name__ == "__main__":
main()