Search in sources :

Example 26 with TranslationServiceClient

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

the class BatchTranslateText method batchTranslateText.

// Batch translate text
public static void batchTranslateText(String projectId, String sourceLanguage, String targetLanguage, String inputUri, String outputUri) 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: `us-central1`
        LocationName parent = LocationName.of(projectId, "us-central1");
        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();
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        BatchTranslateTextRequest request = BatchTranslateTextRequest.newBuilder().setParent(parent.toString()).setSourceLanguageCode(sourceLanguage).addTargetLanguageCodes(targetLanguage).addInputConfigs(inputConfig).setOutputConfig(outputConfig).build();
        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);
        System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
        System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
    }
}
Also used : BatchTranslateMetadata(com.google.cloud.translate.v3.BatchTranslateMetadata) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) GcsSource(com.google.cloud.translate.v3.GcsSource) OutputConfig(com.google.cloud.translate.v3.OutputConfig) BatchTranslateTextRequest(com.google.cloud.translate.v3.BatchTranslateTextRequest) InputConfig(com.google.cloud.translate.v3.InputConfig) GcsDestination(com.google.cloud.translate.v3.GcsDestination) BatchTranslateResponse(com.google.cloud.translate.v3.BatchTranslateResponse) LocationName(com.google.cloud.translate.v3.LocationName)

Example 27 with TranslationServiceClient

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

the class DetectLanguage method detectLanguage.

// Detecting the language of a text string
public static void detectLanguage(String projectId, 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
        DetectLanguageRequest request = DetectLanguageRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setContent(text).build();
        DetectLanguageResponse response = client.detectLanguage(request);
        // The most probable language is first.
        for (DetectedLanguage language : response.getLanguagesList()) {
            // The language detected
            System.out.printf("Language code: %s\n", language.getLanguageCode());
            // Confidence of detection result for this language
            System.out.printf("Confidence: %s\n", language.getConfidence());
        }
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) DetectLanguageResponse(com.google.cloud.translate.v3.DetectLanguageResponse) DetectedLanguage(com.google.cloud.translate.v3.DetectedLanguage) DetectLanguageRequest(com.google.cloud.translate.v3.DetectLanguageRequest) LocationName(com.google.cloud.translate.v3.LocationName)

Example 28 with TranslationServiceClient

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

the class TranslateTextWithGlossaryAndModel method translateTextWithGlossaryAndModel.

// Translating Text with Glossary and Model
public static void translateTextWithGlossaryAndModel(String projectId, String sourceLanguage, String targetLanguage, String text, String glossaryId, String modelId) 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)
        String location = "us-central1";
        LocationName parent = LocationName.of(projectId, location);
        GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
        TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
        String modelPath = String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
        // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
        TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setSourceLanguageCode(sourceLanguage).setTargetLanguageCode(targetLanguage).addContents(text).setGlossaryConfig(glossaryConfig).setModel(modelPath).build();
        TranslateTextResponse response = client.translateText(request);
        // Display the translation for each input text provided
        for (Translation translation : response.getGlossaryTranslationsList()) {
            System.out.printf("Translated text: %s\n", translation.getTranslatedText());
        }
    }
}
Also used : Translation(com.google.cloud.translate.v3.Translation) TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) TranslateTextRequest(com.google.cloud.translate.v3.TranslateTextRequest) GlossaryName(com.google.cloud.translate.v3.GlossaryName) TranslateTextGlossaryConfig(com.google.cloud.translate.v3.TranslateTextGlossaryConfig) TranslateTextResponse(com.google.cloud.translate.v3.TranslateTextResponse) LocationName(com.google.cloud.translate.v3.LocationName)

Example 29 with TranslationServiceClient

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

the class TranslateDocument method translateDocument.

// Translating Document
public static void translateDocument(String projectId, String filePath) throws IOException {
    // up any remaining background resources.
    try (TranslationServiceClient client = TranslationServiceClient.create()) {
        // The ``global`` location is not supported for batch translation
        LocationName parent = LocationName.of(projectId, "us-central1");
        // Supported file types: https://cloud.google.com/translate/docs/supported-formats
        ByteString content = ByteString.readFrom(new FileInputStream(filePath));
        DocumentInputConfig documentInputConfig = DocumentInputConfig.newBuilder().setContent(content).setMimeType("application/pdf").build();
        TranslateDocumentRequest request = TranslateDocumentRequest.newBuilder().setParent(parent.toString()).setTargetLanguageCode("fr-FR").setDocumentInputConfig(documentInputConfig).build();
        TranslateDocumentResponse response = client.translateDocument(request);
        // To view translated document, write `response.document_translation.byte_stream_outputs`
        // to file. If not provided in the TranslationRequest, the translated file will only be
        // returned through a byte-stream and its output mime type will be the same as the input
        // file's mime type
        System.out.println("Response: Detected Language Code - " + response.getDocumentTranslation().getDetectedLanguageCode());
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) ByteString(com.google.protobuf.ByteString) TranslateDocumentRequest(com.google.cloud.translate.v3beta1.TranslateDocumentRequest) TranslateDocumentResponse(com.google.cloud.translate.v3beta1.TranslateDocumentResponse) DocumentInputConfig(com.google.cloud.translate.v3beta1.DocumentInputConfig) FileInputStream(java.io.FileInputStream) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Aggregations

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