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());
}
}
}
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());
}
}
}
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());
}
}
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");
}
}
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");
}
}
Aggregations