use of com.google.cloud.tasks.v2.LocationName in project java-translate by googleapis.
the class CreateGlossary method createGlossary.
// Create a equivalent term sets glossary
// https://cloud.google.com/translate/docs/advanced/glossary#format-glossary
public static void createGlossary(String projectId, String glossaryId, List<String> languageCodes, String inputUri) throws IOException, ExecutionException, InterruptedException {
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
String location = "us-central1";
LocationName parent = LocationName.of(projectId, location);
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
// Supported Languages: https://cloud.google.com/translate/docs/languages
Glossary.LanguageCodesSet languageCodesSet = Glossary.LanguageCodesSet.newBuilder().addAllLanguageCodes(languageCodes).build();
// Configure the source of the file from a GCS bucket
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
GlossaryInputConfig inputConfig = GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build();
Glossary glossary = Glossary.newBuilder().setName(glossaryName.toString()).setLanguageCodesSet(languageCodesSet).setInputConfig(inputConfig).build();
CreateGlossaryRequest request = CreateGlossaryRequest.newBuilder().setParent(parent.toString()).setGlossary(glossary).build();
// Start an asynchronous request
OperationFuture<Glossary, CreateGlossaryMetadata> future = client.createGlossaryAsync(request);
System.out.println("Waiting for operation to complete...");
Glossary response = future.get();
System.out.println("Created Glossary.");
System.out.printf("Glossary name: %s\n", response.getName());
System.out.printf("Entry count: %s\n", response.getEntryCount());
System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri());
}
}
use of com.google.cloud.tasks.v2.LocationName in project java-translate by googleapis.
the class GetSupportedLanguages method getSupportedLanguages.
// Getting a list of supported language codes
public static void getSupportedLanguages(String projectId) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
LocationName parent = LocationName.of(projectId, "global");
GetSupportedLanguagesRequest request = GetSupportedLanguagesRequest.newBuilder().setParent(parent.toString()).build();
SupportedLanguages response = client.getSupportedLanguages(request);
// List language codes of supported languages
for (SupportedLanguage language : response.getLanguagesList()) {
System.out.printf("Language Code: %s\n", language.getLanguageCode());
}
}
}
use of com.google.cloud.tasks.v2.LocationName in project java-translate by googleapis.
the class ListGlossaries method listGlossaries.
// List all the glossaries in a specified location
public static void listGlossaries(String projectId) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
LocationName parent = LocationName.of(projectId, "us-central1");
ListGlossariesRequest request = ListGlossariesRequest.newBuilder().setParent(parent.toString()).build();
for (Glossary responseItem : client.listGlossaries(request).iterateAll()) {
System.out.printf("Glossary name: %s\n", responseItem.getName());
System.out.printf("Entry count: %s\n", responseItem.getEntryCount());
System.out.printf("Input URI: %s\n", responseItem.getInputConfig().getGcsSource().getInputUri());
}
}
}
use of com.google.cloud.tasks.v2.LocationName in project java-translate by googleapis.
the class TranslateText method translateText.
// [END translate_v3_translate_text_1]
// [START translate_v3_translate_text_2]
// Translate text to target language.
public static void translateText(String projectId, String targetLanguage, String text) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
LocationName parent = LocationName.of(projectId, "global");
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setTargetLanguageCode(targetLanguage).addContents(text).build();
TranslateTextResponse response = client.translateText(request);
// Display the translation for each input text provided
for (Translation translation : response.getTranslationsList()) {
System.out.printf("Translated text: %s\n", translation.getTranslatedText());
}
}
}
use of com.google.cloud.tasks.v2.LocationName in project java-translate by googleapis.
the class BatchTranslateDocument method batchTranslateDocument.
// Batch translate document
public static void batchTranslateDocument(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, int timeout) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// refer to https://github.com/googleapis/java-translate/issues/613
TranslationServiceSettings.Builder translationServiceSettingsBuilder = TranslationServiceSettings.newBuilder();
TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create(RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1000)).build());
translationServiceSettingsBuilder.batchTranslateDocumentOperationSettings().setPollingAlgorithm(timedRetryAlgorithm);
TranslationServiceSettings translationServiceSettings = translationServiceSettingsBuilder.build();
// up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create(translationServiceSettings)) {
// The ``global`` location is not supported for batch translation
LocationName parent = LocationName.of(projectId, "us-central1");
// Google Cloud Storage location for the source input. This can be a single file
// (for example, ``gs://translation-test/input.docx``) or a wildcard
// (for example, ``gs://translation-test/*``).
// Supported file types: https://cloud.google.com/translate/docs/supported-formats
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
BatchDocumentInputConfig batchDocumentInputConfig = BatchDocumentInputConfig.newBuilder().setGcsSource(gcsSource).build();
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
BatchDocumentOutputConfig batchDocumentOutputConfig = BatchDocumentOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
BatchTranslateDocumentRequest request = BatchTranslateDocumentRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(batchDocumentInputConfig).setOutputConfig(batchDocumentOutputConfig).build();
OperationFuture<BatchTranslateDocumentResponse, BatchTranslateDocumentMetadata> future = client.batchTranslateDocumentAsync(request);
System.out.println("Waiting for operation to complete...");
// random number between timeout
long randomNumber = ThreadLocalRandom.current().nextInt(timeout, timeout + 100);
BatchTranslateDocumentResponse response = future.get(randomNumber, TimeUnit.SECONDS);
System.out.println("Total Pages: " + response.getTotalPages());
}
}
Aggregations