Search in sources :

Example 1 with Glossary

use of com.google.cloud.translate.v3.Glossary in project google-cloud-java by GoogleCloudPlatform.

the class ITTranslateSnippetsBeta method test2_testGetGlossary.

@Test
public void test2_testGetGlossary() {
    Glossary response = TranslateSnippetsBeta.getGlossary(projectId, "us-central1", "glossary-testing");
    assertTrue(response.toString(), response.toString().contains("glossary-testing"));
}
Also used : Glossary(com.google.cloud.translate.v3beta1.Glossary) Test(org.junit.Test)

Example 2 with Glossary

use of com.google.cloud.translate.v3.Glossary in project google-cloud-java by GoogleCloudPlatform.

the class TranslateSnippetsBeta method getGlossary.

// [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);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) Glossary(com.google.cloud.translate.v3beta1.Glossary) GlossaryName(com.google.cloud.translate.v3beta1.GlossaryName)

Example 3 with Glossary

use of com.google.cloud.translate.v3.Glossary in project google-cloud-java by GoogleCloudPlatform.

the class TranslateSnippetsBeta method listGlossary.

// [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);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) Glossary(com.google.cloud.translate.v3beta1.Glossary) ListGlossariesPagedResponse(com.google.cloud.translate.v3beta1.TranslationServiceClient.ListGlossariesPagedResponse) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 4 with Glossary

use of com.google.cloud.translate.v3.Glossary in project java-translate by googleapis.

the class TranslationServiceClientTest method listGlossariesTest2.

@Test
public void listGlossariesTest2() throws Exception {
    Glossary responsesElement = Glossary.newBuilder().build();
    ListGlossariesResponse expectedResponse = ListGlossariesResponse.newBuilder().setNextPageToken("").addAllGlossaries(Arrays.asList(responsesElement)).build();
    mockTranslationService.addResponse(expectedResponse);
    String parent = "parent-995424086";
    ListGlossariesPagedResponse pagedListResponse = client.listGlossaries(parent);
    List<Glossary> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getGlossariesList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockTranslationService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListGlossariesRequest actualRequest = ((ListGlossariesRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListGlossariesPagedResponse(com.google.cloud.translate.v3.TranslationServiceClient.ListGlossariesPagedResponse) Test(org.junit.Test)

Example 5 with Glossary

use of com.google.cloud.translate.v3.Glossary in project java-translate by googleapis.

the class BatchTranslateTextWithGlossaryAndModel method batchTranslateTextWithGlossaryAndModel.

// Batch translate text with Model and Glossary
public static void batchTranslateTextWithGlossaryAndModel(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, String glossaryId, String modelId) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // 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);
        // Configure the source of the file from a GCS bucket
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
        // Configure where to store the output in a GCS bucket
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        // Configure the glossary used in the request
        GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
        TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
        // Configure the model used in the request
        String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
        // Build the request that will be sent to the API
        BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).putGlossaries(targetLanguage, glossaryConfig).putModels(targetLanguage, modelPath).build();
        // Start an asynchronous request
        OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future = client.batchTranslateTextAsync(request);
        System.out.println("Waiting for operation to complete...");
        // random number between 300 - 450 (maximum allowed seconds)
        long randomNumber = ThreadLocalRandom.current().nextInt(450, 600);
        BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
        // Display the translation for each input text provided
        System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
        System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3.GcsSource) TranslateTextGlossaryConfig(com.google.cloud.translate.v3.TranslateTextGlossaryConfig) BatchTranslateResponse(com.google.cloud.translate.v3.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3.LocationName) BatchTranslateMetadata(com.google.cloud.translate.v3.BatchTranslateMetadata) OutputConfig(com.google.cloud.translate.v3.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3.BatchTranslateTextRequest) GlossaryName(com.google.cloud.translate.v3.GlossaryName) InputConfig(com.google.cloud.translate.v3.InputConfig) GcsDestination(com.google.cloud.translate.v3.GcsDestination)

Aggregations

TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)14 LocationName (com.google.cloud.translate.v3.LocationName)12 GlossaryName (com.google.cloud.translate.v3.GlossaryName)7 Glossary (com.google.cloud.translate.v3beta1.Glossary)5 GcsSource (com.google.cloud.translate.v3.GcsSource)4 TranslateTextGlossaryConfig (com.google.cloud.translate.v3.TranslateTextGlossaryConfig)4 TranslateTextRequest (com.google.cloud.translate.v3.TranslateTextRequest)4 TranslateTextResponse (com.google.cloud.translate.v3.TranslateTextResponse)4 Translation (com.google.cloud.translate.v3.Translation)4 Test (org.junit.Test)4 BatchTranslateMetadata (com.google.cloud.translate.v3.BatchTranslateMetadata)3 BatchTranslateResponse (com.google.cloud.translate.v3.BatchTranslateResponse)3 BatchTranslateTextRequest (com.google.cloud.translate.v3.BatchTranslateTextRequest)3 GcsDestination (com.google.cloud.translate.v3.GcsDestination)3 Glossary (com.google.cloud.translate.v3.Glossary)3 InputConfig (com.google.cloud.translate.v3.InputConfig)3 OutputConfig (com.google.cloud.translate.v3.OutputConfig)3 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)3 GetSupportedLanguagesRequest (com.google.cloud.translate.v3.GetSupportedLanguagesRequest)2 SupportedLanguage (com.google.cloud.translate.v3.SupportedLanguage)2