Skip to content

Commit fd2097d

Browse files
munkhuushmglgcf-owl-bot[bot]
authored andcommitted
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>
1 parent d167198 commit fd2097d

File tree

5 files changed

+390
-0
lines changed

5 files changed

+390
-0
lines changed
86.9 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translatev3beta1;
18+
19+
// [START translate_v3beta1_batch_translate_document]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.api.gax.longrunning.OperationTimedPollAlgorithm;
23+
import com.google.api.gax.retrying.RetrySettings;
24+
import com.google.api.gax.retrying.TimedRetryAlgorithm;
25+
import com.google.cloud.translate.v3beta1.BatchDocumentInputConfig;
26+
import com.google.cloud.translate.v3beta1.BatchDocumentOutputConfig;
27+
import com.google.cloud.translate.v3beta1.BatchTranslateDocumentMetadata;
28+
import com.google.cloud.translate.v3beta1.BatchTranslateDocumentRequest;
29+
import com.google.cloud.translate.v3beta1.BatchTranslateDocumentResponse;
30+
import com.google.cloud.translate.v3beta1.GcsDestination;
31+
import com.google.cloud.translate.v3beta1.GcsSource;
32+
import com.google.cloud.translate.v3beta1.LocationName;
33+
import com.google.cloud.translate.v3beta1.TranslationServiceClient;
34+
import com.google.cloud.translate.v3beta1.TranslationServiceSettings;
35+
import java.io.IOException;
36+
import java.util.concurrent.ExecutionException;
37+
import java.util.concurrent.ThreadLocalRandom;
38+
import java.util.concurrent.TimeUnit;
39+
import java.util.concurrent.TimeoutException;
40+
import org.threeten.bp.Duration;
41+
42+
public class BatchTranslateDocument {
43+
44+
public static void batchTranslateDocument()
45+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
46+
// TODO(developer): Replace these variables before running the sample.
47+
String projectId = "YOUR-PROJECT-ID";
48+
// Supported Languages: https://cloud.google.com/translate/docs/languages
49+
String sourceLanguage = "your-source-language";
50+
String targetLanguage = "your-target-language";
51+
String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt";
52+
String outputUri = "gs://your-gcs-bucket/path/to/results/";
53+
int timeout = 400; // timeout in seconds
54+
batchTranslateDocument(projectId, sourceLanguage, targetLanguage, inputUri, outputUri, timeout);
55+
}
56+
57+
// Batch translate document
58+
public static void batchTranslateDocument(
59+
String projectId,
60+
String sourceLanguage,
61+
String targetLanguage,
62+
String inputUri,
63+
String outputUri,
64+
int timeout)
65+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
66+
// refer to https://github.com/googleapis/java-translate/issues/613
67+
TranslationServiceSettings.Builder translationServiceSettingsBuilder =
68+
TranslationServiceSettings.newBuilder();
69+
TimedRetryAlgorithm timedRetryAlgorithm =
70+
OperationTimedPollAlgorithm.create(
71+
RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1000)).build());
72+
translationServiceSettingsBuilder
73+
.batchTranslateDocumentOperationSettings()
74+
.setPollingAlgorithm(timedRetryAlgorithm);
75+
TranslationServiceSettings translationServiceSettings =
76+
translationServiceSettingsBuilder.build();
77+
78+
// Initialize client that sends requests. This client can be reused for multiple requests. After
79+
// completing all of your requests, call the "close" method on the client to clean
80+
// up any remaining background resources.
81+
try (TranslationServiceClient client =
82+
TranslationServiceClient.create(translationServiceSettings)) {
83+
// The ``global`` location is not supported for batch translation
84+
LocationName parent = LocationName.of(projectId, "us-central1");
85+
86+
// Google Cloud Storage location for the source input. This can be a single file
87+
// (for example, ``gs://translation-test/input.docx``) or a wildcard
88+
// (for example, ``gs://translation-test/*``).
89+
// Supported file types: https://cloud.google.com/translate/docs/supported-formats
90+
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
91+
92+
BatchDocumentInputConfig batchDocumentInputConfig =
93+
BatchDocumentInputConfig.newBuilder().setGcsSource(gcsSource).build();
94+
95+
GcsDestination gcsDestination =
96+
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
97+
98+
BatchDocumentOutputConfig batchDocumentOutputConfig =
99+
BatchDocumentOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
100+
101+
BatchTranslateDocumentRequest request =
102+
BatchTranslateDocumentRequest.newBuilder()
103+
.setParent(parent.toString())
104+
.setSourceLanguageCode(sourceLanguage)
105+
.addTargetLanguageCodes(targetLanguage)
106+
.addInputConfigs(batchDocumentInputConfig)
107+
.setOutputConfig(batchDocumentOutputConfig)
108+
.build();
109+
110+
OperationFuture<BatchTranslateDocumentResponse, BatchTranslateDocumentMetadata> future =
111+
client.batchTranslateDocumentAsync(request);
112+
113+
System.out.println("Waiting for operation to complete...");
114+
115+
// random number between timeout
116+
long randomNumber = ThreadLocalRandom.current().nextInt(timeout, timeout + 100);
117+
BatchTranslateDocumentResponse response = future.get(randomNumber, TimeUnit.SECONDS);
118+
119+
System.out.println("Total Pages: " + response.getTotalPages());
120+
}
121+
}
122+
}
123+
// [END translate_v3beta1_batch_translate_document]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translatev3beta1;
18+
19+
// [START translate_v3beta1_translate_document]
20+
21+
import com.google.cloud.translate.v3beta1.DocumentInputConfig;
22+
import com.google.cloud.translate.v3beta1.LocationName;
23+
import com.google.cloud.translate.v3beta1.TranslateDocumentRequest;
24+
import com.google.cloud.translate.v3beta1.TranslateDocumentResponse;
25+
import com.google.cloud.translate.v3beta1.TranslationServiceClient;
26+
import com.google.protobuf.ByteString;
27+
import java.io.FileInputStream;
28+
import java.io.IOException;
29+
30+
public class TranslateDocument {
31+
32+
public static void translateDocument() throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "YOUR-PROJECT-ID";
35+
String filePath = "your-text";
36+
translateDocument(projectId, filePath);
37+
}
38+
39+
// Translating Document
40+
public static void translateDocument(String projectId, String filePath) throws IOException {
41+
// Initialize client that sends requests. This client can be reused for multiple requests. After
42+
// completing all of your requests, call the "close" method on the client to clean
43+
// up any remaining background resources.
44+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
45+
// The ``global`` location is not supported for batch translation
46+
LocationName parent = LocationName.of(projectId, "us-central1");
47+
48+
// Supported file types: https://cloud.google.com/translate/docs/supported-formats
49+
ByteString content = ByteString.readFrom(new FileInputStream(filePath));
50+
51+
DocumentInputConfig documentInputConfig =
52+
DocumentInputConfig.newBuilder()
53+
.setContent(content)
54+
.setMimeType("application/pdf")
55+
.build();
56+
57+
TranslateDocumentRequest request =
58+
TranslateDocumentRequest.newBuilder()
59+
.setParent(parent.toString())
60+
.setTargetLanguageCode("fr-FR")
61+
.setDocumentInputConfig(documentInputConfig)
62+
.build();
63+
64+
TranslateDocumentResponse response = client.translateDocument(request);
65+
66+
// To view translated document, write `response.document_translation.byte_stream_outputs`
67+
// to file. If not provided in the TranslationRequest, the translated file will only be
68+
// returned through a byte-stream and its output mime type will be the same as the input
69+
// file's mime type
70+
System.out.println(
71+
"Response: Detected Language Code - "
72+
+ response.getDocumentTranslation().getDetectedLanguageCode());
73+
}
74+
}
75+
}
76+
// [END translate_v3beta1_translate_document]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translatev3beta1;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.api.gax.paging.Page;
23+
import com.google.cloud.storage.Blob;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageOptions;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.io.PrintStream;
29+
import java.util.UUID;
30+
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeoutException;
32+
import org.junit.After;
33+
import org.junit.Before;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.junit.runners.JUnit4;
38+
39+
/** Tests for Batch Translate Document sample. */
40+
@RunWith(JUnit4.class)
41+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
42+
public class BatchTranslateDocumentTests {
43+
44+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
45+
private static final String INPUT_URI =
46+
"gs://java-docs-samples-testing/translation/invoice2.docx";
47+
private static final String PREFIX = "BATCH_DOC_TRANSLATION_OUTPUT_";
48+
private static final String OUTPUT_URI =
49+
String.format("gs://%s/%s%s/", PROJECT_ID, PREFIX, UUID.randomUUID());
50+
51+
private ByteArrayOutputStream bout;
52+
private PrintStream out;
53+
private PrintStream originalPrintStream;
54+
55+
private static void cleanUpBucket() {
56+
Storage storage = StorageOptions.getDefaultInstance().getService();
57+
Page<Blob> blobs =
58+
storage.list(
59+
PROJECT_ID,
60+
Storage.BlobListOption.currentDirectory(),
61+
Storage.BlobListOption.prefix(PREFIX));
62+
63+
deleteDirectory(storage, blobs);
64+
}
65+
66+
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
67+
for (Blob blob : blobs.iterateAll()) {
68+
System.out.println(blob.getBlobId());
69+
if (!blob.delete()) {
70+
Page<Blob> subBlobs =
71+
storage.list(
72+
PROJECT_ID,
73+
Storage.BlobListOption.currentDirectory(),
74+
Storage.BlobListOption.prefix(blob.getName()));
75+
76+
deleteDirectory(storage, subBlobs);
77+
}
78+
}
79+
}
80+
81+
private static void requireEnvVar(String varName) {
82+
assertNotNull(String.format(varName), System.getenv(varName));
83+
}
84+
85+
@BeforeClass
86+
public static void checkRequirements() {
87+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
88+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
89+
}
90+
91+
@Before
92+
public void setUp() {
93+
bout = new ByteArrayOutputStream();
94+
out = new PrintStream(bout);
95+
originalPrintStream = System.out;
96+
System.setOut(out);
97+
98+
// clean up bucket before the use to prevent concurrency issue.
99+
cleanUpBucket();
100+
}
101+
102+
@After
103+
public void tearDown() {
104+
cleanUpBucket();
105+
System.out.flush();
106+
System.setOut(originalPrintStream);
107+
}
108+
109+
@Test
110+
public void testBatchTranslateDocument()
111+
throws InterruptedException, ExecutionException, IOException, TimeoutException {
112+
BatchTranslateDocument.batchTranslateDocument(
113+
PROJECT_ID, "en-US", "fr-FR", INPUT_URI, OUTPUT_URI, 1000);
114+
115+
String got = bout.toString();
116+
assertThat(got).contains("Total Pages:");
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.translatev3beta1;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/** Tests for Translate Document sample. */
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class TranslateDocumentTests {
36+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
private PrintStream originalPrintStream;
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(String.format(varName), System.getenv(varName));
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
originalPrintStream = System.out;
57+
System.setOut(out);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
// restores print statements in the original method
63+
System.out.flush();
64+
System.setOut(originalPrintStream);
65+
}
66+
67+
@Test
68+
public void testTranslateText() throws IOException {
69+
TranslateDocument.translateDocument(PROJECT_ID, "resources/fake_invoice.pdf");
70+
String got = bout.toString();
71+
assertThat(got).contains("Response: Detected Language Code");
72+
}
73+
}

0 commit comments

Comments
 (0)