Skip to content

Commit b413551

Browse files
nnegreyanguillanneuf
authored andcommitted
samples: translate: add translate text with glossary samples (#1940)
* translate: add translate text with glossary samples * add some clarifying comments explain blocks of the code
1 parent 686ee9c commit b413551

File tree

4 files changed

+447
-0
lines changed

4 files changed

+447
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2020 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.translate;
18+
19+
// [START translate_v3_batch_translate_text_with_glossary]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.translate.v3.BatchTranslateMetadata;
22+
import com.google.cloud.translate.v3.BatchTranslateResponse;
23+
import com.google.cloud.translate.v3.BatchTranslateTextRequest;
24+
import com.google.cloud.translate.v3.GcsDestination;
25+
import com.google.cloud.translate.v3.GcsSource;
26+
import com.google.cloud.translate.v3.GlossaryName;
27+
import com.google.cloud.translate.v3.InputConfig;
28+
import com.google.cloud.translate.v3.LocationName;
29+
import com.google.cloud.translate.v3.OutputConfig;
30+
import com.google.cloud.translate.v3.TranslateTextGlossaryConfig;
31+
import com.google.cloud.translate.v3.TranslationServiceClient;
32+
33+
import java.io.IOException;
34+
import java.util.concurrent.ExecutionException;
35+
36+
public class BatchTranslateTextWithGlossary {
37+
38+
public static void batchTranslateTextWithGlossary()
39+
throws InterruptedException, ExecutionException, IOException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
String projectId = "YOUR-PROJECT-ID";
42+
// Supported Languages: https://cloud.google.com/translate/docs/languages
43+
String sourceLanguage = "your-source-language";
44+
String targetLanguage = "your-target-language";
45+
String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt";
46+
String outputUri = "gs://your-gcs-bucket/path/to/results/";
47+
String glossaryId = "your-glossary-display-name";
48+
batchTranslateTextWithGlossary(
49+
projectId, sourceLanguage, targetLanguage, inputUri, outputUri, glossaryId);
50+
}
51+
52+
// Batch Translate Text with a Glossary.
53+
public static void batchTranslateTextWithGlossary(
54+
String projectId,
55+
String sourceLanguage,
56+
String targetLanguage,
57+
String inputUri,
58+
String outputUri,
59+
String glossaryId)
60+
throws IOException, ExecutionException, InterruptedException {
61+
62+
// Initialize client that will be used to send requests. This client only needs to be created
63+
// once, and can be reused for multiple requests. After completing all of your requests, call
64+
// the "close" method on the client to safely clean up any remaining background resources.
65+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
66+
// Supported Locations: `global`, [glossary location], or [model location]
67+
// Glossaries must be hosted in `us-central1`
68+
// Custom Models must use the same location as your model. (us-central1)
69+
String location = "us-central1";
70+
LocationName parent = LocationName.of(projectId, location);
71+
72+
// Configure the source of the file from a GCS bucket
73+
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
74+
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
75+
InputConfig inputConfig =
76+
InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
77+
78+
// Configure where to store the output in a GCS bucket
79+
GcsDestination gcsDestination =
80+
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
81+
OutputConfig outputConfig =
82+
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
83+
84+
// Configure the glossary used in the request
85+
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
86+
TranslateTextGlossaryConfig glossaryConfig =
87+
TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
88+
89+
// Build the request that will be sent to the API
90+
BatchTranslateTextRequest request =
91+
BatchTranslateTextRequest.newBuilder()
92+
.setParent(parent.toString())
93+
.setSourceLanguageCode(sourceLanguage)
94+
.addTargetLanguageCodes(targetLanguage)
95+
.addInputConfigs(inputConfig)
96+
.setOutputConfig(outputConfig)
97+
.putGlossaries(targetLanguage, glossaryConfig)
98+
.build();
99+
100+
// Start an asynchronous request
101+
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future =
102+
client.batchTranslateTextAsync(request);
103+
104+
System.out.println("Waiting for operation to complete...");
105+
BatchTranslateResponse response = future.get();
106+
// Display the translation for each input text provided
107+
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
108+
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
109+
}
110+
}
111+
}
112+
// [END translate_v3_batch_translate_text_with_glossary]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2020 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.translate;
18+
19+
// [START translate_v3_translate_text_with_glossary]
20+
import com.google.cloud.translate.v3.GlossaryName;
21+
import com.google.cloud.translate.v3.LocationName;
22+
import com.google.cloud.translate.v3.TranslateTextGlossaryConfig;
23+
import com.google.cloud.translate.v3.TranslateTextRequest;
24+
import com.google.cloud.translate.v3.TranslateTextResponse;
25+
import com.google.cloud.translate.v3.Translation;
26+
import com.google.cloud.translate.v3.TranslationServiceClient;
27+
28+
import java.io.IOException;
29+
30+
public class TranslateTextWithGlossary {
31+
32+
public static void translateTextWithGlossary() throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "YOUR-PROJECT-ID";
35+
// Supported Languages: https://cloud.google.com/translate/docs/languages
36+
String sourceLanguage = "your-source-language";
37+
String targetLanguage = "your-target-language";
38+
String text = "your-text";
39+
String glossaryId = "your-glossary-display-name";
40+
translateTextWithGlossary(projectId, sourceLanguage, targetLanguage, text, glossaryId);
41+
}
42+
43+
// Translates a given text using a glossary.
44+
public static void translateTextWithGlossary(
45+
String projectId,
46+
String sourceLanguage,
47+
String targetLanguage,
48+
String text,
49+
String glossaryId)
50+
throws IOException {
51+
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests. After completing all of your requests, call
54+
// the "close" method on the client to safely clean up any remaining background resources.
55+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
56+
// Supported Locations: `global`, [glossary location], or [model location]
57+
// Glossaries must be hosted in `us-central1`
58+
// Custom Models must use the same location as your model. (us-central1)
59+
String location = "us-central1";
60+
LocationName parent = LocationName.of(projectId, location);
61+
62+
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
63+
TranslateTextGlossaryConfig glossaryConfig =
64+
TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
65+
66+
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
67+
TranslateTextRequest request =
68+
TranslateTextRequest.newBuilder()
69+
.setParent(parent.toString())
70+
.setMimeType("text/plain")
71+
.setSourceLanguageCode(sourceLanguage)
72+
.setTargetLanguageCode(targetLanguage)
73+
.addContents(text)
74+
.setGlossaryConfig(glossaryConfig)
75+
.build();
76+
77+
TranslateTextResponse response = client.translateText(request);
78+
79+
// Display the translation for each input text provided
80+
for (Translation translation : response.getGlossaryTranslationsList()) {
81+
System.out.printf("Translated text: %s\n", translation.getTranslatedText());
82+
}
83+
}
84+
}
85+
}
86+
// [END translate_v3_translate_text_with_glossary]
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2020 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.translate;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.api.gax.longrunning.OperationFuture;
23+
import com.google.api.gax.paging.Page;
24+
import com.google.cloud.storage.Blob;
25+
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.StorageOptions;
27+
import com.google.cloud.translate.v3.CreateGlossaryMetadata;
28+
import com.google.cloud.translate.v3.CreateGlossaryRequest;
29+
import com.google.cloud.translate.v3.DeleteGlossaryMetadata;
30+
import com.google.cloud.translate.v3.DeleteGlossaryRequest;
31+
import com.google.cloud.translate.v3.DeleteGlossaryResponse;
32+
import com.google.cloud.translate.v3.GcsSource;
33+
import com.google.cloud.translate.v3.Glossary;
34+
import com.google.cloud.translate.v3.GlossaryInputConfig;
35+
import com.google.cloud.translate.v3.GlossaryName;
36+
import com.google.cloud.translate.v3.LocationName;
37+
import com.google.cloud.translate.v3.TranslationServiceClient;
38+
39+
import java.io.ByteArrayOutputStream;
40+
import java.io.IOException;
41+
import java.io.PrintStream;
42+
import java.util.ArrayList;
43+
import java.util.List;
44+
import java.util.UUID;
45+
import java.util.concurrent.ExecutionException;
46+
47+
import org.junit.After;
48+
import org.junit.Before;
49+
import org.junit.BeforeClass;
50+
import org.junit.Test;
51+
import org.junit.runner.RunWith;
52+
import org.junit.runners.JUnit4;
53+
54+
/** Tests for Batch Translate Text With Glossary and Model sample. */
55+
@RunWith(JUnit4.class)
56+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
57+
public class BatchTranslateTextWithGlossaryTests {
58+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
59+
private static final String INPUT_URI =
60+
"gs://cloud-samples-data/translation/text_with_glossary.txt";
61+
private static final String GLOSSARY_INPUT_URI =
62+
"gs://cloud-samples-data/translation/glossary_ja.csv";
63+
private static final String GLOSSARY_ID =
64+
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
65+
66+
private ByteArrayOutputStream bout;
67+
private PrintStream out;
68+
69+
private static final void cleanUpBucket() {
70+
Storage storage = StorageOptions.getDefaultInstance().getService();
71+
Page<Blob> blobs =
72+
storage.list(
73+
PROJECT_ID,
74+
Storage.BlobListOption.currentDirectory(),
75+
Storage.BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/"));
76+
77+
deleteDirectory(storage, blobs);
78+
}
79+
80+
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
81+
for (Blob blob : blobs.iterateAll()) {
82+
System.out.println(blob.getBlobId());
83+
if (!blob.delete()) {
84+
Page<Blob> subBlobs =
85+
storage.list(
86+
PROJECT_ID,
87+
Storage.BlobListOption.currentDirectory(),
88+
Storage.BlobListOption.prefix(blob.getName()));
89+
90+
deleteDirectory(storage, subBlobs);
91+
}
92+
}
93+
}
94+
95+
private static void requireEnvVar(String varName) {
96+
assertNotNull(
97+
"Environment variable '%s' is required to perform these tests.".format(varName),
98+
System.getenv(varName));
99+
}
100+
101+
@BeforeClass
102+
public static void checkRequirements() {
103+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
104+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
105+
}
106+
107+
@Before
108+
public void setUp() throws InterruptedException, ExecutionException, IOException {
109+
// Create a glossary that can be used in the test
110+
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
111+
System.setOut(temp);
112+
List<String> languageCodes = new ArrayList<>();
113+
languageCodes.add("en");
114+
languageCodes.add("ja");
115+
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
116+
117+
bout = new ByteArrayOutputStream();
118+
out = new PrintStream(bout);
119+
System.setOut(out);
120+
}
121+
122+
@After
123+
public void tearDown() throws InterruptedException, ExecutionException, IOException {
124+
// Clean up
125+
cleanUpBucket();
126+
// Delete the created glossary
127+
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
128+
System.setOut(null);
129+
}
130+
131+
@Test
132+
public void testBatchTranslateTextWithGlossary()
133+
throws InterruptedException, ExecutionException, IOException {
134+
BatchTranslateTextWithGlossary.batchTranslateTextWithGlossary(
135+
PROJECT_ID,
136+
"en",
137+
"ja",
138+
INPUT_URI,
139+
"gs://" + PROJECT_ID + "/BATCH_TRANSLATION_OUTPUT/",
140+
GLOSSARY_ID);
141+
String got = bout.toString();
142+
assertThat(got).contains("Total Characters: 9");
143+
}
144+
}

0 commit comments

Comments
 (0)