Skip to content

Commit f1def2a

Browse files
nnegreylesv
authored andcommitted
translate: add v3 translate text samples with glossary and model (#1941)
* translate: add v3 translate text samples with glossary and model * Update pom to java 11
1 parent 1d8d873 commit f1def2a

File tree

5 files changed

+464
-2
lines changed

5 files changed

+464
-2
lines changed

translate/cloud-client/pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
</parent>
3131

3232
<properties>
33-
<maven.compiler.target>1.8</maven.compiler.target>
34-
<maven.compiler.source>1.8</maven.compiler.source>
33+
<maven.compiler.target>11</maven.compiler.target>
34+
<maven.compiler.source>11</maven.compiler.source>
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3636
</properties>
3737

@@ -56,6 +56,10 @@ See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google
5656
<artifactId>google-cloud-translate</artifactId>
5757
</dependency>
5858
<!-- [END translate_java_dependencies] -->
59+
<dependency>
60+
<groupId>com.google.cloud</groupId>
61+
<artifactId>google-cloud-storage</artifactId>
62+
</dependency>
5963

6064
<!-- Test dependencies -->
6165
<dependency>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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_and_model]
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 BatchTranslateTextWithGlossaryAndModel {
37+
38+
public static void batchTranslateTextWithGlossaryAndModel()
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+
String modelId = "YOUR-MODEL-ID";
49+
batchTranslateTextWithGlossaryAndModel(
50+
projectId, sourceLanguage, targetLanguage, inputUri, outputUri, glossaryId, modelId);
51+
}
52+
53+
// Batch translate text with Model and Glossary
54+
public static void batchTranslateTextWithGlossaryAndModel(
55+
String projectId,
56+
String sourceLanguage,
57+
String targetLanguage,
58+
String inputUri,
59+
String outputUri,
60+
String glossaryId,
61+
String modelId)
62+
throws IOException, ExecutionException, InterruptedException {
63+
64+
// Initialize client that will be used to send requests. This client only needs to be created
65+
// once, and can be reused for multiple requests. After completing all of your requests, call
66+
// the "close" method on the client to safely clean up any remaining background resources.
67+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
68+
// Supported Locations: `global`, [glossary location], or [model location]
69+
// Glossaries must be hosted in `us-central1`
70+
// Custom Models must use the same location as your model. (us-central1)
71+
String location = "us-central1";
72+
LocationName parent = LocationName.of(projectId, location);
73+
74+
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
75+
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
76+
InputConfig inputConfig =
77+
InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
78+
79+
GcsDestination gcsDestination =
80+
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
81+
OutputConfig outputConfig =
82+
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
83+
84+
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
85+
TranslateTextGlossaryConfig glossaryConfig =
86+
TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
87+
88+
String modelPath =
89+
String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
90+
91+
BatchTranslateTextRequest request =
92+
BatchTranslateTextRequest.newBuilder()
93+
.setParent(parent.toString())
94+
.setSourceLanguageCode(sourceLanguage)
95+
.addTargetLanguageCodes(targetLanguage)
96+
.addInputConfigs(inputConfig)
97+
.setOutputConfig(outputConfig)
98+
.putGlossaries(targetLanguage, glossaryConfig)
99+
.putModels(targetLanguage, modelPath)
100+
.build();
101+
102+
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future =
103+
client.batchTranslateTextAsync(request);
104+
105+
System.out.println("Waiting for operation to complete...");
106+
BatchTranslateResponse response = future.get();
107+
// Display the translation for each input text provided
108+
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
109+
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
110+
}
111+
}
112+
}
113+
// [END translate_v3_batch_translate_text_with_glossary_and_model]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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_and_model]
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 TranslateTextWithGlossaryAndModel {
31+
32+
public static void translateTextWithGlossaryAndModel() 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+
String modelId = "YOUR-MODEL-ID";
41+
translateTextWithGlossaryAndModel(
42+
projectId, sourceLanguage, targetLanguage, text, glossaryId, modelId);
43+
}
44+
45+
// Translating Text with Glossary and Model
46+
public static void translateTextWithGlossaryAndModel(
47+
String projectId,
48+
String sourceLanguage,
49+
String targetLanguage,
50+
String text,
51+
String glossaryId,
52+
String modelId)
53+
throws IOException {
54+
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests. After completing all of your requests, call
57+
// the "close" method on the client to safely clean up any remaining background resources.
58+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
59+
// Supported Locations: `global`, [glossary location], or [model location]
60+
// Glossaries must be hosted in `us-central1`
61+
// Custom Models must use the same location as your model. (us-central1)
62+
String location = "us-central1";
63+
LocationName parent = LocationName.of(projectId, location);
64+
65+
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
66+
TranslateTextGlossaryConfig glossaryConfig =
67+
TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
68+
69+
String modelPath =
70+
String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
71+
72+
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
73+
TranslateTextRequest request =
74+
TranslateTextRequest.newBuilder()
75+
.setParent(parent.toString())
76+
.setMimeType("text/plain")
77+
.setSourceLanguageCode(sourceLanguage)
78+
.setTargetLanguageCode(targetLanguage)
79+
.addContents(text)
80+
.setGlossaryConfig(glossaryConfig)
81+
.setModel(modelPath)
82+
.build();
83+
84+
TranslateTextResponse response = client.translateText(request);
85+
86+
// Display the translation for each input text provided
87+
for (Translation translation : response.getGlossaryTranslationsList()) {
88+
System.out.printf("Translated text: %s\n", translation.getTranslatedText());
89+
}
90+
}
91+
}
92+
}
93+
// [END translate_v3_translate_text_with_glossary_and_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 sample. */
55+
@RunWith(JUnit4.class)
56+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
57+
public class BatchTranslateTextWithGlossaryAndModelTests {
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_custom_model_and_glossary.txt";
61+
private static final String GLOSSARY_ID =
62+
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
63+
private static final String MODEL_ID = "TRL2188848820815848149";
64+
private static final String GLOSSARY_INPUT_URI =
65+
"gs://cloud-samples-data/translation/glossary_ja.csv";
66+
67+
private ByteArrayOutputStream bout;
68+
private PrintStream out;
69+
70+
private static final void cleanUpBucket() {
71+
Storage storage = StorageOptions.getDefaultInstance().getService();
72+
Page<Blob> blobs =
73+
storage.list(
74+
PROJECT_ID,
75+
Storage.BlobListOption.currentDirectory(),
76+
Storage.BlobListOption.prefix("BATCH_TRANSLATION_OUTPUT/"));
77+
78+
deleteDirectory(storage, blobs);
79+
}
80+
81+
private static void deleteDirectory(Storage storage, Page<Blob> blobs) {
82+
for (Blob blob : blobs.iterateAll()) {
83+
System.out.println(blob.getBlobId());
84+
if (!blob.delete()) {
85+
Page<Blob> subBlobs =
86+
storage.list(
87+
PROJECT_ID,
88+
Storage.BlobListOption.currentDirectory(),
89+
Storage.BlobListOption.prefix(blob.getName()));
90+
91+
deleteDirectory(storage, subBlobs);
92+
}
93+
}
94+
}
95+
96+
private static void requireEnvVar(String varName) {
97+
assertNotNull(
98+
"Environment variable '%s' is required to perform these tests.".format(varName),
99+
System.getenv(varName));
100+
}
101+
102+
@BeforeClass
103+
public static void checkRequirements() {
104+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
105+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
106+
}
107+
108+
@Before
109+
public void setUp() throws InterruptedException, ExecutionException, IOException {
110+
// Create a glossary that can be used in the test
111+
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
112+
System.setOut(temp);
113+
List<String> languageCodes = new ArrayList<>();
114+
languageCodes.add("en");
115+
languageCodes.add("ja");
116+
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
117+
118+
bout = new ByteArrayOutputStream();
119+
out = new PrintStream(bout);
120+
System.setOut(out);
121+
}
122+
123+
@After
124+
public void tearDown() throws InterruptedException, ExecutionException, IOException {
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 testBatchTranslateTextWithGlossaryAndModel()
133+
throws InterruptedException, ExecutionException, IOException {
134+
BatchTranslateTextWithGlossaryAndModel.batchTranslateTextWithGlossaryAndModel(
135+
PROJECT_ID,
136+
"en",
137+
"ja",
138+
INPUT_URI,
139+
"gs://" + PROJECT_ID + "/BATCH_TRANSLATION_OUTPUT/",
140+
GLOSSARY_ID,
141+
MODEL_ID);
142+
String got = bout.toString();
143+
assertThat(got).contains("Total Characters: 25");
144+
}
145+
}

0 commit comments

Comments
 (0)