-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
TranslateSnippetsBeta.java
448 lines (396 loc) · 17 KB
/
TranslateSnippetsBeta.java
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.examples.translate.snippets;
import com.google.api.core.ApiFuture;
import com.google.cloud.translate.v3beta1.BatchTranslateResponse;
import com.google.cloud.translate.v3beta1.BatchTranslateTextRequest;
import com.google.cloud.translate.v3beta1.CreateGlossaryRequest;
import com.google.cloud.translate.v3beta1.DeleteGlossaryResponse;
import com.google.cloud.translate.v3beta1.DetectLanguageRequest;
import com.google.cloud.translate.v3beta1.DetectLanguageResponse;
import com.google.cloud.translate.v3beta1.GcsDestination;
import com.google.cloud.translate.v3beta1.GcsSource;
import com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest;
import com.google.cloud.translate.v3beta1.Glossary;
import com.google.cloud.translate.v3beta1.Glossary.LanguageCodesSet;
import com.google.cloud.translate.v3beta1.GlossaryInputConfig;
import com.google.cloud.translate.v3beta1.GlossaryName;
import com.google.cloud.translate.v3beta1.InputConfig;
import com.google.cloud.translate.v3beta1.LocationName;
import com.google.cloud.translate.v3beta1.OutputConfig;
import com.google.cloud.translate.v3beta1.SupportedLanguage;
import com.google.cloud.translate.v3beta1.SupportedLanguages;
import com.google.cloud.translate.v3beta1.TranslateTextGlossaryConfig;
import com.google.cloud.translate.v3beta1.TranslateTextRequest;
import com.google.cloud.translate.v3beta1.TranslateTextResponse;
import com.google.cloud.translate.v3beta1.TranslationServiceClient;
import com.google.cloud.translate.v3beta1.TranslationServiceClient.ListGlossariesPagedResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
/** Snippets for the beta features */
public class TranslateSnippetsBeta {
private TranslateSnippetsBeta() {}
/**
* Lists all the supported language codes.
*
* @param projectId - Id of the project.
* @param location - location name.
*/
// [START translate_list_codes_beta]
static SupportedLanguages listSupportedLanguages(String projectId, String location) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GetSupportedLanguagesRequest getSupportedLanguagesRequest =
GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).build();
// Call the API
ApiFuture<SupportedLanguages> future =
translationServiceClient
.getSupportedLanguagesCallable()
.futureCall(getSupportedLanguagesRequest);
SupportedLanguages response = future.get();
List<SupportedLanguage> languages = response.getLanguagesList();
for (SupportedLanguage language : languages) {
System.out.printf("Code: %s\n", language.getLanguageCode());
}
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_list_codes_beta]
/**
* Lists all the supported language names and codes.
*
* @param projectId - Id of the project.
* @param location - location name.
*/
// [START translate_list_language_names_beta]
static SupportedLanguages listSupportedLanguagesWithTarget(String projectId, String location) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GetSupportedLanguagesRequest getSupportedLanguagesRequest =
GetSupportedLanguagesRequest.newBuilder()
.setParent(locationName.toString())
.setDisplayLanguageCode("en-US")
.build();
// Call the API
ApiFuture<SupportedLanguages> future =
translationServiceClient
.getSupportedLanguagesCallable()
.futureCall(getSupportedLanguagesRequest);
SupportedLanguages response = future.get();
List<SupportedLanguage> languages = response.getLanguagesList();
for (SupportedLanguage language : languages) {
System.out.printf(
"Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode());
}
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_list_language_names_beta]
/**
* Detects the language of a given text.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param text - Text for detection
*/
// [START translate_detect_language_beta]
static DetectLanguageResponse detectLanguageOfText(
String projectId, String location, String text) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
DetectLanguageRequest detectLanguageRequest =
DetectLanguageRequest.newBuilder()
.setParent(locationName.toString())
.setMimeType("text/plain")
.setContent(text)
.build();
// Call the API
DetectLanguageResponse response =
translationServiceClient.detectLanguage(detectLanguageRequest);
System.out.format("Detected Language Code: %s", response.getLanguages(0).getLanguageCode());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_detect_language_beta]
// [START translate_translate_text_beta]
/**
* Translates a given text to a target language.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param text - Text for translation.
* @param sourceLanguageCode - Language code of text. e.g. "en"
* @param targetLanguageCode - Language code for translation. e.g. "sr"
*/
static TranslateTextResponse translateText(
String projectId,
String location,
String text,
String sourceLanguageCode,
String targetLanguageCode) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
TranslateTextRequest translateTextRequest =
TranslateTextRequest.newBuilder()
.setParent(locationName.toString())
.setMimeType("text/plain")
.setSourceLanguageCode(sourceLanguageCode)
.setTargetLanguageCode(targetLanguageCode)
.addContents(text)
.build();
// Call the API
TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest);
System.out.format(
"Translated Text: %s", response.getTranslationsList().get(0).getTranslatedText());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_translate_text_beta]
/**
* Translates a batch of texts on GCS and stores the result in a GCS location.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param sourceUri - Google Cloud Storage URI. Location where text is stored.
* @param destinationUri - Google Cloud Storage URI where result will be stored.
*/
// [START translate_batch_translate_text_beta]
static BatchTranslateResponse batchTranslateText(
String projectId, String location, String sourceUri, String destinationUri) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(sourceUri).build();
InputConfig inputConfig =
InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
GcsDestination gcsDestination =
GcsDestination.newBuilder().setOutputUriPrefix(destinationUri).build();
OutputConfig outputConfig =
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
BatchTranslateTextRequest batchTranslateTextRequest =
BatchTranslateTextRequest.newBuilder()
.setParent(locationName.toString())
.setSourceLanguageCode("en")
.addTargetLanguageCodes("sr")
.addInputConfigs(inputConfig)
.setOutputConfig(outputConfig)
.build();
// Call the API
BatchTranslateResponse response =
translationServiceClient
.batchTranslateTextAsync(batchTranslateTextRequest)
.get(300, TimeUnit.SECONDS);
System.out.printf("Total Characters: %d\n", response.getTotalCharacters());
System.out.printf("Translated Characters: %d\n", response.getTranslatedCharacters());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_batch_translate_text_beta]
/**
* Creates a glossary.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param name - Glossary name.
* @param gcsUri - Google Cloud Storage URI where glossary is stored in csv format.
*/
// [START translate_create_glossary_beta]
static Glossary createGlossary(String projectId, String location, String name, String gcsUri) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
LanguageCodesSet languageCodesSet =
LanguageCodesSet.newBuilder().addLanguageCodes("en").addLanguageCodes("es").build();
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(gcsUri).build();
GlossaryInputConfig glossaryInputConfig =
GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build();
GlossaryName glossaryName =
GlossaryName.newBuilder()
.setProject(projectId)
.setLocation(location)
.setGlossary(name)
.build();
Glossary glossary =
Glossary.newBuilder()
.setLanguageCodesSet(languageCodesSet)
.setInputConfig(glossaryInputConfig)
.setName(glossaryName.toString())
.build();
CreateGlossaryRequest request =
CreateGlossaryRequest.newBuilder()
.setParent(locationName.toString())
.setGlossary(glossary)
.build();
// Call the API
Glossary response =
translationServiceClient.createGlossaryAsync(request).get(300, TimeUnit.SECONDS);
System.out.format("Created: %s\n", response.getName());
System.out.format("Input Uri: %s\n", response.getInputConfig().getGcsSource());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_create_glossary_beta]
/**
* Lists all the glossaries for a given project.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param filter - criteria for listing glossaries.
*/
// [START translate_list_glossary_beta]
static ListGlossariesPagedResponse listGlossary(
String projectId, String location, String filter) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
ListGlossariesPagedResponse response =
translationServiceClient.listGlossaries(locationName.toString(), filter);
// Call the API
for (Glossary element : response.iterateAll()) {
System.out.format("Name: %s\n", element.getName());
System.out.format("Language Codes Set:\n");
System.out.format(
"Source Language Code: %s\n",
element.getLanguageCodesSet().getLanguageCodesList().get(0));
System.out.format(
"Target Language Code: %s\n",
element.getLanguageCodesSet().getLanguageCodesList().get(1));
System.out.format("Input Uri: %s\n", element.getInputConfig().getGcsSource());
}
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_list_glossary_beta]
/**
* Retrieves a glossary.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param name - Glossary name.
*/
// [START translate_get_glossary_beta]
static Glossary getGlossary(String projectId, String location, String name) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
GlossaryName glossaryName =
GlossaryName.newBuilder()
.setProject(projectId)
.setLocation(location)
.setGlossary(name)
.build();
// Call the API
Glossary response = translationServiceClient.getGlossary(glossaryName.toString());
System.out.format("Got: %s\n", response.getName());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_get_glossary_beta]
/**
* Translates a given text using a glossary.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param name - Glossary name.
* @param text - Text to be translated. e.g. "Hello World!"
* @param sourceLanguageCode - Language code of text. e.g. "en"
* @param targetLanguageCode - Language code for translation. e.g. "sr"
*/
// [START translate_translate_text_with_glossary_beta]
static TranslateTextResponse translateTextWithGlossary(
String projectId,
String location,
String name,
String text,
String sourceLanguageCode,
String targetLanguageCode) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
LocationName locationName =
LocationName.newBuilder().setProject(projectId).setLocation(location).build();
GlossaryName glossaryName =
GlossaryName.newBuilder()
.setProject(projectId)
.setLocation(location)
.setGlossary(name)
.build();
TranslateTextGlossaryConfig translateTextGlossaryConfig =
TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
TranslateTextRequest translateTextRequest =
TranslateTextRequest.newBuilder()
.setParent(locationName.toString())
.setMimeType("text/plain")
.setSourceLanguageCode(sourceLanguageCode)
.setTargetLanguageCode(targetLanguageCode)
.addContents(text)
.setGlossaryConfig(translateTextGlossaryConfig)
.build();
// Call the API
TranslateTextResponse response = translationServiceClient.translateText(translateTextRequest);
System.out.format(
"Translated text: %s", response.getGlossaryTranslationsList().get(0).getTranslatedText());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_translate_text_with_glossary_beta]
/**
* Deletes a glossary.
*
* @param projectId - Id of the project.
* @param location - location name.
* @param name - Glossary name.
*/
// [START translate_delete_glossary_beta]
static DeleteGlossaryResponse deleteGlossary(String projectId, String location, String name) {
try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
GlossaryName glossaryName =
GlossaryName.newBuilder()
.setProject(projectId)
.setLocation(location)
.setGlossary(name)
.build();
// Call the API
DeleteGlossaryResponse response =
translationServiceClient
.deleteGlossaryAsync(glossaryName.toString())
.get(300, TimeUnit.SECONDS);
System.out.format("Deleted: %s\n", response.getName());
return response;
} catch (Exception e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
// [END translate_delete_glossary_beta]
}