Skip to content

Commit ca5af43

Browse files
nnegreychingor13
authored andcommitted
samples: translate: add v3 glossary samples (#1936)
* translate: Add v3 glossary samples * add the samples * Clarify comment and add link for more info * Provide more descriptive comments
1 parent 655a3ea commit ca5af43

File tree

8 files changed

+663
-0
lines changed

8 files changed

+663
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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_create_glossary]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.translate.v3.CreateGlossaryMetadata;
22+
import com.google.cloud.translate.v3.CreateGlossaryRequest;
23+
import com.google.cloud.translate.v3.GcsSource;
24+
import com.google.cloud.translate.v3.Glossary;
25+
import com.google.cloud.translate.v3.GlossaryInputConfig;
26+
import com.google.cloud.translate.v3.GlossaryName;
27+
import com.google.cloud.translate.v3.LocationName;
28+
import com.google.cloud.translate.v3.TranslationServiceClient;
29+
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.concurrent.ExecutionException;
34+
35+
public class CreateGlossary {
36+
37+
public static void createGlossary() throws InterruptedException, ExecutionException, IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String projectId = "YOUR-PROJECT-ID";
40+
String glossaryId = "your-glossary-display-name";
41+
List<String> languageCodes = new ArrayList<>();
42+
languageCodes.add("your-language-code");
43+
String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt";
44+
createGlossary(projectId, glossaryId, languageCodes, inputUri);
45+
}
46+
47+
// Create a equivalent term sets glossary
48+
// https://cloud.google.com/translate/docs/advanced/glossary#format-glossary
49+
public static void createGlossary(
50+
String projectId, String glossaryId, List<String> languageCodes, String inputUri)
51+
throws IOException, ExecutionException, InterruptedException {
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
57+
// Supported Locations: `global`, [glossary location], or [model location]
58+
// Glossaries must be hosted in `us-central1`
59+
// Custom Models must use the same location as your model. (us-central1)
60+
String location = "us-central1";
61+
LocationName parent = LocationName.of(projectId, location);
62+
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
63+
64+
// Supported Languages: https://cloud.google.com/translate/docs/languages
65+
Glossary.LanguageCodesSet languageCodesSet =
66+
Glossary.LanguageCodesSet.newBuilder().addAllLanguageCodes(languageCodes).build();
67+
68+
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
69+
GlossaryInputConfig inputConfig =
70+
GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build();
71+
72+
Glossary glossary =
73+
Glossary.newBuilder()
74+
.setName(glossaryName.toString())
75+
.setLanguageCodesSet(languageCodesSet)
76+
.setInputConfig(inputConfig)
77+
.build();
78+
79+
CreateGlossaryRequest request =
80+
CreateGlossaryRequest.newBuilder()
81+
.setParent(parent.toString())
82+
.setGlossary(glossary)
83+
.build();
84+
85+
OperationFuture<Glossary, CreateGlossaryMetadata> future =
86+
client.createGlossaryAsync(request);
87+
88+
System.out.println("Waiting for operation to complete...");
89+
Glossary response = future.get();
90+
System.out.println("Created Glossary.");
91+
System.out.printf("Glossary name: %s\n", response.getName());
92+
System.out.printf("Entry count: %s\n", response.getEntryCount());
93+
System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri());
94+
}
95+
}
96+
}
97+
// [END translate_v3_create_glossary]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_delete_glossary]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.translate.v3.DeleteGlossaryMetadata;
22+
import com.google.cloud.translate.v3.DeleteGlossaryRequest;
23+
import com.google.cloud.translate.v3.DeleteGlossaryResponse;
24+
import com.google.cloud.translate.v3.GlossaryName;
25+
import com.google.cloud.translate.v3.TranslationServiceClient;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
public class DeleteGlossary {
31+
32+
public static void deleteGlossary() throws InterruptedException, ExecutionException, IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "YOUR-PROJECT-ID";
35+
String glossaryId = "your-glossary-display-name";
36+
deleteGlossary(projectId, glossaryId);
37+
}
38+
39+
// Delete a specific glossary based on the glossary ID
40+
public static void deleteGlossary(String projectId, String glossaryId)
41+
throws InterruptedException, ExecutionException, IOException {
42+
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources.
46+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
47+
// Supported Locations: `global`, [glossary location], or [model location]
48+
// Glossaries must be hosted in `us-central1`
49+
// Custom Models must use the same location as your model. (us-central1)
50+
GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId);
51+
DeleteGlossaryRequest request =
52+
DeleteGlossaryRequest.newBuilder().setName(glossaryName.toString()).build();
53+
54+
OperationFuture<DeleteGlossaryResponse, DeleteGlossaryMetadata> future =
55+
client.deleteGlossaryAsync(request);
56+
57+
System.out.println("Waiting for operation to complete...");
58+
DeleteGlossaryResponse response = future.get();
59+
System.out.format("Deleted Glossary: %s\n", response.getName());
60+
}
61+
}
62+
}
63+
// [END translate_v3_delete_glossary]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_get_glossary]
20+
import com.google.cloud.translate.v3.GetGlossaryRequest;
21+
import com.google.cloud.translate.v3.Glossary;
22+
import com.google.cloud.translate.v3.GlossaryName;
23+
import com.google.cloud.translate.v3.TranslationServiceClient;
24+
25+
import java.io.IOException;
26+
27+
public class GetGlossary {
28+
29+
public static void getGlossary() throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR-PROJECT-ID";
32+
String glossaryId = "your-glossary-display-name";
33+
getGlossary(projectId, glossaryId);
34+
}
35+
36+
// Get a particular glossary based on the glossary ID
37+
public static void getGlossary(String projectId, String glossaryId) throws IOException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
42+
// Supported Locations: `global`, [glossary location], or [model location]
43+
// Glossaries must be hosted in `us-central1`
44+
// Custom Models must use the same location as your model. (us-central1)
45+
GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId);
46+
GetGlossaryRequest request =
47+
GetGlossaryRequest.newBuilder().setName(glossaryName.toString()).build();
48+
49+
Glossary response = client.getGlossary(request);
50+
51+
System.out.printf("Glossary name: %s\n", response.getName());
52+
System.out.printf("Entry count: %s\n", response.getEntryCount());
53+
System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri());
54+
}
55+
}
56+
}
57+
// [END translate_v3_get_glossary]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_list_glossary]
20+
import com.google.cloud.translate.v3.Glossary;
21+
import com.google.cloud.translate.v3.ListGlossariesRequest;
22+
import com.google.cloud.translate.v3.LocationName;
23+
import com.google.cloud.translate.v3.TranslationServiceClient;
24+
25+
import java.io.IOException;
26+
27+
public class ListGlossaries {
28+
29+
public static void listGlossaries() throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR-PROJECT-ID";
32+
listGlossaries(projectId);
33+
}
34+
35+
// List all the glossaries in a specified location
36+
public static void listGlossaries(String projectId) throws IOException {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests. After completing all of your requests, call
39+
// the "close" method on the client to safely clean up any remaining background resources.
40+
try (TranslationServiceClient client = TranslationServiceClient.create()) {
41+
// Supported Locations: `global`, [glossary location], or [model location]
42+
// Glossaries must be hosted in `us-central1`
43+
// Custom Models must use the same location as your model. (us-central1)
44+
LocationName parent = LocationName.of(projectId, "us-central1");
45+
ListGlossariesRequest request =
46+
ListGlossariesRequest.newBuilder().setParent(parent.toString()).build();
47+
48+
for (Glossary responseItem : client.listGlossaries(request).iterateAll()) {
49+
System.out.printf("Glossary name: %s\n", responseItem.getName());
50+
System.out.printf("Entry count: %s\n", responseItem.getEntryCount());
51+
System.out.printf(
52+
"Input URI: %s\n", responseItem.getInputConfig().getGcsSource().getInputUri());
53+
}
54+
}
55+
}
56+
}
57+
// [END translate_v3_list_glossary]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.UUID;
28+
import java.util.concurrent.ExecutionException;
29+
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
37+
@RunWith(JUnit4.class)
38+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
39+
public class CreateGlossaryTests {
40+
41+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
42+
private static final String GLOSSARY_INPUT_URI =
43+
"gs://cloud-samples-data/translation/glossary_ja.csv";
44+
private static final String GLOSSARY_ID =
45+
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
46+
47+
private ByteArrayOutputStream bout;
48+
private PrintStream out;
49+
50+
private static void requireEnvVar(String varName) {
51+
assertNotNull(
52+
"Environment variable '%s' is required to perform these tests.".format(varName),
53+
System.getenv(varName));
54+
}
55+
56+
@BeforeClass
57+
public static void checkRequirements() {
58+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
59+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
60+
}
61+
62+
@Before
63+
public void setUp() {
64+
bout = new ByteArrayOutputStream();
65+
out = new PrintStream(bout);
66+
System.setOut(out);
67+
}
68+
69+
@After
70+
public void tearDown() throws InterruptedException, ExecutionException, IOException {
71+
// Delete the created glossary
72+
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
73+
74+
System.setOut(null);
75+
}
76+
77+
@Test
78+
public void testCreateGlossary() throws InterruptedException, ExecutionException, IOException {
79+
List<String> languageCodes = new ArrayList<>();
80+
languageCodes.add("en");
81+
languageCodes.add("ja");
82+
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI);
83+
84+
String got = bout.toString();
85+
assertThat(got).contains("Created");
86+
assertThat(got).contains(GLOSSARY_ID);
87+
assertThat(got).contains(GLOSSARY_INPUT_URI);
88+
}
89+
}

0 commit comments

Comments
 (0)