-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlossariesManagement.java
More file actions
221 lines (192 loc) Β· 10.1 KB
/
GlossariesManagement.java
File metadata and controls
221 lines (192 loc) Β· 10.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import com.translated.lara.Credentials;
import com.translated.lara.errors.LaraException;
import com.translated.lara.translator.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Complete glossary management examples for the Lara Java SDK
*
* This example demonstrates:
* - Create, list, update, delete glossaries
* - Individual term management (add/remove terms)
* - CSV import with status monitoring
* - Glossary export
* - Glossary terms count
* - Import status checking
*/
public class GlossariesManagement {
public static void main(String[] args) {
// Set your credentials here
String accessKeyId = "your-access-key-id";
String accessKeySecret = "your-access-key-secret";
Credentials credentials = new Credentials(accessKeyId, accessKeySecret);
Translator lara = new Translator(credentials);
String glossaryId = null;
System.out.println("ποΈ Glossaries require a specific subscription plan.");
System.out.println(" If you encounter errors, please check your subscription level.\n");
try {
// Example 1: Basic glossary management
System.out.println("=== Basic Glossary Management ===");
Glossary glossary = lara.glossaries.create("MyDemoGlossary");
System.out.println("β
Created glossary: " + glossary.getName() + " (ID: " + glossary.getId() + ")");
glossaryId = glossary.getId();
// List all glossaries
List<Glossary> glossaries = lara.glossaries.list();
System.out.println("π Total glossaries: " + glossaries.size());
System.out.println();
// Example 2: Glossary operations
System.out.println("=== Glossary Operations ===");
// Get glossary details
Glossary retrievedGlossary = lara.glossaries.get(glossaryId);
if (retrievedGlossary != null) {
System.out.println("π Glossary: " + retrievedGlossary.getName() + " (Owner: " + retrievedGlossary.getOwnerId() + ")");
}
// Update glossary
Glossary updatedGlossary = lara.glossaries.update(glossaryId, "UpdatedDemoGlossary");
System.out.println("π Updated name: '" + glossary.getName() + "' -> '" + updatedGlossary.getName() + "'");
// Example 3: Term management
System.out.println("=== Term Management ===");
// Add (or replace) individual terms to glossary
try {
List<Map<String, String>> terms = Arrays.asList(
Map.of("language", "fr-FR", "value", "Bonjour"),
Map.of("language", "es-ES", "value", "Hola")
);
Object addResult = lara.glossaries.addOrReplaceEntry(glossaryId, terms, null);
System.out.println("β
Terms added successfully to glossary");
System.out.println();
} catch (LaraException e) {
System.out.println("β οΈ Could not add terms: " + e.getMessage() + "\n");
}
// Remove a specific term from glossary
try {
Map<String, String> termToRemove = Map.of("language", "fr-FR", "value", "Bonjour");
Object removeResult = lara.glossaries.deleteEntry(glossaryId, termToRemove, null);
System.out.println("β
Term removed successfully from glossary");
System.out.println();
} catch (LaraException e) {
System.out.println("β οΈ Could not remove term: " + e.getMessage() + "\n");
}
// Example 4: CSV import functionality
System.out.println("=== CSV Import Functionality ===");
// Replace with your actual CSV file path
String csvFilePath = "sample_glossary.csv"; // Create this file with your glossary data
File csvFile = createSampleCsvFile(csvFilePath);
if (csvFile != null && csvFile.exists()) {
System.out.println("Importing CSV file: " + csvFile.getName());
GlossaryImport csvImport = lara.glossaries.importCsv(glossaryId, csvFile);
System.out.println("Import started with ID: " + csvImport.getId());
System.out.println("Initial progress: " + (csvImport.getProgress() * 100) + "%");
// Check import status manually
System.out.println("Checking import status...");
GlossaryImport importStatus = lara.glossaries.getImportStatus(csvImport.getId());
System.out.println("Current progress: " + (importStatus.getProgress() * 100) + "%");
// Wait for import to complete
try {
GlossaryImport completedImport = lara.glossaries.waitForImport(csvImport, 10000L); // 10 seconds timeout
System.out.println("β
Import completed!");
System.out.println("Final progress: " + (completedImport.getProgress() * 100) + "%");
} catch (InterruptedException e) {
System.out.println("Import timeout: The import process took too long to complete.");
}
System.out.println();
// Clean up sample file
csvFile.delete();
} else {
System.out.println("CSV file not found: " + csvFilePath);
}
// Example 5: Export functionality
System.out.println("=== Export Functionality ===");
try {
// Export as CSV table unidirectional format
System.out.println("π€ Exporting as CSV table unidirectional...");
String csvUniData = lara.glossaries.export(glossaryId, Glossary.Type.CSV_TABLE_UNI, "en-US");
System.out.println("β
CSV unidirectional export successful (" + csvUniData.length() + " bytes)");
// Save sample exports to files - replace with your desired output paths
String exportFilePath = "exported_glossary.csv"; // Replace with actual path
saveToFile(csvUniData.getBytes(), exportFilePath);
System.out.println("πΎ Sample export saved to: " + exportFilePath);
System.out.println();
} catch (LaraException | IOException e) {
System.out.println("Error with export: " + e.getMessage() + "\n");
}
// Example 6: Glossary Terms Count
System.out.println("=== Glossary Terms Count ===");
try {
// Get detailed counts
GlossaryCounts detailedCounts = lara.glossaries.counts(glossaryId);
if (detailedCounts.getUnidirectional() != null) {
System.out.println(" Unidirectional entries by language pair:");
for (Map.Entry<String, Integer> entry : detailedCounts.getUnidirectional().entrySet()) {
System.out.println(" " + entry.getKey() + ": " + entry.getValue() + " terms");
}
} else {
System.out.println("No unidirectional entries found");
}
int totalEntries = 0;
if (detailedCounts.getUnidirectional() != null) {
totalEntries = detailedCounts.getUnidirectional().values().stream().mapToInt(Integer::intValue).sum();
}
System.out.println(" Total entries: " + totalEntries);
System.out.println();
} catch (LaraException e) {
System.out.println("Error getting terms count: " + e.getMessage() + "\n");
}
} catch (LaraException e) {
System.out.println("Error creating glossary: " + e.getMessage() + "\n");
return;
} finally {
// Cleanup
System.out.println("=== Cleanup ===");
if (glossaryId != null) {
try {
Glossary deletedGlossary = lara.glossaries.delete(glossaryId);
System.out.println("ποΈ Deleted glossary: " + deletedGlossary.getName());
// Clean up export files - replace with actual cleanup if needed
String exportFilePath = "exported_glossary.csv";
File exportFile = new File(exportFilePath);
if (exportFile.exists()) {
exportFile.delete();
System.out.println("ποΈ Cleaned up export file");
}
} catch (LaraException e) {
System.out.println("Error deleting glossary: " + e.getMessage());
}
}
}
System.out.println("\nπ Glossary management examples completed!");
}
/**
* Helper method to create a sample CSV file for testing
*/
private static File createSampleCsvFile(String filePath) {
try {
File csvFile = new File(filePath);
try (FileOutputStream fos = new FileOutputStream(csvFile)) {
String csvContent = "en-US,es-ES,it-IT\n" +
"Hello,Hola,Ciao\n" +
"Goodbye,AdiΓ³s,Arrivederci\n" +
"Thank you,Gracias,Grazie\n" +
"Welcome,Bienvenido,Benvenuto\n";
fos.write(csvContent.getBytes());
System.out.println("π Created sample CSV file: " + filePath);
}
return csvFile;
} catch (IOException e) {
System.out.println("β Error creating sample CSV file: " + e.getMessage());
return null;
}
}
/**
* Helper method to save byte data to file
*/
private static void saveToFile(byte[] data, String filePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(data);
}
}
}