Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 824d83b

Browse filesBrowse files
munkhuushmglnnegrey
authored andcommitted
Translation V3 GA samples (GoogleCloudPlatform#2478)
* Translation V3 GA samples * renamed v3 files * fixed tests * renamed region tags * renamed region tags * reformatted v2 samples * fixed small nits on batch_translate_text with model * made requested changes * renamed methods names in main * added or case tests * added lang code pair for list_glossary * changed GOOGLE_PROJECT--> GCLOUD_PROJETC * tweaked tests --> changed target lang from es to ja * removed v2 samples * added missing features batch_translate_with_glossary & model * hardcoded model_id * fixed wrong model_id * updated input_uri
1 parent 67bd83e commit 824d83b
Copy full SHA for 824d83b
Expand file treeCollapse file tree

31 files changed

+1931
-3
lines changed
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-translate==1.4.0
2-
google-cloud-storage==1.14.0
1+
google-cloud-translate==1.7.0
2+
google-cloud-storage==1.19.1

‎translate/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: translate/cloud-client/snippets_test.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ def test_translate_utf8(capsys):
4646
text = u'나는 파인애플을 좋아한다.'
4747
snippets.translate_text('en', text)
4848
out, _ = capsys.readouterr()
49-
assert u'I like pineapples.' in out
49+
assert u'I like pineapple' in out
+99Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "translate_v3_batch_translate_text")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-translate
21+
22+
# sample-metadata
23+
# title: Batch translate text
24+
# description: Batch translate text
25+
# usage: python3 translate_v3_batch_translate_text.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project_id "[Google Cloud Project ID]"] [--location "us-central1"] [--source_lang en] [--target_lang ja]
26+
27+
# [START translate_v3_batch_translate_text]
28+
from google.cloud import translate_v3
29+
30+
def sample_batch_translate_text(
31+
input_uri, output_uri, project_id, location, source_lang, target_lang
32+
):
33+
"""Batch translate text"""
34+
35+
client = translate_v3.TranslationServiceClient()
36+
37+
# TODO(developer): Uncomment and set the following variables
38+
# input_uri = 'gs://cloud-samples-data/text.txt'
39+
# output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'
40+
# project_id = '[Google Cloud Project ID]'
41+
# location = 'us-central1'
42+
# source_lang = 'en'
43+
# target_lang = 'ja'
44+
target_language_codes = [target_lang]
45+
gcs_source = {"input_uri": input_uri}
46+
47+
# Optional. Can be "text/plain" or "text/html".
48+
mime_type = "text/plain"
49+
input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type}
50+
input_configs = [input_configs_element]
51+
gcs_destination = {"output_uri_prefix": output_uri}
52+
output_config = {"gcs_destination": gcs_destination}
53+
parent = client.location_path(project_id, location)
54+
55+
operation = client.batch_translate_text(
56+
parent=parent,
57+
source_language_code=source_lang,
58+
target_language_codes=target_language_codes,
59+
input_configs=input_configs,
60+
output_config=output_config)
61+
62+
print(u"Waiting for operation to complete...")
63+
response = operation.result(90)
64+
65+
print(u"Total Characters: {}".format(response.total_characters))
66+
print(u"Translated Characters: {}".format(response.translated_characters))
67+
68+
69+
# [END translate_v3_batch_translate_text]
70+
71+
72+
def main():
73+
import argparse
74+
75+
parser = argparse.ArgumentParser()
76+
parser.add_argument(
77+
"--input_uri", type=str, default="gs://cloud-samples-data/text.txt"
78+
)
79+
parser.add_argument(
80+
"--output_uri", type=str, default="gs://YOUR_BUCKET_ID/path_to_store_results/"
81+
)
82+
parser.add_argument("--project_id", type=str, default="[Google Cloud Project ID]")
83+
parser.add_argument("--location", type=str, default="us-central1")
84+
parser.add_argument("--source_lang", type=str, default="en")
85+
parser.add_argument("--target_lang", type=str, default="ja")
86+
args = parser.parse_args()
87+
88+
sample_batch_translate_text(
89+
args.input_uri,
90+
args.output_uri,
91+
args.project_id,
92+
args.location,
93+
args.source_lang,
94+
args.target_lang,
95+
)
96+
97+
98+
if __name__ == "__main__":
99+
main()
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2019 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 uuid
18+
import translate_v3_batch_translate_text
19+
from google.cloud import storage
20+
21+
PROJECT_ID = os.environ['GCLOUD_PROJECT']
22+
23+
@pytest.fixture(scope='function')
24+
def bucket():
25+
"""Create a temporary bucket to store annotation output."""
26+
bucket_name = str(uuid.uuid1())
27+
storage_client = storage.Client()
28+
bucket = storage_client.create_bucket(bucket_name)
29+
30+
yield bucket
31+
32+
bucket.delete(force=True)
33+
34+
def test_batch_translate_text(capsys, bucket):
35+
translate_v3_batch_translate_text.sample_batch_translate_text(
36+
'gs://cloud-samples-data/translation/text.txt',
37+
'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name),
38+
PROJECT_ID,
39+
'us-central1',
40+
'en',
41+
'es'
42+
)
43+
out, _ = capsys.readouterr()
44+
assert 'Total Characters: 13' in out
+118Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "batch_translate_text_with_glossary")
18+
19+
# To install the latest published package dependency, execute the following:
20+
# pip install google-cloud-translate
21+
22+
# sample-metadata
23+
# title: Batch Translate Text with Glossary
24+
# description: Batch Translate Text with Glossary a given URI using a glossary.
25+
# usage: python3 translate_v3_batch_translate_text_with_glossary.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--glossary_id "[YOUR_GLOSSARY_ID]"] [--target_language en] [--source_language de]
26+
27+
# [START translate_v3_batch_translate_text_with_glossary]
28+
from google.cloud import translate_v3
29+
30+
def sample_batch_translate_text_with_glossary(
31+
input_uri, output_uri, project_id, location, source_lang, target_lang, glossary_id
32+
):
33+
"""Batch translate text with Glossary"""
34+
35+
client = translate_v3.TranslationServiceClient()
36+
37+
# TODO(developer): Uncomment and set the following variables
38+
# input_uri = 'gs://cloud-samples-data/text.txt'
39+
# output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'
40+
# project_id = '[Google Cloud Project ID]'
41+
# location = 'us-central1'
42+
# source_lang = 'en'
43+
# target_lang = 'ja'
44+
# glossary_id = '[YOUR_GLOSSARY_ID]'
45+
target_language_codes = [target_lang]
46+
gcs_source = {"input_uri": input_uri}
47+
48+
# Optional. Can be "text/plain" or "text/html".
49+
mime_type = "text/plain"
50+
input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type}
51+
input_configs = [input_configs_element]
52+
gcs_destination = {"output_uri_prefix": output_uri}
53+
output_config = {"gcs_destination": gcs_destination}
54+
input_uri, output_uri, project_id, location, source_lang, target_lang, glossary_id
55+
parent = client.location_path(project_id, location)
56+
57+
glossary_path = client.glossary_path(
58+
project_id,
59+
'us-central1', # The location of the glossary
60+
glossary_id)
61+
62+
glossary_config = translate_v3.types.TranslateTextGlossaryConfig(
63+
glossary=glossary_path)
64+
65+
glossaries = {"ja": glossary_config} #target lang as key
66+
67+
operation = client.batch_translate_text(
68+
parent=parent,
69+
source_language_code=source_lang,
70+
target_language_codes=target_language_codes,
71+
input_configs=input_configs,
72+
glossaries=glossaries,
73+
output_config=output_config)
74+
75+
print(u"Waiting for operation to complete...")
76+
response = operation.result(90)
77+
78+
print(u"Total Characters: {}".format(response.total_characters))
79+
print(u"Translated Characters: {}".format(response.translated_characters))
80+
81+
82+
# [END translate_v3_batch_translate_text_with_glossary]
83+
84+
85+
def main():
86+
import argparse
87+
88+
parser = argparse.ArgumentParser()
89+
parser.add_argument(
90+
"--input_uri", type=str, default="gs://cloud-samples-data/translation/text_with_glossary.txt"
91+
)
92+
parser.add_argument(
93+
"--output_uri", type=str, default="gs://YOUR_BUCKET_ID/path_to_store_results/"
94+
)
95+
parser.add_argument("--project_id", type=str, default="[Google Cloud Project ID]")
96+
parser.add_argument("--location", type=str, default="us-central1")
97+
parser.add_argument("--source_lang", type=str, default="en")
98+
parser.add_argument("--target_lang", type=str, default="ja")
99+
parser.add_argument(
100+
"--glossary_id",
101+
type=str,
102+
default="[YOUR_GLOSSARY_ID]",
103+
)
104+
args = parser.parse_args()
105+
106+
sample_batch_translate_text_with_glossary(
107+
args.input_uri,
108+
args.output_uri,
109+
args.project_id,
110+
args.location,
111+
args.source_lang,
112+
args.target_lang,
113+
args.glossary_id
114+
)
115+
116+
117+
if __name__ == "__main__":
118+
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.