Search in sources :

Example 96 with LocationName

use of com.google.cloud.kms.v1.LocationName 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 97 with LocationName

use of com.google.cloud.kms.v1.LocationName 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 98 with LocationName

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

Example 99 with LocationName

use of com.google.cloud.kms.v1.LocationName in project java-tasks by googleapis.

the class TasksSampleApplication method main.

/**
 * Runs the Cloud Tasks sample application.
 */
public static void main(String[] args) throws IOException {
    String projectId = ServiceOptions.getDefaultProjectId();
    LocationName parent = LocationName.of(projectId, LOCATION_ID);
    QueueName queueName = QueueName.of(parent.getProject(), parent.getLocation(), GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // Create queue
        Queue queue = Queue.newBuilder().setName(queueName.toString()).setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build()).build();
        CreateQueueRequest createQueueRequest = CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();
        Queue createdQueue = client.createQueue(createQueueRequest);
        System.out.println("Test queue ready: " + createdQueue);
        // Create task
        HttpRequest taskTarget = HttpRequest.newBuilder().setUrl("https://google.com").setHttpMethod(HttpMethod.GET).build();
        Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
        Task task = client.createTask(queueName, taskRequest);
        System.out.println("Created task: " + task);
        // Cleanup
        client.purgeQueue(queueName);
        System.out.println("Queue purged");
        client.deleteQueue(queueName);
        System.out.println("Queue deleted");
    }
}
Also used : HttpRequest(com.google.cloud.tasks.v2.HttpRequest) Task(com.google.cloud.tasks.v2.Task) CreateQueueRequest(com.google.cloud.tasks.v2.CreateQueueRequest) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) QueueName(com.google.cloud.tasks.v2.QueueName) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Example 100 with LocationName

use of com.google.cloud.kms.v1.LocationName in project native-image-support-java by GoogleCloudPlatform.

the class TasksSampleApplication method main.

/**
 * Runs the Cloud Tasks sample application.
 */
public static void main(String[] args) throws IOException {
    String projectId = ServiceOptions.getDefaultProjectId();
    LocationName parent = LocationName.of(projectId, "us-east1");
    QueueName queueName = QueueName.of(parent.getProject(), parent.getLocation(), GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // Create queue
        Queue queue = Queue.newBuilder().setName(queueName.toString()).setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build()).build();
        CreateQueueRequest createQueueRequest = CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();
        Queue createdQueue = client.createQueue(createQueueRequest);
        System.out.println("Test queue ready: " + createdQueue);
        // Create task
        HttpRequest taskTarget = HttpRequest.newBuilder().setUrl("https://google.com").setHttpMethod(HttpMethod.GET).build();
        Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
        Task task = client.createTask(queueName, taskRequest);
        System.out.println("Created task: " + task);
        // Cleanup
        client.purgeQueue(queueName);
        System.out.println("Queue purged");
        client.deleteQueue(queueName);
        System.out.println("Queue deleted");
    }
}
Also used : HttpRequest(com.google.cloud.tasks.v2.HttpRequest) Task(com.google.cloud.tasks.v2.Task) CreateQueueRequest(com.google.cloud.tasks.v2.CreateQueueRequest) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) QueueName(com.google.cloud.tasks.v2.QueueName) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Aggregations

Test (org.junit.Test)34 LocationName (com.google.privacy.dlp.v2.LocationName)22 OrganizationLocationName (com.google.privacy.dlp.v2.OrganizationLocationName)22 AbstractMessage (com.google.protobuf.AbstractMessage)18 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)17 LocationName (com.google.cloud.automl.v1.LocationName)17 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)16 StatusRuntimeException (io.grpc.StatusRuntimeException)14 LocationName (com.google.cloud.translate.v3.LocationName)13 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)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 LocationName (com.google.cloud.bigquery.connection.v1.LocationName)8 Dataset (com.google.cloud.automl.v1.Dataset)7 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)5