-
Notifications
You must be signed in to change notification settings - Fork 6.5k
translate v3 snippets #2745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
czahedi
merged 21 commits into
GoogleCloudPlatform:master
from
munkhuushmgl:translate-v3-snippets
Feb 4, 2020
Merged
translate v3 snippets #2745
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
218016d
translate text v3
munkhuushmgl 90018cc
added translate text with glossary snippets
munkhuushmgl 2f9da38
Merge branch 'master' into translate-v3-snippets
munkhuushmgl 52688ae
Merge branch 'master' into translate-v3-snippets
texasmichelle a9c8c72
Merge branch 'master' into translate-v3-snippets
munkhuushmgl a68e006
finished glossary tests
munkhuushmgl 9f4f4b2
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl 98cf0ad
Merge branch 'master' into translate-v3-snippets
munkhuushmgl 3ecc0f1
removed overlapping files
munkhuushmgl 8f87d7c
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl 82df904
added encoding tag
munkhuushmgl fc9fc18
Merge branch 'master' into translate-v3-snippets
munkhuushmgl 279b950
Merge branch 'master' into translate-v3-snippets
munkhuushmgl e6034f9
added more descriptive docs and broke down tests
munkhuushmgl 73abda2
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl f65a384
Update translate/cloud-client/translate_v3_create_glossary.py
munkhuushmgl db213be
Update translate/cloud-client/translate_v3_create_glossary.py
munkhuushmgl 9fdfd41
Merge branch 'master' into translate-v3-snippets
munkhuushmgl a1afab4
fixed test for translate with glossary
munkhuushmgl 6c3a75f
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl df8e008
fixed lint
munkhuushmgl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
translate/cloud-client/translate_v3_create_get_delete_glossary_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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]", | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
input_uri="[YOUR_INPUT_URI]", | ||
glossary_id="[YOUR_GLOSSARY_ID]", | ||
): | ||
"""Create Glossary""" | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client = translate.TranslationServiceClient() | ||
source_lang_code = "en" | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="[GOOGLE_CLOUD_PROJECT_ID]", glossary_id="[YOUR_GLOSSARY_ID]" | ||
): | ||
"""Delete Glossary""" | ||
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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="[Google Cloud Project ID]", glossary_id="[Glossary ID]"): | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Get Glossary""" | ||
|
||
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] |
52 changes: 52 additions & 0 deletions
52
translate/cloud-client/translate_v3_translate_text_with_glossary.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 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="Hello, world!", | ||
source_language="en", | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
target_language="fr", | ||
project_id="[GOOGLE_CLOUD_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) | ||
munkhuushmgl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
response = client.translate_text( | ||
contents, | ||
target_language_code=target_language, | ||
source_language_code=source_language, | ||
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] |
48 changes: 48 additions & 0 deletions
48
translate/cloud-client/translate_v3_translate_text_with_glossary_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", "en", "ja", PROJECT_ID, glossary | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "アカウント" or "口座" in out |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.