Search in sources :

Example 1 with LocationName

use of com.google.cloudbuild.v1.LocationName in project google-cloud-java by GoogleCloudPlatform.

the class TranslateSnippetsBeta method listSupportedLanguages.

/**
 * Lists all the supported language codes.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 */
// [START translate_list_codes_beta]
static SupportedLanguages listSupportedLanguages(String projectId, String location) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        GetSupportedLanguagesRequest getSupportedLanguagesRequest = GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).build();
        // Call the API
        ApiFuture<SupportedLanguages> future = translationServiceClient.getSupportedLanguagesCallable().futureCall(getSupportedLanguagesRequest);
        SupportedLanguages response = future.get();
        List<SupportedLanguage> languages = response.getLanguagesList();
        for (SupportedLanguage language : languages) {
            System.out.printf("Code: %s\n", language.getLanguageCode());
        }
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GetSupportedLanguagesRequest(com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest) SupportedLanguages(com.google.cloud.translate.v3beta1.SupportedLanguages) TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) SupportedLanguage(com.google.cloud.translate.v3beta1.SupportedLanguage) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 2 with LocationName

use of com.google.cloudbuild.v1.LocationName in project google-cloud-java by GoogleCloudPlatform.

the class TranslateSnippetsBeta method detectLanguageOfText.

// [END translate_list_language_names_beta]
/**
 * Detects the language of a given text.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 * @param text - Text for detection
 */
// [START translate_detect_language_beta]
static DetectLanguageResponse detectLanguageOfText(String projectId, String location, String text) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        DetectLanguageRequest detectLanguageRequest = DetectLanguageRequest.newBuilder().setParent(locationName.toString()).setMimeType("text/plain").setContent(text).build();
        // Call the API
        DetectLanguageResponse response = translationServiceClient.detectLanguage(detectLanguageRequest);
        System.out.format("Detected Language Code: %s", response.getLanguages(0).getLanguageCode());
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) DetectLanguageResponse(com.google.cloud.translate.v3beta1.DetectLanguageResponse) DetectLanguageRequest(com.google.cloud.translate.v3beta1.DetectLanguageRequest) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 3 with LocationName

use of com.google.cloudbuild.v1.LocationName in project google-cloud-java by GoogleCloudPlatform.

the class TranslateSnippetsBeta method listSupportedLanguagesWithTarget.

// [END translate_list_codes_beta]
/**
 * Lists all the supported language names and codes.
 *
 * @param projectId - Id of the project.
 * @param location - location name.
 */
// [START translate_list_language_names_beta]
static SupportedLanguages listSupportedLanguagesWithTarget(String projectId, String location) {
    try (TranslationServiceClient translationServiceClient = TranslationServiceClient.create()) {
        LocationName locationName = LocationName.newBuilder().setProject(projectId).setLocation(location).build();
        GetSupportedLanguagesRequest getSupportedLanguagesRequest = GetSupportedLanguagesRequest.newBuilder().setParent(locationName.toString()).setDisplayLanguageCode("en-US").build();
        // Call the API
        ApiFuture<SupportedLanguages> future = translationServiceClient.getSupportedLanguagesCallable().futureCall(getSupportedLanguagesRequest);
        SupportedLanguages response = future.get();
        List<SupportedLanguage> languages = response.getLanguagesList();
        for (SupportedLanguage language : languages) {
            System.out.printf("Name: %s, Code: %s\n", language.getDisplayName(), language.getLanguageCode());
        }
        return response;
    } catch (Exception e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GetSupportedLanguagesRequest(com.google.cloud.translate.v3beta1.GetSupportedLanguagesRequest) SupportedLanguages(com.google.cloud.translate.v3beta1.SupportedLanguages) TranslationServiceClient(com.google.cloud.translate.v3beta1.TranslationServiceClient) SupportedLanguage(com.google.cloud.translate.v3beta1.SupportedLanguage) LocationName(com.google.cloud.translate.v3beta1.LocationName)

Example 4 with LocationName

use of com.google.cloudbuild.v1.LocationName 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 5 with LocationName

use of com.google.cloudbuild.v1.LocationName 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)

Aggregations

Test (org.junit.Test)28 LocationName (com.google.privacy.dlp.v2.LocationName)22 OrganizationLocationName (com.google.privacy.dlp.v2.OrganizationLocationName)22 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)17 LocationName (com.google.cloud.automl.v1.LocationName)17 AbstractMessage (com.google.protobuf.AbstractMessage)14 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)13 LocationName (com.google.cloud.translate.v3.LocationName)13 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)13 StatusRuntimeException (io.grpc.StatusRuntimeException)13 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)12 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)12 LocationName (com.google.cloud.automl.v1beta1.LocationName)12 LocationName (com.google.cloud.translate.v3beta1.LocationName)10 TranslationServiceClient (com.google.cloud.translate.v3beta1.TranslationServiceClient)10 Model (com.google.cloud.automl.v1.Model)8 Dataset (com.google.cloud.automl.v1.Dataset)7 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 GcsSource (com.google.cloud.translate.v3.GcsSource)5 ArrayList (java.util.ArrayList)5