|
| 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] |
0 commit comments