-
Notifications
You must be signed in to change notification settings - Fork 2.9k
translate: add v3 glossary samples #1936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8c42e2
translate: Add v3 glossary samples
nnegrey daf7d5d
add the samples
nnegrey be047dc
Clarify comment and add link for more info
nnegrey 71b7077
Provide more descriptive comments
nnegrey 787b177
Merge branch 'master' into translate-v3-glossary
nnegrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
96 changes: 96 additions & 0 deletions
96
translate/cloud-client/src/main/java/com/example/translate/CreateGlossary.java
This file contains hidden or 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,96 @@ | ||
/* | ||
* Copyright 2020 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.translate; | ||
|
||
// [START translate_v3_create_glossary] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.translate.v3.CreateGlossaryMetadata; | ||
import com.google.cloud.translate.v3.CreateGlossaryRequest; | ||
import com.google.cloud.translate.v3.GcsSource; | ||
import com.google.cloud.translate.v3.Glossary; | ||
import com.google.cloud.translate.v3.GlossaryInputConfig; | ||
import com.google.cloud.translate.v3.GlossaryName; | ||
import com.google.cloud.translate.v3.LocationName; | ||
import com.google.cloud.translate.v3.TranslationServiceClient; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class CreateGlossary { | ||
|
||
public static void createGlossary() throws InterruptedException, ExecutionException, IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR-PROJECT-ID"; | ||
String glossaryId = "your-glossary-display-name"; | ||
List<String> languageCodes = new ArrayList<>(); | ||
languageCodes.add("your-language-code"); | ||
String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt"; | ||
createGlossary(projectId, glossaryId, languageCodes, inputUri); | ||
} | ||
|
||
// Create Glossary | ||
public static void createGlossary( | ||
String projectId, String glossaryId, List<String> languageCodes, String inputUri) | ||
throws IOException, ExecutionException, InterruptedException { | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (TranslationServiceClient client = TranslationServiceClient.create()) { | ||
// Supported Locations: `global`, [glossary location], or [model location] | ||
// Glossaries must be hosted in `us-central1` | ||
// Custom Models must use the same location as your model. (us-central1) | ||
String location = "us-central1"; | ||
LocationName parent = LocationName.of(projectId, location); | ||
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId); | ||
|
||
// Supported Languages: https://cloud.google.com/translate/docs/languages | ||
Glossary.LanguageCodesSet languageCodesSet = | ||
Glossary.LanguageCodesSet.newBuilder().addAllLanguageCodes(languageCodes).build(); | ||
|
||
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build(); | ||
GlossaryInputConfig inputConfig = | ||
GlossaryInputConfig.newBuilder().setGcsSource(gcsSource).build(); | ||
|
||
Glossary glossary = | ||
Glossary.newBuilder() | ||
.setName(glossaryName.toString()) | ||
.setLanguageCodesSet(languageCodesSet) | ||
.setInputConfig(inputConfig) | ||
.build(); | ||
|
||
CreateGlossaryRequest request = | ||
CreateGlossaryRequest.newBuilder() | ||
.setParent(parent.toString()) | ||
.setGlossary(glossary) | ||
.build(); | ||
|
||
OperationFuture<Glossary, CreateGlossaryMetadata> future = | ||
client.createGlossaryAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
Glossary response = future.get(); | ||
System.out.println("Created Glossary."); | ||
System.out.printf("Glossary name: %s\n", response.getName()); | ||
System.out.printf("Entry count: %s\n", response.getEntryCount()); | ||
System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri()); | ||
} | ||
} | ||
} | ||
// [END translate_v3_create_glossary] |
63 changes: 63 additions & 0 deletions
63
translate/cloud-client/src/main/java/com/example/translate/DeleteGlossary.java
This file contains hidden or 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,63 @@ | ||
/* | ||
* Copyright 2020 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.translate; | ||
|
||
// [START translate_v3_delete_glossary] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.translate.v3.DeleteGlossaryMetadata; | ||
import com.google.cloud.translate.v3.DeleteGlossaryRequest; | ||
import com.google.cloud.translate.v3.DeleteGlossaryResponse; | ||
import com.google.cloud.translate.v3.GlossaryName; | ||
import com.google.cloud.translate.v3.TranslationServiceClient; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class DeleteGlossary { | ||
|
||
public static void deleteGlossary() throws InterruptedException, ExecutionException, IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR-PROJECT-ID"; | ||
String glossaryId = "your-glossary-display-name"; | ||
deleteGlossary(projectId, glossaryId); | ||
} | ||
|
||
/** Delete Glossary */ | ||
public static void deleteGlossary(String projectId, String glossaryId) | ||
throws InterruptedException, ExecutionException, IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (TranslationServiceClient client = TranslationServiceClient.create()) { | ||
// Supported Locations: `global`, [glossary location], or [model location] | ||
// Glossaries must be hosted in `us-central1` | ||
// Custom Models must use the same location as your model. (us-central1) | ||
GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId); | ||
DeleteGlossaryRequest request = | ||
DeleteGlossaryRequest.newBuilder().setName(glossaryName.toString()).build(); | ||
|
||
OperationFuture<DeleteGlossaryResponse, DeleteGlossaryMetadata> future = | ||
client.deleteGlossaryAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
DeleteGlossaryResponse response = future.get(); | ||
System.out.format("Deleted Glossary: %s\n", response.getName()); | ||
} | ||
} | ||
} | ||
// [END translate_v3_delete_glossary] |
58 changes: 58 additions & 0 deletions
58
translate/cloud-client/src/main/java/com/example/translate/GetGlossary.java
This file contains hidden or 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,58 @@ | ||
/* | ||
* Copyright 2020 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.translate; | ||
|
||
// [START translate_v3_get_glossary] | ||
import com.google.cloud.translate.v3.GetGlossaryRequest; | ||
import com.google.cloud.translate.v3.Glossary; | ||
import com.google.cloud.translate.v3.GlossaryName; | ||
import com.google.cloud.translate.v3.TranslationServiceClient; | ||
|
||
import java.io.IOException; | ||
|
||
public class GetGlossary { | ||
|
||
public static void getGlossary() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR-PROJECT-ID"; | ||
String glossaryId = "your-glossary-display-name"; | ||
getGlossary(projectId, glossaryId); | ||
} | ||
|
||
// Get Glossary | ||
public static void getGlossary(String projectId, String glossaryId) throws IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (TranslationServiceClient client = TranslationServiceClient.create()) { | ||
// Supported Locations: `global`, [glossary location], or [model location] | ||
// Glossaries must be hosted in `us-central1` | ||
// Custom Models must use the same location as your model. (us-central1) | ||
GlossaryName glossaryName = GlossaryName.of(projectId, "us-central1", glossaryId); | ||
GetGlossaryRequest request = | ||
GetGlossaryRequest.newBuilder().setName(glossaryName.toString()).build(); | ||
|
||
Glossary response = client.getGlossary(request); | ||
|
||
System.out.printf("Glossary name: %s\n", response.getName()); | ||
System.out.printf("Entry count: %s\n", response.getEntryCount()); | ||
System.out.printf("Input URI: %s\n", response.getInputConfig().getGcsSource().getInputUri()); | ||
} | ||
} | ||
} | ||
// [END translate_v3_get_glossary] |
58 changes: 58 additions & 0 deletions
58
translate/cloud-client/src/main/java/com/example/translate/ListGlossaries.java
This file contains hidden or 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,58 @@ | ||
/* | ||
* Copyright 2020 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.translate; | ||
|
||
// [START translate_v3_list_glossary] | ||
import com.google.cloud.translate.v3.Glossary; | ||
import com.google.cloud.translate.v3.ListGlossariesRequest; | ||
import com.google.cloud.translate.v3.LocationName; | ||
import com.google.cloud.translate.v3.TranslationServiceClient; | ||
|
||
import java.io.IOException; | ||
|
||
public class ListGlossaries { | ||
|
||
public static void listGlossaries() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR-PROJECT-ID"; | ||
listGlossaries(projectId); | ||
} | ||
|
||
// List Glossaries | ||
public static void listGlossaries(String projectId) throws IOException { | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (TranslationServiceClient client = TranslationServiceClient.create()) { | ||
// Supported Locations: `global`, [glossary location], or [model location] | ||
// Glossaries must be hosted in `us-central1` | ||
// Custom Models must use the same location as your model. (us-central1) | ||
LocationName parent = LocationName.of(projectId, "us-central1"); | ||
ListGlossariesRequest request = | ||
ListGlossariesRequest.newBuilder().setParent(parent.toString()).build(); | ||
|
||
for (Glossary responseItem : client.listGlossaries(request).iterateAll()) { | ||
System.out.printf("Glossary name: %s\n", responseItem.getName()); | ||
System.out.printf("Entry count: %s\n", responseItem.getEntryCount()); | ||
System.out.printf( | ||
"Input URI: %s\n", responseItem.getInputConfig().getGcsSource().getInputUri()); | ||
} | ||
} | ||
} | ||
} | ||
// [END translate_v3_list_glossary] |
89 changes: 89 additions & 0 deletions
89
translate/cloud-client/src/test/java/com/example/translate/CreateGlossaryTests.java
This file contains hidden or 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,89 @@ | ||
/* | ||
* Copyright 2020 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.translate; | ||
|
||
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 java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
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; | ||
|
||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class CreateGlossaryTests { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String GLOSSARY_INPUT_URI = | ||
"gs://cloud-samples-data/translation/glossary_ja.csv"; | ||
private static final String GLOSSARY_ID = | ||
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); | ||
|
||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
"Environment variable '%s' is required to perform these tests.".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); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() throws InterruptedException, ExecutionException, IOException { | ||
// Delete the created glossary | ||
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID); | ||
|
||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testCreateGlossary() throws InterruptedException, ExecutionException, IOException { | ||
List<String> languageCodes = new ArrayList<>(); | ||
languageCodes.add("en"); | ||
languageCodes.add("ja"); | ||
CreateGlossary.createGlossary(PROJECT_ID, GLOSSARY_ID, languageCodes, GLOSSARY_INPUT_URI); | ||
|
||
String got = bout.toString(); | ||
assertThat(got).contains("Created"); | ||
assertThat(got).contains(GLOSSARY_ID); | ||
assertThat(got).contains(GLOSSARY_INPUT_URI); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.