-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: added new translate document samples (#608)
* samples: added new translate document samples * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * fixed lint and removed main * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * fixed import lint * removed mac files & added resources * chore: year bump * changed doc file * added polling algorithm * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * made requested the changes * format * made requested changes regarding comment & String concant Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d167198
commit fd2097d
Showing
5 changed files
with
390 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
123 changes: 123 additions & 0 deletions
123
translate/snippets/src/main/java/com/example/translatev3beta1/BatchTranslateDocument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.translatev3beta1; | ||
|
||
// [START translate_v3beta1_batch_translate_document] | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.api.gax.longrunning.OperationTimedPollAlgorithm; | ||
import com.google.api.gax.retrying.RetrySettings; | ||
import com.google.api.gax.retrying.TimedRetryAlgorithm; | ||
import com.google.cloud.translate.v3beta1.BatchDocumentInputConfig; | ||
import com.google.cloud.translate.v3beta1.BatchDocumentOutputConfig; | ||
import com.google.cloud.translate.v3beta1.BatchTranslateDocumentMetadata; | ||
import com.google.cloud.translate.v3beta1.BatchTranslateDocumentRequest; | ||
import com.google.cloud.translate.v3beta1.BatchTranslateDocumentResponse; | ||
import com.google.cloud.translate.v3beta1.GcsDestination; | ||
import com.google.cloud.translate.v3beta1.GcsSource; | ||
import com.google.cloud.translate.v3beta1.LocationName; | ||
import com.google.cloud.translate.v3beta1.TranslationServiceClient; | ||
import com.google.cloud.translate.v3beta1.TranslationServiceSettings; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import org.threeten.bp.Duration; | ||
|
||
public class BatchTranslateDocument { | ||
|
||
public static void batchTranslateDocument() | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR-PROJECT-ID"; | ||
// Supported Languages: https://cloud.google.com/translate/docs/languages | ||
String sourceLanguage = "your-source-language"; | ||
String targetLanguage = "your-target-language"; | ||
String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt"; | ||
String outputUri = "gs://your-gcs-bucket/path/to/results/"; | ||
int timeout = 400; // timeout in seconds | ||
batchTranslateDocument(projectId, sourceLanguage, targetLanguage, inputUri, outputUri, timeout); | ||
} | ||
|
||
// Batch translate document | ||
public static void batchTranslateDocument( | ||
String projectId, | ||
String sourceLanguage, | ||
String targetLanguage, | ||
String inputUri, | ||
String outputUri, | ||
int timeout) | ||
throws IOException, ExecutionException, InterruptedException, TimeoutException { | ||
// refer to https://github.com/googleapis/java-translate/issues/613 | ||
TranslationServiceSettings.Builder translationServiceSettingsBuilder = | ||
TranslationServiceSettings.newBuilder(); | ||
TimedRetryAlgorithm timedRetryAlgorithm = | ||
OperationTimedPollAlgorithm.create( | ||
RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1000)).build()); | ||
translationServiceSettingsBuilder | ||
.batchTranslateDocumentOperationSettings() | ||
.setPollingAlgorithm(timedRetryAlgorithm); | ||
TranslationServiceSettings translationServiceSettings = | ||
translationServiceSettingsBuilder.build(); | ||
|
||
// Initialize client that sends requests. This client can be reused for multiple requests. After | ||
// completing all of your requests, call the "close" method on the client to clean | ||
// up any remaining background resources. | ||
try (TranslationServiceClient client = | ||
TranslationServiceClient.create(translationServiceSettings)) { | ||
// The ``global`` location is not supported for batch translation | ||
LocationName parent = LocationName.of(projectId, "us-central1"); | ||
|
||
// Google Cloud Storage location for the source input. This can be a single file | ||
// (for example, ``gs://translation-test/input.docx``) or a wildcard | ||
// (for example, ``gs://translation-test/*``). | ||
// Supported file types: https://cloud.google.com/translate/docs/supported-formats | ||
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build(); | ||
|
||
BatchDocumentInputConfig batchDocumentInputConfig = | ||
BatchDocumentInputConfig.newBuilder().setGcsSource(gcsSource).build(); | ||
|
||
GcsDestination gcsDestination = | ||
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); | ||
|
||
BatchDocumentOutputConfig batchDocumentOutputConfig = | ||
BatchDocumentOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); | ||
|
||
BatchTranslateDocumentRequest request = | ||
BatchTranslateDocumentRequest.newBuilder() | ||
.setParent(parent.toString()) | ||
.setSourceLanguageCode(sourceLanguage) | ||
.addTargetLanguageCodes(targetLanguage) | ||
.addInputConfigs(batchDocumentInputConfig) | ||
.setOutputConfig(batchDocumentOutputConfig) | ||
.build(); | ||
|
||
OperationFuture<BatchTranslateDocumentResponse, BatchTranslateDocumentMetadata> future = | ||
client.batchTranslateDocumentAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
|
||
// random number between timeout | ||
long randomNumber = ThreadLocalRandom.current().nextInt(timeout, timeout + 100); | ||
BatchTranslateDocumentResponse response = future.get(randomNumber, TimeUnit.SECONDS); | ||
|
||
System.out.println("Total Pages: " + response.getTotalPages()); | ||
} | ||
} | ||
} | ||
// [END translate_v3beta1_batch_translate_document] |
76 changes: 76 additions & 0 deletions
76
translate/snippets/src/main/java/com/example/translatev3beta1/TranslateDocument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.translatev3beta1; | ||
|
||
// [START translate_v3beta1_translate_document] | ||
|
||
import com.google.cloud.translate.v3beta1.DocumentInputConfig; | ||
import com.google.cloud.translate.v3beta1.LocationName; | ||
import com.google.cloud.translate.v3beta1.TranslateDocumentRequest; | ||
import com.google.cloud.translate.v3beta1.TranslateDocumentResponse; | ||
import com.google.cloud.translate.v3beta1.TranslationServiceClient; | ||
import com.google.protobuf.ByteString; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
public class TranslateDocument { | ||
|
||
public static void translateDocument() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR-PROJECT-ID"; | ||
String filePath = "your-text"; | ||
translateDocument(projectId, filePath); | ||
} | ||
|
||
// Translating Document | ||
public static void translateDocument(String projectId, String filePath) throws IOException { | ||
// Initialize client that sends requests. This client can be reused for multiple requests. After | ||
// completing all of your requests, call the "close" method on the client to clean | ||
// 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()); | ||
} | ||
} | ||
} | ||
// [END translate_v3beta1_translate_document] |
118 changes: 118 additions & 0 deletions
118
...late/snippets/src/test/java/com/example/translatev3beta1/BatchTranslateDocumentTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.translatev3beta1; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import com.google.api.gax.paging.Page; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for Batch Translate Document sample. */ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class BatchTranslateDocumentTests { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String INPUT_URI = | ||
"gs://java-docs-samples-testing/translation/invoice2.docx"; | ||
private static final String PREFIX = "BATCH_DOC_TRANSLATION_OUTPUT_"; | ||
private static final String OUTPUT_URI = | ||
String.format("gs://%s/%s%s/", PROJECT_ID, PREFIX, UUID.randomUUID()); | ||
|
||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private PrintStream originalPrintStream; | ||
|
||
private static void cleanUpBucket() { | ||
Storage storage = StorageOptions.getDefaultInstance().getService(); | ||
Page<Blob> blobs = | ||
storage.list( | ||
PROJECT_ID, | ||
Storage.BlobListOption.currentDirectory(), | ||
Storage.BlobListOption.prefix(PREFIX)); | ||
|
||
deleteDirectory(storage, blobs); | ||
} | ||
|
||
private static void deleteDirectory(Storage storage, Page<Blob> blobs) { | ||
for (Blob blob : blobs.iterateAll()) { | ||
System.out.println(blob.getBlobId()); | ||
if (!blob.delete()) { | ||
Page<Blob> subBlobs = | ||
storage.list( | ||
PROJECT_ID, | ||
Storage.BlobListOption.currentDirectory(), | ||
Storage.BlobListOption.prefix(blob.getName())); | ||
|
||
deleteDirectory(storage, subBlobs); | ||
} | ||
} | ||
} | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull(String.format(varName), System.getenv(varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
originalPrintStream = System.out; | ||
System.setOut(out); | ||
|
||
// clean up bucket before the use to prevent concurrency issue. | ||
cleanUpBucket(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
cleanUpBucket(); | ||
System.out.flush(); | ||
System.setOut(originalPrintStream); | ||
} | ||
|
||
@Test | ||
public void testBatchTranslateDocument() | ||
throws InterruptedException, ExecutionException, IOException, TimeoutException { | ||
BatchTranslateDocument.batchTranslateDocument( | ||
PROJECT_ID, "en-US", "fr-FR", INPUT_URI, OUTPUT_URI, 1000); | ||
|
||
String got = bout.toString(); | ||
assertThat(got).contains("Total Pages:"); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
translate/snippets/src/test/java/com/example/translatev3beta1/TranslateDocumentTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.translatev3beta1; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for Translate Document sample. */ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class TranslateDocumentTests { | ||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
|
||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private PrintStream originalPrintStream; | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull(String.format(varName), System.getenv(varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
originalPrintStream = System.out; | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
// restores print statements in the original method | ||
System.out.flush(); | ||
System.setOut(originalPrintStream); | ||
} | ||
|
||
@Test | ||
public void testTranslateText() throws IOException { | ||
TranslateDocument.translateDocument(PROJECT_ID, "resources/fake_invoice.pdf"); | ||
String got = bout.toString(); | ||
assertThat(got).contains("Response: Detected Language Code"); | ||
} | ||
} |