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: samples #3034

Merged
merged 21 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
dd3e34e
translate with custom model, get supported langs
munkhuushmgl Mar 5, 2020
85af59b
inlined small nit
munkhuushmgl Mar 5, 2020
1a22c4f
added encoding to model test
munkhuushmgl Mar 5, 2020
81bb4c8
added missing region tags and link to supported langs
munkhuushmgl Mar 5, 2020
f63a537
Merge branch 'master' into moving-translate-samples
munkhuushmgl Mar 5, 2020
1fd9261
Merge branch 'master' into moving-translate-samples
leahecole Mar 6, 2020
76fccaa
Merge branch 'master' into moving-translate-samples
munkhuushmgl Mar 7, 2020
da4401c
inlined text-to-translate
munkhuushmgl Mar 9, 2020
70b4c8c
Merge branch 'master' into moving-translate-samples
munkhuushmgl Mar 9, 2020
7663ac2
Merge branch 'moving-translate-samples' of https://github.com/munkhuu…
munkhuushmgl Mar 9, 2020
8f5a3d7
directly inlined contents
munkhuushmgl Mar 9, 2020
3e213e7
Merge branch 'master' into moving-translate-samples
munkhuushmgl Mar 9, 2020
534e469
Merge branch 'master' into moving-translate-samples
munkhuushmgl Mar 9, 2020
c624817
Merge branch 'master' into moving-translate-samples
munkhuushmgl Mar 10, 2020
8afb5ce
revert text-translate vars
munkhuushmgl Mar 11, 2020
bd3ef9d
Merge branch 'moving-translate-samples' of https://github.com/munkhuu…
munkhuushmgl Mar 11, 2020
8739c09
Merge branch 'master' into moving-translate-samples
leahecole Mar 12, 2020
ba63302
reversed inlined text params
munkhuushmgl Mar 12, 2020
01c6c35
Merge branch 'moving-translate-samples' of https://github.com/munkhuu…
munkhuushmgl Mar 12, 2020
8a317a9
small nit
munkhuushmgl Mar 12, 2020
6f88140
Merge branch 'master' into moving-translate-samples
munkhuushmgl Mar 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"):

parent = client.location_path(project_id, "global")

# Supported language codes: https://cloud.google.com/translate/docs/languages
response = client.get_supported_languages(parent=parent)

# List language codes of supported languages.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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_get_supported_languages_for_target]
from google.cloud import translate
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved


def get_supported_languages_with_target(project_id="YOUR_PROJECT_ID"):
"""Listing supported languages with target language name."""

client = translate.TranslationServiceClient()
parent = client.location_path(project_id, "global")

# Supported language codes: https://cloud.google.com/translate/docs/languages
response = client.get_supported_languages(
display_language_code="is", # target language code
parent=parent
)
# List language codes of supported languages
for language in response.languages:
print(u"Language Code: {}".format(language.language_code))
print(u"Display Name: {}".format(language.display_name))

# [END translate_v3_get_supported_languages_for_target]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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_get_supported_languages_with_target as get_supported_langs

PROJECT_ID = os.environ["GCLOUD_PROJECT"]


def test_list_languages_with_target(capsys):
get_supported_langs.get_supported_languages_with_target(
PROJECT_ID
)
out, _ = capsys.readouterr()
assert u"Language Code: sq" in out
assert u"Display Name: albanska" in out
4 changes: 2 additions & 2 deletions translate/cloud-client/translate_v3_translate_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from google.cloud import translate


def sample_translate_text(project_id="YOUR_PROJECT_ID"):
def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"):
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
"""Translating Text."""

client = translate.TranslationServiceClient()
Expand All @@ -27,7 +27,7 @@ def sample_translate_text(project_id="YOUR_PROJECT_ID"):
# https://cloud.google.com/translate/docs/supported-formats
response = client.translate_text(
parent=parent,
contents=["Hello, world!"],
contents=[text],
mime_type="text/plain", # mime types: text/plain, text/html
source_language_code="en-US",
target_language_code="fr",
Expand Down
3 changes: 2 additions & 1 deletion translate/cloud-client/translate_v3_translate_text_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


def test_translate_text(capsys):
translate_v3_translate_text.sample_translate_text(PROJECT_ID)
translate_v3_translate_text.sample_translate_text(
"Hello World!", PROJECT_ID)
out, _ = capsys.readouterr()
assert "Bonjour le monde" in out
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@
def translate_text_with_glossary(
text="YOUR_TEXT_TO_TRANSLATE",
project_id="YOUR_PROJECT_ID",
glossary_id="YOUR_GLOSSARY_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)
glossary_config = translate_v3.types.TranslateTextGlossaryConfig(
glossary=glossary)

# Supported language codes: https://cloud.google.com/translate/docs/languages
response = client.translate_text(
contents,
contents=[text],
target_language_code="ja",
source_language_code="en",
parent=parent,
Expand Down
48 changes: 48 additions & 0 deletions translate/cloud-client/translate_v3_translate_text_with_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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_model]

from google.cloud import translate


def translate_text_with_model(
text="YOUR_TEXT_TO_TRANSLATE",
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
project_id="YOUR_PROJECT_ID",
model_id="YOUR_MODEL_ID",
):
"""Translates a given text using Translation custom model."""

client = translate.TranslationServiceClient()

parent = client.location_path(project_id, "us-central1")
model_path = "projects/{}/locations/{}/models/{}".format(
project_id, "us-central1", model_id
)

# Supported language codes: https://cloud.google.com/translate/docs/languages
response = client.translate_text(
contents=[text],
target_language_code="ja",
model=model_path,
source_language_code="en",
parent=parent,
mime_type="text/plain", # mime types: text/plain, text/html
)
# 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_with_model]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- encoding: 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_with_model

PROJECT_ID = os.environ["GCLOUD_PROJECT"]
MODEL_ID = "TRL3128559826197068699"


def test_translate_text_with_model(capsys):
translate_v3_translate_text_with_model.translate_text_with_model(
"That' il do it.", PROJECT_ID, MODEL_ID
)
out, _ = capsys.readouterr()
assert "それはそうだ" or "それじゃあ" in out