Search in sources :

Example 1 with BatchTranslateResponse

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);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3beta1.GcsSource) OutputConfig(com.google.cloud.translate.v3beta1.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3beta1.BatchTranslateTextRequest) InputConfig(com.google.cloud.translate.v3beta1.InputConfig) GlossaryInputConfig(com.google.cloud.translate.v3beta1.GlossaryInputConfig) GcsDestination(com.google.cloud.translate.v3beta1.GcsDestination) BatchTranslateResponse(com.google.cloud.translate.v3beta1.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 2 with BatchTranslateResponse

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);
}
Also used : Blob(com.google.cloud.storage.Blob) Storage(com.google.cloud.storage.Storage) BatchTranslateResponse(com.google.cloud.translate.v3beta1.BatchTranslateResponse) Test(org.junit.Test)

Aggregations

BatchTranslateResponse (com.google.cloud.translate.v3beta1.BatchTranslateResponse)2 Blob (com.google.cloud.storage.Blob)1 Storage (com.google.cloud.storage.Storage)1 BatchTranslateTextRequest (com.google.cloud.translate.v3beta1.BatchTranslateTextRequest)1 GcsDestination (com.google.cloud.translate.v3beta1.GcsDestination)1 GcsSource (com.google.cloud.translate.v3beta1.GcsSource)1 GlossaryInputConfig (com.google.cloud.translate.v3beta1.GlossaryInputConfig)1 InputConfig (com.google.cloud.translate.v3beta1.InputConfig)1 LocationName (com.google.cloud.translate.v3beta1.LocationName)1 OutputConfig (com.google.cloud.translate.v3beta1.OutputConfig)1 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)1 Test (org.junit.Test)1