diff --git a/samples/snippets/translate_v3_create_glossary.py b/samples/snippets/translate_v3_create_glossary.py new file mode 100644 index 00000000..b42d33c8 --- /dev/null +++ b/samples/snippets/translate_v3_create_glossary.py @@ -0,0 +1,59 @@ +# 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. + +# [START translate_v3_create_glossary] +from google.cloud import translate_v3 as translate + + +def create_glossary( + project_id="YOUR_PROJECT_ID", + input_uri="YOUR_INPUT_URI", + glossary_id="YOUR_GLOSSARY_ID", +): + """ + Create a equivalent term sets glossary. Glossary can be words or + short phrases (usually fewer than five words). + https://cloud.google.com/translate/docs/advanced/glossary#format-glossary + """ + client = translate.TranslationServiceClient() + + # Supported language codes: https://cloud.google.com/translate/docs/languages + source_lang_code = "en" + target_lang_code = "ja" + location = "us-central1" # The location of the glossary + + name = client.glossary_path(project_id, location, glossary_id) + language_codes_set = translate.types.Glossary.LanguageCodesSet( + language_codes=[source_lang_code, target_lang_code] + ) + + gcs_source = translate.types.GcsSource(input_uri=input_uri) + + input_config = translate.types.GlossaryInputConfig(gcs_source=gcs_source) + + glossary = translate.types.Glossary( + name=name, language_codes_set=language_codes_set, input_config=input_config + ) + + parent = client.location_path(project_id, location) + # glossary is a custom dictionary Translation API uses + # to translate the domain-specific terminology. + operation = client.create_glossary(parent=parent, glossary=glossary) + + result = operation.result(timeout=90) + print("Created: {}".format(result.name)) + print("Input Uri: {}".format(result.input_config.gcs_source.input_uri)) + + +# [END translate_v3_create_glossary] diff --git a/samples/snippets/translate_v3_create_glossary_test.py b/samples/snippets/translate_v3_create_glossary_test.py new file mode 100644 index 00000000..bf9f08cb --- /dev/null +++ b/samples/snippets/translate_v3_create_glossary_test.py @@ -0,0 +1,38 @@ +# 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. + +import os +import translate_v3_create_glossary +import translate_v3_delete_glossary +import uuid + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" + + +def test_create_glossary(capsys): + glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) + translate_v3_create_glossary.create_glossary( + PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id + ) + out, _ = capsys.readouterr() + # assert + assert "Created:" in out + assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out + + # clean up after use + try: + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass diff --git a/samples/snippets/translate_v3_delete_glossary.py b/samples/snippets/translate_v3_delete_glossary.py new file mode 100644 index 00000000..da1acb40 --- /dev/null +++ b/samples/snippets/translate_v3_delete_glossary.py @@ -0,0 +1,32 @@ +# 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. + +# [START translate_v3_delete_glossary] +from google.cloud import translate_v3 as translate + + +def delete_glossary( + project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID" +): + """Delete a specific glossary based on the glossary ID.""" + client = translate.TranslationServiceClient() + + parent = client.glossary_path(project_id, "us-central1", glossary_id) + + operation = client.delete_glossary(parent) + result = operation.result(timeout=90) + print("Deleted: {}".format(result.name)) + + +# [END translate_v3_delete_glossary] diff --git a/samples/snippets/translate_v3_delete_glossary_test.py b/samples/snippets/translate_v3_delete_glossary_test.py new file mode 100644 index 00000000..1c1fb2da --- /dev/null +++ b/samples/snippets/translate_v3_delete_glossary_test.py @@ -0,0 +1,34 @@ +# 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. + +import os +import translate_v3_create_glossary +import translate_v3_delete_glossary +import uuid + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" + + +def test_delete_glossary(capsys): + # setup + glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) + translate_v3_create_glossary.create_glossary( + PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id + ) + + # assert + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + out, _ = capsys.readouterr() + assert "Deleted:" in out diff --git a/samples/snippets/translate_v3_get_glossary.py b/samples/snippets/translate_v3_get_glossary.py new file mode 100644 index 00000000..99753136 --- /dev/null +++ b/samples/snippets/translate_v3_get_glossary.py @@ -0,0 +1,32 @@ +# 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 +# +# https://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. + +# [START translate_v3_get_glossary] +from google.cloud import translate_v3 as translate + + +def get_glossary(project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID"): + """Get a particular glossary based on the glossary ID.""" + + client = translate.TranslationServiceClient() + + name = client.glossary_path(project_id, "us-central1", glossary_id) + + response = client.get_glossary(name) + print(u"Glossary name: {}".format(response.name)) + print(u"Entry count: {}".format(response.entry_count)) + print(u"Input URI: {}".format(response.input_config.gcs_source.input_uri)) + + +# [END translate_v3_get_glossary] diff --git a/samples/snippets/translate_v3_get_glossary_test.py b/samples/snippets/translate_v3_get_glossary_test.py new file mode 100644 index 00000000..7a4aa167 --- /dev/null +++ b/samples/snippets/translate_v3_get_glossary_test.py @@ -0,0 +1,45 @@ +# 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. + +import os +import pytest +import translate_v3_create_glossary +import translate_v3_delete_glossary +import translate_v3_get_glossary +import uuid + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" + + +@pytest.fixture(scope="session") +def glossary(): + """Get the ID of a glossary available to session (do not mutate/delete).""" + glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) + translate_v3_create_glossary.create_glossary( + PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id + ) + + yield glossary_id + + try: + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass + + +def test_get_glossary(capsys, glossary): + translate_v3_get_glossary.get_glossary(PROJECT_ID, glossary) + out, _ = capsys.readouterr() + assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/samples/snippets/translate_v3_translate_text.py b/samples/snippets/translate_v3_translate_text.py index b6820aaf..c5034c94 100644 --- a/samples/snippets/translate_v3_translate_text.py +++ b/samples/snippets/translate_v3_translate_text.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_translate_text(project_id="[Google Cloud Project ID]"): +def sample_translate_text(project_id="YOUR_PROJECT_ID"): """Translating Text.""" client = translate.TranslationServiceClient() diff --git a/samples/snippets/translate_v3_translate_text_with_glossary.py b/samples/snippets/translate_v3_translate_text_with_glossary.py new file mode 100644 index 00000000..cc4da005 --- /dev/null +++ b/samples/snippets/translate_v3_translate_text_with_glossary.py @@ -0,0 +1,51 @@ +# 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. + +# [START translate_v3_translate_text_with_glossary] + +from google.cloud import translate_v3 + + +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_v3.TranslationServiceClient() + + contents = [text] + parent = client.location_path(project_id, "us-central1") + + glossary = client.glossary_path( + project_id, "us-central1", glossary_id # The location of the glossary + ) + + glossary_config = translate_v3.types.TranslateTextGlossaryConfig(glossary=glossary) + + # Supported language codes: https://cloud.google.com/translate/docs/languages + response = client.translate_text( + contents, + 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(u"\t {}".format(translation.translated_text)) + + +# [END translate_v3_translate_text_with_glossary] diff --git a/samples/snippets/translate_v3_translate_text_with_glossary_test.py b/samples/snippets/translate_v3_translate_text_with_glossary_test.py new file mode 100644 index 00000000..72f9d64e --- /dev/null +++ b/samples/snippets/translate_v3_translate_text_with_glossary_test.py @@ -0,0 +1,48 @@ +# -*- encoding: utf-8 -*- +# 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. + +import os +import pytest +import translate_v3_create_glossary +import translate_v3_delete_glossary +import translate_v3_translate_text_with_glossary +import uuid + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" + + +@pytest.fixture(scope="session") +def glossary(): + """Get the ID of a glossary available to session (do not mutate/delete).""" + glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) + translate_v3_create_glossary.create_glossary( + PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id + ) + + yield glossary_id + + try: + translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) + except Exception: + pass + + +def test_translate_text_with_glossary(capsys, glossary): + translate_v3_translate_text_with_glossary.translate_text_with_glossary( + "account", PROJECT_ID, glossary + ) + out, _ = capsys.readouterr() + assert "アカウント" or "口座" in out