Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit fe48980

Browse files
munkhuushmgltexasmichellekurtisvg
authored andcommitted
translate v3 snippets [(#2745)](GoogleCloudPlatform/python-docs-samples#2745)
* translate text v3 * added translate text with glossary snippets * finished glossary tests * removed overlapping files * added encoding tag * added more descriptive docs and broke down tests * Update translate/cloud-client/translate_v3_create_glossary.py Co-Authored-By: Kurtis Van Gent <[email protected]> * Update translate/cloud-client/translate_v3_create_glossary.py Co-Authored-By: Kurtis Van Gent <[email protected]> * fixed test for translate with glossary * fixed lint Co-authored-by: Michelle Casbon <[email protected]> Co-authored-by: Kurtis Van Gent <[email protected]>
1 parent 81a06be commit fe48980

9 files changed

+340
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START translate_v3_create_glossary]
16+
from google.cloud import translate_v3 as translate
17+
18+
19+
def create_glossary(
20+
project_id="YOUR_PROJECT_ID",
21+
input_uri="YOUR_INPUT_URI",
22+
glossary_id="YOUR_GLOSSARY_ID",
23+
):
24+
"""
25+
Create a equivalent term sets glossary. Glossary can be words or
26+
short phrases (usually fewer than five words).
27+
https://cloud.google.com/translate/docs/advanced/glossary#format-glossary
28+
"""
29+
client = translate.TranslationServiceClient()
30+
31+
# Supported language codes: https://cloud.google.com/translate/docs/languages
32+
source_lang_code = "en"
33+
target_lang_code = "ja"
34+
location = "us-central1" # The location of the glossary
35+
36+
name = client.glossary_path(project_id, location, glossary_id)
37+
language_codes_set = translate.types.Glossary.LanguageCodesSet(
38+
language_codes=[source_lang_code, target_lang_code]
39+
)
40+
41+
gcs_source = translate.types.GcsSource(input_uri=input_uri)
42+
43+
input_config = translate.types.GlossaryInputConfig(gcs_source=gcs_source)
44+
45+
glossary = translate.types.Glossary(
46+
name=name, language_codes_set=language_codes_set, input_config=input_config
47+
)
48+
49+
parent = client.location_path(project_id, location)
50+
# glossary is a custom dictionary Translation API uses
51+
# to translate the domain-specific terminology.
52+
operation = client.create_glossary(parent=parent, glossary=glossary)
53+
54+
result = operation.result(timeout=90)
55+
print("Created: {}".format(result.name))
56+
print("Input Uri: {}".format(result.input_config.gcs_source.input_uri))
57+
58+
59+
# [END translate_v3_create_glossary]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import translate_v3_create_glossary
17+
import translate_v3_delete_glossary
18+
import uuid
19+
20+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
21+
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
22+
23+
24+
def test_create_glossary(capsys):
25+
glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
26+
translate_v3_create_glossary.create_glossary(
27+
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
28+
)
29+
out, _ = capsys.readouterr()
30+
# assert
31+
assert "Created:" in out
32+
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out
33+
34+
# clean up after use
35+
try:
36+
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
37+
except Exception:
38+
pass
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START translate_v3_delete_glossary]
16+
from google.cloud import translate_v3 as translate
17+
18+
19+
def delete_glossary(
20+
project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID"
21+
):
22+
"""Delete a specific glossary based on the glossary ID."""
23+
client = translate.TranslationServiceClient()
24+
25+
parent = client.glossary_path(project_id, "us-central1", glossary_id)
26+
27+
operation = client.delete_glossary(parent)
28+
result = operation.result(timeout=90)
29+
print("Deleted: {}".format(result.name))
30+
31+
32+
# [END translate_v3_delete_glossary]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import translate_v3_create_glossary
17+
import translate_v3_delete_glossary
18+
import uuid
19+
20+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
21+
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
22+
23+
24+
def test_delete_glossary(capsys):
25+
# setup
26+
glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
27+
translate_v3_create_glossary.create_glossary(
28+
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
29+
)
30+
31+
# assert
32+
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
33+
out, _ = capsys.readouterr()
34+
assert "Deleted:" in out
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START translate_v3_get_glossary]
16+
from google.cloud import translate_v3 as translate
17+
18+
19+
def get_glossary(project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID"):
20+
"""Get a particular glossary based on the glossary ID."""
21+
22+
client = translate.TranslationServiceClient()
23+
24+
name = client.glossary_path(project_id, "us-central1", glossary_id)
25+
26+
response = client.get_glossary(name)
27+
print(u"Glossary name: {}".format(response.name))
28+
print(u"Entry count: {}".format(response.entry_count))
29+
print(u"Input URI: {}".format(response.input_config.gcs_source.input_uri))
30+
31+
32+
# [END translate_v3_get_glossary]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import pytest
17+
import translate_v3_create_glossary
18+
import translate_v3_delete_glossary
19+
import translate_v3_get_glossary
20+
import uuid
21+
22+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
23+
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
24+
25+
26+
@pytest.fixture(scope="session")
27+
def glossary():
28+
"""Get the ID of a glossary available to session (do not mutate/delete)."""
29+
glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
30+
translate_v3_create_glossary.create_glossary(
31+
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
32+
)
33+
34+
yield glossary_id
35+
36+
try:
37+
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
38+
except Exception:
39+
pass
40+
41+
42+
def test_get_glossary(capsys, glossary):
43+
translate_v3_get_glossary.get_glossary(PROJECT_ID, glossary)
44+
out, _ = capsys.readouterr()
45+
assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out

samples/snippets/translate_v3_translate_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.cloud import translate
1717

1818

19-
def sample_translate_text(project_id="[Google Cloud Project ID]"):
19+
def sample_translate_text(project_id="YOUR_PROJECT_ID"):
2020
"""Translating Text."""
2121

2222
client = translate.TranslationServiceClient()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START translate_v3_translate_text_with_glossary]
16+
17+
from google.cloud import translate_v3
18+
19+
20+
def translate_text_with_glossary(
21+
text="YOUR_TEXT_TO_TRANSLATE",
22+
project_id="YOUR_PROJECT_ID",
23+
glossary_id="YOUR_GLOSSARY_ID"
24+
):
25+
"""Translates a given text using a glossary."""
26+
27+
client = translate_v3.TranslationServiceClient()
28+
29+
contents = [text]
30+
parent = client.location_path(project_id, "us-central1")
31+
32+
glossary = client.glossary_path(
33+
project_id, "us-central1", glossary_id # The location of the glossary
34+
)
35+
36+
glossary_config = translate_v3.types.TranslateTextGlossaryConfig(glossary=glossary)
37+
38+
# Supported language codes: https://cloud.google.com/translate/docs/languages
39+
response = client.translate_text(
40+
contents,
41+
target_language_code="ja",
42+
source_language_code="en",
43+
parent=parent,
44+
glossary_config=glossary_config,
45+
)
46+
print("Translated text: \n")
47+
for translation in response.glossary_translations:
48+
print(u"\t {}".format(translation.translated_text))
49+
50+
51+
# [END translate_v3_translate_text_with_glossary]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- encoding: utf-8 -*-
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+
import os
17+
import pytest
18+
import translate_v3_create_glossary
19+
import translate_v3_delete_glossary
20+
import translate_v3_translate_text_with_glossary
21+
import uuid
22+
23+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
24+
GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv"
25+
26+
27+
@pytest.fixture(scope="session")
28+
def glossary():
29+
"""Get the ID of a glossary available to session (do not mutate/delete)."""
30+
glossary_id = "must-start-with-letters-" + str(uuid.uuid1())
31+
translate_v3_create_glossary.create_glossary(
32+
PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id
33+
)
34+
35+
yield glossary_id
36+
37+
try:
38+
translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id)
39+
except Exception:
40+
pass
41+
42+
43+
def test_translate_text_with_glossary(capsys, glossary):
44+
translate_v3_translate_text_with_glossary.translate_text_with_glossary(
45+
"account", PROJECT_ID, glossary
46+
)
47+
out, _ = capsys.readouterr()
48+
assert "アカウント" or "口座" in out

0 commit comments

Comments
 (0)