use of com.google.cloud.translate.v3beta1.BatchTranslateResponse in project google-cloud-java by GoogleCloudPlatform.
the class TranslateSnippetsBeta method batchTranslateText.
// [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);
}
}
use of com.google.cloud.translate.v3beta1.BatchTranslateResponse in project google-cloud-java by GoogleCloudPlatform.
the class ITTranslateSnippetsBeta method test1_testBatchTranslateText.
@Test
public void test1_testBatchTranslateText() {
BatchTranslateResponse response = TranslateSnippetsBeta.batchTranslateText(projectId, "us-central1", "gs://cloud-samples-data/translation/text.txt", "gs://" + projectId + "/BATCH_TRANSLATION_OUTPUT/");
assertEquals(13, response.getTotalCharacters());
assertEquals(13, response.getTranslatedCharacters());
Storage storage = StorageOptions.getDefaultInstance().getService();
Page<Blob> blobs = storage.list(projectId, BlobListOption.currentDirectory(), BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/"));
deleteDirectory(storage, blobs);
}
Aggregations