def translate_text_with_glossary( text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID", ): """Translates a given text using a glossary.""" client = translate.TranslationServiceClient() location = "us-central1" parent = f"projects/{project_id}/locations/{location}" glossary = client.glossary_path( project_id, "us-central1", glossary_id # The location of the glossary ) glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary) # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.translate_text( request={ "contents": [text], "target_language_code": "ja", "source_language_code": "en", "parent": parent, "glossary_config": glossary_config, }) print("Translated text: \n") for translation in response.glossary_translations: print("\t {}".format(translation.translated_text))
def batch_translate_text_with_glossary_and_model( input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", project_id="YOUR_PROJECT_ID", model_id="YOUR_MODEL_ID", glossary_id="YOUR_GLOSSARY_ID", ): """ Batch translate text with Glossary and Translation model """ client = translate.TranslationServiceClient() # Supported language codes: https://cloud.google.com/translate/docs/languages location = "us-central1" target_language_codes = ["ja"] gcs_source = {"input_uri": input_uri} # Optional. Can be "text/plain" or "text/html". mime_type = "text/plain" input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type} input_configs = [input_configs_element] gcs_destination = {"output_uri_prefix": output_uri} output_config = {"gcs_destination": gcs_destination} parent = f"projects/{project_id}/locations/{location}" model_path = "projects/{}/locations/{}/models/{}".format( project_id, "us-central1", model_id ) models = {"ja": model_path} glossary_path = client.glossary_path( project_id, "us-central1", glossary_id # The location of the glossary ) glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary_path) glossaries = {"ja": glossary_config} # target lang as key operation = client.batch_translate_text( request={ "parent": parent, "source_language_code": "en", "target_language_codes": target_language_codes, "input_configs": input_configs, "output_config": output_config, "models": models, "glossaries": glossaries, } ) print("Waiting for operation to complete...") response = operation.result() # Display the translation for each input text provided print("Total Characters: {}".format(response.total_characters)) print("Translated Characters: {}".format(response.translated_characters))
def batch_translate_text_with_glossary( input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID", timeout=180, ): """Translates a batch of texts on GCS and stores the result in a GCS location. Glossary is applied for translation.""" client = translate.TranslationServiceClient() # Supported language codes: https://cloud.google.com/translate/docs/languages location = "us-central1" # Supported file types: https://cloud.google.com/translate/docs/supported-formats gcs_source = {"input_uri": input_uri} input_configs_element = { "gcs_source": gcs_source, "mime_type": "text/plain" # Can be "text/plain" or "text/html". } gcs_destination = {"output_uri_prefix": output_uri} output_config = {"gcs_destination": gcs_destination} parent = f"projects/{project_id}/locations/{location}" # glossary is a custom dictionary Translation API uses # to translate the domain-specific terminology. glossary_path = client.glossary_path( project_id, "us-central1", glossary_id # The location of the glossary ) glossary_config = translate.TranslateTextGlossaryConfig( glossary=glossary_path) glossaries = {"ja": glossary_config} # target lang as key operation = client.batch_translate_text( request={ "parent": parent, "source_language_code": "en", "target_language_codes": ["ja"], # Up to 10 language codes here. "input_configs": [input_configs_element], "glossaries": glossaries, "output_config": output_config }) print(u"Waiting for operation to complete...") response = operation.result(timeout) print(u"Total Characters: {}".format(response.total_characters)) print(u"Translated Characters: {}".format(response.translated_characters))
def sample_batch_translate_text_with_glossary_and_model( input_uri, output_uri, project_id, location, target_language, source_language, model_id, glossary_id, ): """ Batch translate text with Glossary and Translation model """ client = translate.TranslationServiceClient() # TODO(developer): Uncomment and set the following variables # 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' # target_language = 'en' # source_language = 'de' # model_id = '{your-model-id}' # glossary_id = '[YOUR_GLOSSARY_ID]' target_language_codes = [target_language] gcs_source = {"input_uri": input_uri} # Optional. Can be "text/plain" or "text/html". mime_type = "text/plain" input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type} input_configs = [input_configs_element] gcs_destination = {"output_uri_prefix": output_uri} output_config = {"gcs_destination": gcs_destination} parent = f"projects/{project_id}/locations/{location}" model_path = "projects/{}/locations/{}/models/{}".format( project_id, "us-central1", model_id) models = {target_language: model_path} glossary_path = client.glossary_path( project_id, "us-central1", glossary_id # The location of the glossary ) glossary_config = translate.TranslateTextGlossaryConfig( glossary=glossary_path) glossaries = {"ja": glossary_config} # target lang as key operation = client.batch_translate_text( request={ "parent": parent, "source_language_code": "en", "target_language_codes": target_language_codes, "input_configs": input_configs, "output_config": output_config, "models": models, "glossaries": glossaries, }) print("Waiting for operation to complete...") response = operation.result() # Display the translation for each input text provided print("Total Characters: {}".format(response.total_characters)) print("Translated Characters: {}".format(response.translated_characters))