Search in sources :

Example 1 with GlossaryName

use of com.google.cloud.translate.v3.GlossaryName 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 2 with GlossaryName

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

the class TranslateSnippetsBeta method translateTextWithGlossary.

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

Example 3 with GlossaryName

use of com.google.cloud.translate.v3.GlossaryName 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)

Example 4 with GlossaryName

use of com.google.cloud.translate.v3.GlossaryName 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());
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3.GcsSource) CreateGlossaryRequest(com.google.cloud.translate.v3.CreateGlossaryRequest) CreateGlossaryMetadata(com.google.cloud.translate.v3.CreateGlossaryMetadata) GlossaryInputConfig(com.google.cloud.translate.v3.GlossaryInputConfig) Glossary(com.google.cloud.translate.v3.Glossary) GlossaryName(com.google.cloud.translate.v3.GlossaryName) LocationName(com.google.cloud.translate.v3.LocationName)

Example 5 with GlossaryName

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

the class BatchTranslateTextWithGlossary method batchTranslateTextWithGlossary.

// Batch Translate Text with a Glossary.
public static void batchTranslateTextWithGlossary(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri, String glossaryId) 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();
        // 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).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

GlossaryName (com.google.cloud.translate.v3.GlossaryName)7 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)7 LocationName (com.google.cloud.translate.v3.LocationName)5 TranslateTextGlossaryConfig (com.google.cloud.translate.v3.TranslateTextGlossaryConfig)4 GlossaryName (com.google.cloud.translate.v3beta1.GlossaryName)4 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)4 GcsSource (com.google.cloud.translate.v3.GcsSource)3 BatchTranslateMetadata (com.google.cloud.translate.v3.BatchTranslateMetadata)2 BatchTranslateResponse (com.google.cloud.translate.v3.BatchTranslateResponse)2 BatchTranslateTextRequest (com.google.cloud.translate.v3.BatchTranslateTextRequest)2 GcsDestination (com.google.cloud.translate.v3.GcsDestination)2 Glossary (com.google.cloud.translate.v3.Glossary)2 InputConfig (com.google.cloud.translate.v3.InputConfig)2 OutputConfig (com.google.cloud.translate.v3.OutputConfig)2 TranslateTextRequest (com.google.cloud.translate.v3.TranslateTextRequest)2 TranslateTextResponse (com.google.cloud.translate.v3.TranslateTextResponse)2 Translation (com.google.cloud.translate.v3.Translation)2 Glossary (com.google.cloud.translate.v3beta1.Glossary)2 LocationName (com.google.cloud.translate.v3beta1.LocationName)2 CreateGlossaryMetadata (com.google.cloud.translate.v3.CreateGlossaryMetadata)1