-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglossaries_management.py
More file actions
190 lines (157 loc) Β· 7.85 KB
/
glossaries_management.py
File metadata and controls
190 lines (157 loc) Β· 7.85 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from lara_sdk import AccessKey, Translator, GlossaryTerm
import os
"""
Complete glossary management examples for the Lara Python SDK
This example demonstrates:
- Create, list, update, delete glossaries
- CSV import with status monitoring
- Glossary export
- Glossary terms count
- Import status checking
- Add/replace and delete glossary term entries
"""
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"
# Set your credentials here
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)
print("ποΈ Glossaries require a specific subscription plan.")
print(" If you encounter errors, please check your subscription level.\n")
glossary_id = None
try:
# Example 1: Basic glossary management
print("=== Basic Glossary Management ===")
glossary = lara.glossaries.create("MyDemoGlossary")
print(f"β
Created glossary: {glossary.name} (ID: {glossary.id})")
glossary_id = glossary.id
# List all glossaries
glossaries = lara.glossaries.list()
print(f"π Total glossaries: {len(glossaries)}")
print()
# Example 2: Glossary operations
print("=== Glossary Operations ===")
# Get glossary details
retrieved_glossary = lara.glossaries.get(glossary_id)
if retrieved_glossary:
print(f"π Glossary: {retrieved_glossary.name} (Owner: {retrieved_glossary.owner_id})")
# Get glossary terms count
counts = lara.glossaries.counts(glossary_id)
if counts.unidirectional:
for lang, count in counts.unidirectional.items():
print(f" {lang}: {count} entries")
# Update glossary
updated_glossary = lara.glossaries.update(glossary_id, "UpdatedDemoGlossary")
print(f"π Updated name: '{glossary.name}' -> '{updated_glossary.name}'")
# Example 3: CSV import functionality
print("=== CSV Import Functionality ===")
# Replace with your actual CSV file path
csv_file_path = "sample_glossary.csv" # Create this file with your glossary data
if os.path.exists(csv_file_path):
print(f"Importing CSV file: {os.path.basename(csv_file_path)}")
csv_import = lara.glossaries.import_csv(glossary_id, csv_file_path)
print(f"Import started with ID: {csv_import.id}")
print(f"Initial progress: {round(csv_import.progress * 100)}%")
# Check import status manually
print("Checking import status...")
import_status = lara.glossaries.get_import_status(csv_import.id)
print(f"Current progress: {round(import_status.progress * 100)}%")
# Wait for import to complete
try:
completed_import = lara.glossaries.wait_for_import(csv_import, max_wait_time=10)
print("β
Import completed!")
print(f"Final progress: {round(completed_import.progress * 100)}%")
except TimeoutError:
print("Import timeout: The import process took too long to complete.")
print()
else:
print(f"CSV file not found: {csv_file_path}")
# Example 4: Export functionality
print("=== Export Functionality ===")
try:
# Export as CSV table unidirectional format
print("π€ Exporting as CSV table unidirectional...")
csv_uni_data = lara.glossaries.export(glossary_id, content_type="csv/table-uni", source="en-US")
print(f"β
CSV unidirectional export successful ({len(csv_uni_data)} bytes)")
# Save sample export to file - replace with your desired output path
export_file_path = "exported_glossary.csv" # Replace with actual path
with open(export_file_path, 'wb') as f:
f.write(csv_uni_data)
print(f"πΎ Sample export saved to: {os.path.basename(export_file_path)}")
print()
except Exception as e:
print(f"Error with export: {e}\n")
# Example 5: Glossary Terms Count
print("=== Glossary Terms Count ===")
try:
# Get detailed counts
detailed_counts = lara.glossaries.counts(glossary_id)
print("π Detailed glossary terms count:")
if detailed_counts.unidirectional:
print(" Unidirectional entries by language pair:")
for lang_pair, count in detailed_counts.unidirectional.items():
print(f" {lang_pair}: {count} terms")
else:
print(" No unidirectional entries found")
total_entries = 0
if detailed_counts.unidirectional:
total_entries += sum(detailed_counts.unidirectional.values())
print(f" Total entries: {total_entries}")
except Exception as e:
print(f"Error getting glossary terms count: {e}\n")
# Example 6: Add/Replace and Delete glossary term entries
print("=== Glossary Term Entries ===")
try:
# Add a new entry with multiple language terms
terms = [
GlossaryTerm(language="en-US", value="computer"),
GlossaryTerm(language="it-IT", value="computer")
]
add_result = lara.glossaries.add_or_replace_entry(glossary_id, terms)
print(f"Added entry, import ID: {add_result.id}")
# Add another entry with a custom GUID
terms_with_guid = [
GlossaryTerm(language="en-US", value="keyboard"),
GlossaryTerm(language="it-IT", value="tastiera")
]
lara.glossaries.add_or_replace_entry(glossary_id, terms_with_guid, guid="custom-guid-123")
print("Added entry with custom GUID")
# Replace an existing entry by using the same GUID
updated_terms = [
GlossaryTerm(language="en-US", value="keyboard"),
GlossaryTerm(language="it-IT", value="tastiera"),
GlossaryTerm(language="fr-FR", value="clavier")
]
lara.glossaries.add_or_replace_entry(glossary_id, updated_terms, guid="custom-guid-123")
print("Replaced entry using existing GUID")
# Delete an entry by GUID
lara.glossaries.delete_entry(glossary_id, guid="custom-guid-123")
print("Deleted entry by GUID")
# Delete an entry by term
lara.glossaries.delete_entry(glossary_id, term=GlossaryTerm(language="en-US", value="computer"))
print("Deleted entry by term")
print()
except Exception as e:
print(f"Error with term entries: {e}\n")
except Exception as e:
print(f"Error creating glossary: {e}\n")
finally:
# Cleanup
print("=== Cleanup ===")
if glossary_id:
try:
deleted_glossary = lara.glossaries.delete(glossary_id)
print(f"ποΈ Deleted glossary: {deleted_glossary.name}")
# Clean up export files - replace with actual cleanup if needed
export_file_path = "exported_glossary.csv"
if os.path.exists(export_file_path):
os.remove(export_file_path)
print("ποΈ Cleaned up export file")
except Exception as e:
print(f"Error deleting glossary: {e}")
print("\nπ Glossary management examples completed!")
if __name__ == "__main__":
main()