-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
snippets.py
168 lines (119 loc) · 5.27 KB
/
snippets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# Copyright 2016 Google, Inc.
#
# 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.
"""This application demonstrates how to perform basic operations with the
Google Cloud Translate API
For more information, the documentation at
https://cloud.google.com/translate/docs.
"""
import argparse
# [START translate_detect_language]
def detect_language(text):
"""Detects the text's language."""
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.detect_language(text)
print("Text: {}".format(text))
print("Confidence: {}".format(result["confidence"]))
print("Language: {}".format(result["language"]))
# [END translate_detect_language]
# [START translate_list_codes]
def list_languages():
"""Lists all available languages."""
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
results = translate_client.get_languages()
for language in results:
print(u"{name} ({language})".format(**language))
# [END translate_list_codes]
# [START translate_list_language_names]
def list_languages_with_target(target):
"""Lists all available languages and localizes them to the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
results = translate_client.get_languages(target_language=target)
for language in results:
print(u"{name} ({language})".format(**language))
# [END translate_list_language_names]
# [START translate_text_with_model]
def translate_text_with_model(target, text, model="nmt"):
"""Translates text into the target language.
Make sure your project is allowlisted.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, bytes):
text = text.decode("utf-8")
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language=target, model=model)
print(u"Text: {}".format(result["input"]))
print(u"Translation: {}".format(result["translatedText"]))
print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))
# [END translate_text_with_model]
# [START translate_translate_text]
def translate_text(target, text):
"""Translates text into the target language.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
import six
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, six.binary_type):
text = text.decode("utf-8")
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language=target)
print(u"Text: {}".format(result["input"]))
print(u"Translation: {}".format(result["translatedText"]))
print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))
# [END translate_translate_text]
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
subparsers = parser.add_subparsers(dest="command")
detect_langage_parser = subparsers.add_parser(
"detect-language", help=detect_language.__doc__
)
detect_langage_parser.add_argument("text")
list_languages_parser = subparsers.add_parser(
"list-languages", help=list_languages.__doc__
)
list_languages_with_target_parser = subparsers.add_parser(
"list-languages-with-target", help=list_languages_with_target.__doc__
)
list_languages_with_target_parser.add_argument("target")
translate_text_parser = subparsers.add_parser(
"translate-text", help=translate_text.__doc__
)
translate_text_parser.add_argument("target")
translate_text_parser.add_argument("text")
args = parser.parse_args()
if args.command == "detect-language":
detect_language(args.text)
elif args.command == "list-languages":
list_languages()
elif args.command == "list-languages-with-target":
list_languages_with_target(args.target)
elif args.command == "translate-text":
translate_text(args.target, args.text)