|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright 2019 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "batch_translate_text_with_glossary") |
| 18 | + |
| 19 | +# To install the latest published package dependency, execute the following: |
| 20 | +# pip install google-cloud-translate |
| 21 | + |
| 22 | +# sample-metadata |
| 23 | +# title: Batch Translate Text with Glossary |
| 24 | +# description: Batch Translate Text with Glossary a given URI using a glossary. |
| 25 | +# usage: python3 translate_v3_batch_translate_text_with_glossary.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--glossary_id "[YOUR_GLOSSARY_ID]"] [--target_language en] [--source_language de] |
| 26 | + |
| 27 | +# [START translate_v3_batch_translate_text_with_glossary] |
| 28 | +from google.cloud import translate_v3 |
| 29 | + |
| 30 | +def sample_batch_translate_text_with_glossary( |
| 31 | + input_uri, output_uri, project_id, location, source_lang, target_lang, glossary_id |
| 32 | +): |
| 33 | + """Batch translate text with Glossary""" |
| 34 | + |
| 35 | + client = translate_v3.TranslationServiceClient() |
| 36 | + |
| 37 | + # TODO(developer): Uncomment and set the following variables |
| 38 | + # input_uri = 'gs://cloud-samples-data/text.txt' |
| 39 | + # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' |
| 40 | + # project_id = '[Google Cloud Project ID]' |
| 41 | + # location = 'us-central1' |
| 42 | + # source_lang = 'en' |
| 43 | + # target_lang = 'ja' |
| 44 | + # glossary_id = '[YOUR_GLOSSARY_ID]' |
| 45 | + target_language_codes = [target_lang] |
| 46 | + gcs_source = {"input_uri": input_uri} |
| 47 | + |
| 48 | + # Optional. Can be "text/plain" or "text/html". |
| 49 | + mime_type = "text/plain" |
| 50 | + input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type} |
| 51 | + input_configs = [input_configs_element] |
| 52 | + gcs_destination = {"output_uri_prefix": output_uri} |
| 53 | + output_config = {"gcs_destination": gcs_destination} |
| 54 | + input_uri, output_uri, project_id, location, source_lang, target_lang, glossary_id |
| 55 | + parent = client.location_path(project_id, location) |
| 56 | + |
| 57 | + glossary_path = client.glossary_path( |
| 58 | + project_id, |
| 59 | + 'us-central1', # The location of the glossary |
| 60 | + glossary_id) |
| 61 | + |
| 62 | + glossary_config = translate_v3.types.TranslateTextGlossaryConfig( |
| 63 | + glossary=glossary_path) |
| 64 | + |
| 65 | + glossaries = {"ja": glossary_config} #target lang as key |
| 66 | + |
| 67 | + operation = client.batch_translate_text( |
| 68 | + parent=parent, |
| 69 | + source_language_code=source_lang, |
| 70 | + target_language_codes=target_language_codes, |
| 71 | + input_configs=input_configs, |
| 72 | + glossaries=glossaries, |
| 73 | + output_config=output_config) |
| 74 | + |
| 75 | + print(u"Waiting for operation to complete...") |
| 76 | + response = operation.result(90) |
| 77 | + |
| 78 | + print(u"Total Characters: {}".format(response.total_characters)) |
| 79 | + print(u"Translated Characters: {}".format(response.translated_characters)) |
| 80 | + |
| 81 | + |
| 82 | +# [END translate_v3_batch_translate_text_with_glossary] |
| 83 | + |
| 84 | + |
| 85 | +def main(): |
| 86 | + import argparse |
| 87 | + |
| 88 | + parser = argparse.ArgumentParser() |
| 89 | + parser.add_argument( |
| 90 | + "--input_uri", type=str, default="gs://cloud-samples-data/translation/text_with_glossary.txt" |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--output_uri", type=str, default="gs://YOUR_BUCKET_ID/path_to_store_results/" |
| 94 | + ) |
| 95 | + parser.add_argument("--project_id", type=str, default="[Google Cloud Project ID]") |
| 96 | + parser.add_argument("--location", type=str, default="us-central1") |
| 97 | + parser.add_argument("--source_lang", type=str, default="en") |
| 98 | + parser.add_argument("--target_lang", type=str, default="ja") |
| 99 | + parser.add_argument( |
| 100 | + "--glossary_id", |
| 101 | + type=str, |
| 102 | + default="[YOUR_GLOSSARY_ID]", |
| 103 | + ) |
| 104 | + args = parser.parse_args() |
| 105 | + |
| 106 | + sample_batch_translate_text_with_glossary( |
| 107 | + args.input_uri, |
| 108 | + args.output_uri, |
| 109 | + args.project_id, |
| 110 | + args.location, |
| 111 | + args.source_lang, |
| 112 | + args.target_lang, |
| 113 | + args.glossary_id |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments