Skip to content
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

translate v3 snippets #2745

Merged
merged 21 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
218016d
translate text v3
munkhuushmgl Jan 22, 2020
90018cc
added translate text with glossary snippets
munkhuushmgl Jan 22, 2020
2f9da38
Merge branch 'master' into translate-v3-snippets
munkhuushmgl Jan 29, 2020
52688ae
Merge branch 'master' into translate-v3-snippets
texasmichelle Jan 30, 2020
a9c8c72
Merge branch 'master' into translate-v3-snippets
munkhuushmgl Jan 31, 2020
a68e006
finished glossary tests
munkhuushmgl Feb 1, 2020
9f4f4b2
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl Feb 1, 2020
98cf0ad
Merge branch 'master' into translate-v3-snippets
munkhuushmgl Feb 1, 2020
3ecc0f1
removed overlapping files
munkhuushmgl Feb 1, 2020
8f87d7c
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl Feb 1, 2020
82df904
added encoding tag
munkhuushmgl Feb 3, 2020
fc9fc18
Merge branch 'master' into translate-v3-snippets
munkhuushmgl Feb 3, 2020
279b950
Merge branch 'master' into translate-v3-snippets
munkhuushmgl Feb 3, 2020
e6034f9
added more descriptive docs and broke down tests
munkhuushmgl Feb 4, 2020
73abda2
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl Feb 4, 2020
f65a384
Update translate/cloud-client/translate_v3_create_glossary.py
munkhuushmgl Feb 4, 2020
db213be
Update translate/cloud-client/translate_v3_create_glossary.py
munkhuushmgl Feb 4, 2020
9fdfd41
Merge branch 'master' into translate-v3-snippets
munkhuushmgl Feb 4, 2020
a1afab4
fixed test for translate with glossary
munkhuushmgl Feb 4, 2020
6c3a75f
Merge branch 'translate-v3-snippets' of https://github.com/munkhuushm…
munkhuushmgl Feb 4, 2020
df8e008
fixed lint
munkhuushmgl Feb 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions translate/cloud-client/translate_create_glossary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2020 Google LLC
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
#
# 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_create_glossary]
from google.cloud import translate_v3


def create_glossary(
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
project_id, source_lang_code, target_lang_code, input_uri, glossary_id
):
"""Create Glossary"""
client = translate_v3.TranslationServiceClient()

# project_id = 'YOUR_PROJECT_ID'
# glossary_id = 'glossary-id'
location = "us-central1" # The location of the glossary

name = client.glossary_path(project_id, location, glossary_id)
language_codes_set = translate_v3.types.Glossary.LanguageCodesSet(
language_codes=[source_lang_code, target_lang_code]
)

gcs_source = translate_v3.types.GcsSource(input_uri=input_uri)

input_config = translate_v3.types.GlossaryInputConfig(gcs_source=gcs_source)

glossary = translate_v3.types.Glossary(
name=name, language_codes_set=language_codes_set, input_config=input_config
)

parent = client.location_path(project_id, location)

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_create_glossary]
33 changes: 33 additions & 0 deletions translate/cloud-client/translate_delete_glossary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2020 Google LLC
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
#
# 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_delete_glossary]
from google.cloud import translate_v3


def delete_glossary(project_id, glossary_id):
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
"""Delete Glossary"""
client = translate_v3.TranslationServiceClient()

# project_id = 'YOUR_PROJECT_ID'
# glossary_id = 'GLOSSARY_ID'

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_delete_glossary]
39 changes: 39 additions & 0 deletions translate/cloud-client/translate_v3_translate_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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.


def translate_text(text, target_language, project_id):
# [START translate_v3_translate_text]
from google.cloud import translate_v3

client = translate_v3.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# text = 'Text you wish to translate'
# target_language = 'fr'
# project_id = '[Google Cloud Project ID]'
contents = [text]
parent = client.location_path(project_id, "global")

response = client.translate_text(
parent=parent,
contents=contents,
mime_type="text/plain", # mime types: text/plain, text/html
source_language_code="en-US",
target_language_code=target_language,
)
# Display the translation for each input text provided
for translation in response.translations:
print(u"Translated text: {}".format(translation.translated_text))
# [END translate_v3_translate_text]
26 changes: 26 additions & 0 deletions translate/cloud-client/translate_v3_translate_text_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
# 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_translate_text

PROJECT_ID = os.environ["GCLOUD_PROJECT"]


def test_translate_text_v3(capsys):
translate_v3_translate_text.translate_text(
"Hello world", "sr-Latn", PROJECT_ID)
out, _ = capsys.readouterr()
assert "Zdravo svijete" in out or "Pozdrav svijetu" in out
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_translate_text_with_glossary]

from google.cloud import translate_v3


def translate_text_with_glossary(
text, source_language, target_language, project_id, glossary_id
):
"""Translates a given text using a glossary."""

client = translate_v3.TranslationServiceClient()

# text = 'Hello, world!'
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
# 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]'
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,
)

for translation in response.glossary_translations:
print(u"Translated text: {}".format(translation.translated_text))
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved


# [END translate_translate_text_with_glossary]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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_create_glossary
import translate_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.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_create_glossary.create_glossary(
PROJECT_ID, "en", "es", GLOSSARY_INPUT_URI, glossary_id
)

yield glossary_id

try:
translate_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", "es", PROJECT_ID, glossary
)
out, _ = capsys.readouterr()
assert "cuenta" in out