google-cloud-translate 1.4.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/AUTHENTICATION.md +6 -2
- data/CHANGELOG.md +33 -0
- data/CONTRIBUTING.md +1 -1
- data/LICENSE +1 -1
- data/OVERVIEW.md +223 -18
- data/lib/google-cloud-translate.rb +79 -91
- data/lib/google/cloud/translate.rb +127 -114
- data/lib/google/cloud/translate/v2.rb +169 -0
- data/lib/google/cloud/translate/v2/api.rb +255 -0
- data/lib/google/cloud/translate/v2/credentials.rb +58 -0
- data/lib/google/cloud/translate/v2/detection.rb +132 -0
- data/lib/google/cloud/translate/v2/language.rb +68 -0
- data/lib/google/cloud/translate/v2/service.rb +205 -0
- data/lib/google/cloud/translate/v2/translation.rb +120 -0
- data/lib/google/cloud/translate/v3.rb +144 -0
- data/lib/google/cloud/translate/v3/credentials.rb +42 -0
- data/lib/google/cloud/translate/v3/doc/google/cloud/translate/v3/translation_service.rb +663 -0
- data/lib/google/cloud/translate/v3/doc/google/longrunning/operations.rb +51 -0
- data/lib/google/cloud/translate/v3/doc/google/protobuf/any.rb +131 -0
- data/lib/google/cloud/translate/v3/doc/google/protobuf/timestamp.rb +113 -0
- data/lib/google/cloud/translate/v3/doc/google/rpc/status.rb +87 -0
- data/lib/google/cloud/translate/v3/translation_service_client.rb +927 -0
- data/lib/google/cloud/translate/v3/translation_service_client_config.json +66 -0
- data/lib/google/cloud/translate/v3/translation_service_pb.rb +226 -0
- data/lib/google/cloud/translate/v3/translation_service_services_pb.rb +68 -0
- data/lib/google/cloud/translate/version.rb +1 -1
- metadata +47 -37
- data/lib/google/cloud/translate/api.rb +0 -274
- data/lib/google/cloud/translate/credentials.rb +0 -57
- data/lib/google/cloud/translate/detection.rb +0 -139
- data/lib/google/cloud/translate/language.rb +0 -70
- data/lib/google/cloud/translate/service.rb +0 -206
- data/lib/google/cloud/translate/translation.rb +0 -125
@@ -1,274 +0,0 @@
|
|
1
|
-
# Copyright 2016 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
|
-
# https://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
|
-
|
16
|
-
require "google/cloud/translate/service"
|
17
|
-
require "google/cloud/translate/translation"
|
18
|
-
require "google/cloud/translate/detection"
|
19
|
-
require "google/cloud/translate/language"
|
20
|
-
|
21
|
-
module Google
|
22
|
-
module Cloud
|
23
|
-
module Translate
|
24
|
-
##
|
25
|
-
# # Api
|
26
|
-
#
|
27
|
-
# Represents top-level access to the Google Cloud Translation API.
|
28
|
-
# Translation API supports more than one hundred different languages, from
|
29
|
-
# Afrikaans to Zulu. Used in combination, this enables translation between
|
30
|
-
# thousands of language pairs. Also, you can send in HTML and receive HTML
|
31
|
-
# with translated text back. You don't need to extract your source text or
|
32
|
-
# reassemble the translated content.
|
33
|
-
#
|
34
|
-
# @see https://cloud.google.com/translation/docs/getting-started
|
35
|
-
# Cloud Translation API Quickstart
|
36
|
-
#
|
37
|
-
# @example
|
38
|
-
# require "google/cloud/translate"
|
39
|
-
#
|
40
|
-
# translate = Google::Cloud::Translate.new
|
41
|
-
#
|
42
|
-
# translation = translate.translate "Hello world!", to: "la"
|
43
|
-
#
|
44
|
-
# translation.to_s #=> "Salve mundi!"
|
45
|
-
#
|
46
|
-
# translation.from #=> "en"
|
47
|
-
# translation.origin #=> "Hello world!"
|
48
|
-
# translation.to #=> "la"
|
49
|
-
# translation.text #=> "Salve mundi!"
|
50
|
-
#
|
51
|
-
class Api
|
52
|
-
##
|
53
|
-
# @private The Service object.
|
54
|
-
attr_accessor :service
|
55
|
-
|
56
|
-
##
|
57
|
-
# @private Creates a new Api instance.
|
58
|
-
#
|
59
|
-
# See {Google::Cloud.translate}
|
60
|
-
def initialize service
|
61
|
-
@service = service
|
62
|
-
end
|
63
|
-
|
64
|
-
##
|
65
|
-
# The Cloud Translation API project connected to.
|
66
|
-
#
|
67
|
-
# @example
|
68
|
-
# require "google/cloud/translate"
|
69
|
-
#
|
70
|
-
# translate = Google::Cloud::Translate.new(
|
71
|
-
# project_id: "my-todo-project",
|
72
|
-
# credentials: "/path/to/keyfile.json"
|
73
|
-
# )
|
74
|
-
#
|
75
|
-
# translate.project_id #=> "my-todo-project"
|
76
|
-
#
|
77
|
-
def project_id
|
78
|
-
service.project
|
79
|
-
end
|
80
|
-
alias project project_id
|
81
|
-
|
82
|
-
##
|
83
|
-
# Returns text translations from one language to another.
|
84
|
-
#
|
85
|
-
# @see https://cloud.google.com/translation/docs/translating-text#Translate
|
86
|
-
# Translating Text
|
87
|
-
#
|
88
|
-
# @param [String] text The text or texts to translate.
|
89
|
-
# @param [String] to The target language into which the text should be
|
90
|
-
# translated. This is required. The value must be an [ISO
|
91
|
-
# 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
|
92
|
-
# language code.
|
93
|
-
# @param [String] from The source language of the text or texts. This is
|
94
|
-
# an [ISO
|
95
|
-
# 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
|
96
|
-
# language code. This is optional.
|
97
|
-
# @param [String] format The format of the text. Possible values include
|
98
|
-
# `:text` and `:html`. This is optional. The Translation API default
|
99
|
-
# is `:html`.
|
100
|
-
# @param [String] model The model used by the service to perform the
|
101
|
-
# translation. Can be either `base` to use the Phrase-Based Machine
|
102
|
-
# Translation (PBMT) model, or `nmt` to use the Neural Machine
|
103
|
-
# Translation (NMT) model. The default is `nmt`.
|
104
|
-
#
|
105
|
-
# If the model is `nmt`, and the requested language translation pair
|
106
|
-
# is not supported for the NMT model, then the request is translated
|
107
|
-
# using the PBMT model.
|
108
|
-
#
|
109
|
-
# @param [String] cid The customization id for translate. This is
|
110
|
-
# optional.
|
111
|
-
#
|
112
|
-
# @return [Translation, Array<Translation>] A single {Translation}
|
113
|
-
# object if just one text was given, or an array of {Translation}
|
114
|
-
# objects if multiple texts were given.
|
115
|
-
#
|
116
|
-
# @example
|
117
|
-
# require "google/cloud/translate"
|
118
|
-
#
|
119
|
-
# translate = Google::Cloud::Translate.new
|
120
|
-
#
|
121
|
-
# translation = translate.translate "Hello world!", to: "la"
|
122
|
-
#
|
123
|
-
# translation.to_s #=> "Salve mundi!"
|
124
|
-
#
|
125
|
-
# translation.detected? #=> true
|
126
|
-
# translation.from #=> "en"
|
127
|
-
# translation.origin #=> "Hello world!"
|
128
|
-
# translation.to #=> "la"
|
129
|
-
# translation.text #=> "Salve mundi!"
|
130
|
-
# translation.model #=> "base"
|
131
|
-
#
|
132
|
-
# @example Using the neural machine translation model:
|
133
|
-
# require "google/cloud/translate"
|
134
|
-
#
|
135
|
-
# translate = Google::Cloud::Translate.new
|
136
|
-
#
|
137
|
-
# translation = translate.translate "Hello world!",
|
138
|
-
# to: "la", model: "nmt"
|
139
|
-
#
|
140
|
-
# translation.to_s #=> "Salve mundi!"
|
141
|
-
# translation.model #=> "nmt"
|
142
|
-
#
|
143
|
-
# @example Setting the `from` language.
|
144
|
-
# require "google/cloud/translate"
|
145
|
-
#
|
146
|
-
# translate = Google::Cloud::Translate.new
|
147
|
-
#
|
148
|
-
# translation = translate.translate "Hello world!",
|
149
|
-
# from: :en, to: :la
|
150
|
-
# translation.detected? #=> false
|
151
|
-
# translation.text #=> "Salve mundi!"
|
152
|
-
#
|
153
|
-
# @example Retrieving multiple translations.
|
154
|
-
# require "google/cloud/translate"
|
155
|
-
#
|
156
|
-
# translate = Google::Cloud::Translate.new
|
157
|
-
#
|
158
|
-
# translations = translate.translate "Hello my friend.",
|
159
|
-
# "See you soon.",
|
160
|
-
# from: "en", to: "la"
|
161
|
-
# translations.count #=> 2
|
162
|
-
# translations[0].text #=> "Salve amice."
|
163
|
-
# translations[1].text #=> "Vide te mox."
|
164
|
-
#
|
165
|
-
# @example Preserving HTML tags.
|
166
|
-
# require "google/cloud/translate"
|
167
|
-
#
|
168
|
-
# translate = Google::Cloud::Translate.new
|
169
|
-
#
|
170
|
-
# translation = translate.translate "<strong>Hello</strong> world!",
|
171
|
-
# to: :la
|
172
|
-
# translation.text #=> "<strong>Salve</strong> mundi!"
|
173
|
-
#
|
174
|
-
def translate *text, to: nil, from: nil, format: nil, model: nil,
|
175
|
-
cid: nil
|
176
|
-
return nil if text.empty?
|
177
|
-
raise ArgumentError, "to is required" if to.nil?
|
178
|
-
to = to.to_s
|
179
|
-
from = from.to_s if from
|
180
|
-
format = format.to_s if format
|
181
|
-
text = Array(text).flatten
|
182
|
-
gapi = service.translate text, to: to, from: from,
|
183
|
-
format: format, model: model, cid: cid
|
184
|
-
Translation.from_gapi_list gapi, text, to, from
|
185
|
-
end
|
186
|
-
|
187
|
-
##
|
188
|
-
# Detect the most likely language or languages of a text or multiple
|
189
|
-
# texts.
|
190
|
-
#
|
191
|
-
# @see https://cloud.google.com/translation/docs/detecting-language
|
192
|
-
# Detecting Language
|
193
|
-
#
|
194
|
-
# @param [String] text The text or texts upon which language detection
|
195
|
-
# should be performed.
|
196
|
-
#
|
197
|
-
# @return [Detection, Array<Detection>] A single {Detection} object if
|
198
|
-
# just one text was given, or an array of {Detection} objects if
|
199
|
-
# multiple texts were given.
|
200
|
-
#
|
201
|
-
# @example
|
202
|
-
# require "google/cloud/translate"
|
203
|
-
#
|
204
|
-
# translate = Google::Cloud::Translate.new
|
205
|
-
#
|
206
|
-
# detection = translate.detect "Hello world!"
|
207
|
-
# detection.language #=> "en"
|
208
|
-
# detection.confidence #=> 0.7100697
|
209
|
-
#
|
210
|
-
# @example Detecting multiple texts.
|
211
|
-
# require "google/cloud/translate"
|
212
|
-
#
|
213
|
-
# translate = Google::Cloud::Translate.new
|
214
|
-
#
|
215
|
-
# detections = translate.detect "Hello world!",
|
216
|
-
# "Bonjour le monde!"
|
217
|
-
# detections.count #=> 2
|
218
|
-
# detections.first.language #=> "en"
|
219
|
-
# detections.first.confidence #=> 0.7100697
|
220
|
-
# detections.last.language #=> "fr"
|
221
|
-
# detections.last.confidence #=> 0.40440267
|
222
|
-
#
|
223
|
-
def detect *text
|
224
|
-
return nil if text.empty?
|
225
|
-
text = Array(text).flatten
|
226
|
-
gapi = service.detect text
|
227
|
-
Detection.from_gapi gapi, text
|
228
|
-
end
|
229
|
-
|
230
|
-
##
|
231
|
-
# List the languages supported by the API. These are the languages to
|
232
|
-
# and from which text can be translated.
|
233
|
-
#
|
234
|
-
# @see https://cloud.google.com/translation/docs/discovering-supported-languages
|
235
|
-
# Discovering Supported Languages
|
236
|
-
#
|
237
|
-
# @param [String] language The language and collation in which the names
|
238
|
-
# of the languages are returned. If this is `nil` then no names are
|
239
|
-
# returned.
|
240
|
-
#
|
241
|
-
# @return [Array<Language>] An array of {Language} objects supported by
|
242
|
-
# the API.
|
243
|
-
#
|
244
|
-
# @example
|
245
|
-
# require "google/cloud/translate"
|
246
|
-
#
|
247
|
-
# translate = Google::Cloud::Translate.new
|
248
|
-
#
|
249
|
-
# languages = translate.languages
|
250
|
-
# languages.count #=> 104
|
251
|
-
#
|
252
|
-
# english = languages.detect { |l| l.code == "en" }
|
253
|
-
# english.name #=> nil
|
254
|
-
#
|
255
|
-
# @example Get all languages with their names in French.
|
256
|
-
# require "google/cloud/translate"
|
257
|
-
#
|
258
|
-
# translate = Google::Cloud::Translate.new
|
259
|
-
#
|
260
|
-
# languages = translate.languages "fr"
|
261
|
-
# languages.count #=> 104
|
262
|
-
#
|
263
|
-
# english = languages.detect { |l| l.code == "en" }
|
264
|
-
# english.name #=> "Anglais"
|
265
|
-
#
|
266
|
-
def languages language = nil
|
267
|
-
language = language.to_s if language
|
268
|
-
gapi = service.languages language
|
269
|
-
Array(gapi["languages"]).map { |g| Language.from_gapi g }
|
270
|
-
end
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# Copyright 2016 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
|
-
# https://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
|
-
|
16
|
-
require "googleauth"
|
17
|
-
|
18
|
-
module Google
|
19
|
-
module Cloud
|
20
|
-
module Translate
|
21
|
-
##
|
22
|
-
# # Credentials
|
23
|
-
#
|
24
|
-
# Represents the authentication and authorization used to connect to the
|
25
|
-
# Cloud Translation API.
|
26
|
-
#
|
27
|
-
# @example
|
28
|
-
# require "google/cloud/translate"
|
29
|
-
#
|
30
|
-
# keyfile = "/path/to/keyfile.json"
|
31
|
-
# creds = Google::Cloud::Translate::Credentials.new keyfile
|
32
|
-
#
|
33
|
-
# translate = Google::Cloud::Translate.new(
|
34
|
-
# project_id: "my-todo-project",
|
35
|
-
# credentials: creds
|
36
|
-
# )
|
37
|
-
#
|
38
|
-
# translate.project_id #=> "my-todo-project"
|
39
|
-
#
|
40
|
-
class Credentials < Google::Auth::Credentials
|
41
|
-
SCOPE = ["https://www.googleapis.com/auth/cloud-platform"].freeze
|
42
|
-
PATH_ENV_VARS = ["TRANSLATE_CREDENTIALS",
|
43
|
-
"TRANSLATE_KEYFILE",
|
44
|
-
"GOOGLE_CLOUD_CREDENTIALS",
|
45
|
-
"GOOGLE_CLOUD_KEYFILE",
|
46
|
-
"GCLOUD_KEYFILE"].freeze
|
47
|
-
JSON_ENV_VARS = ["TRANSLATE_CREDENTIALS_JSON",
|
48
|
-
"TRANSLATE_KEYFILE_JSON",
|
49
|
-
"GOOGLE_CLOUD_CREDENTIALS_JSON",
|
50
|
-
"GOOGLE_CLOUD_KEYFILE_JSON",
|
51
|
-
"GCLOUD_KEYFILE_JSON"].freeze
|
52
|
-
DEFAULT_PATHS = \
|
53
|
-
["~/.config/gcloud/application_default_credentials.json"].freeze
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,139 +0,0 @@
|
|
1
|
-
# Copyright 2016 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
|
-
# https://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
|
-
|
16
|
-
module Google
|
17
|
-
module Cloud
|
18
|
-
module Translate
|
19
|
-
##
|
20
|
-
# # Detection
|
21
|
-
#
|
22
|
-
# Represents a detect language query result. Returned by
|
23
|
-
# {Google::Cloud::Translate::Api#detect}.
|
24
|
-
#
|
25
|
-
# @see https://cloud.google.com/translation/docs/detecting-language
|
26
|
-
# Detecting Language
|
27
|
-
#
|
28
|
-
# @example
|
29
|
-
# require "google/cloud/translate"
|
30
|
-
#
|
31
|
-
# translate = Google::Cloud::Translate.new
|
32
|
-
#
|
33
|
-
# detections = translate.detect "chien", "chat"
|
34
|
-
#
|
35
|
-
# detections.size #=> 2
|
36
|
-
# detections[0].text #=> "chien"
|
37
|
-
# detections[0].language #=> "fr"
|
38
|
-
# detections[0].confidence #=> 0.7109375
|
39
|
-
# detections[1].text #=> "chat"
|
40
|
-
# detections[1].language #=> "en"
|
41
|
-
# detections[1].confidence #=> 0.59922177
|
42
|
-
#
|
43
|
-
class Detection
|
44
|
-
##
|
45
|
-
# The text upon which the language detection was performed.
|
46
|
-
#
|
47
|
-
# @return [String]
|
48
|
-
attr_reader :text
|
49
|
-
|
50
|
-
##
|
51
|
-
# The list of detection results for the given text. The most likely
|
52
|
-
# language is listed first, and its attributes can be accessed through
|
53
|
-
# {#language} and {#confidence}.
|
54
|
-
#
|
55
|
-
# @return [Array<Detection::Result>]
|
56
|
-
attr_reader :results
|
57
|
-
|
58
|
-
##
|
59
|
-
# @private Create a new object.
|
60
|
-
def initialize text, results
|
61
|
-
@text = text
|
62
|
-
@results = results
|
63
|
-
end
|
64
|
-
|
65
|
-
##
|
66
|
-
# The confidence that the language detection result is correct. The
|
67
|
-
# closer this value is to 1, the higher the confidence in language
|
68
|
-
# detection.
|
69
|
-
#
|
70
|
-
# @return [Float] a value between 0 and 1
|
71
|
-
def confidence
|
72
|
-
return nil if results.empty?
|
73
|
-
results.first.confidence
|
74
|
-
end
|
75
|
-
|
76
|
-
##
|
77
|
-
# The most likely language that was detected. This is an [ISO
|
78
|
-
# 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language
|
79
|
-
# code.
|
80
|
-
#
|
81
|
-
# @return [String] the language code
|
82
|
-
def language
|
83
|
-
return nil if results.empty?
|
84
|
-
results.first.language
|
85
|
-
end
|
86
|
-
|
87
|
-
##
|
88
|
-
# @private New Detection from a ListDetectionsResponse object as
|
89
|
-
# defined by the Google API Client object.
|
90
|
-
def self.from_gapi gapi, text
|
91
|
-
res = text.zip(Array(gapi["detections"])).map do |txt, detections|
|
92
|
-
results = detections.map { |g| Result.from_gapi g }
|
93
|
-
new txt, results
|
94
|
-
end
|
95
|
-
return res.first if res.size == 1
|
96
|
-
res
|
97
|
-
end
|
98
|
-
|
99
|
-
##
|
100
|
-
# # Result
|
101
|
-
#
|
102
|
-
# Represents an individual result in a
|
103
|
-
# {Google::Cloud::Translate::Detection} result.
|
104
|
-
#
|
105
|
-
class Result
|
106
|
-
##
|
107
|
-
# The confidence that the language detection result is correct. The
|
108
|
-
# closer this value is to 1, the higher the confidence in language
|
109
|
-
# detection.
|
110
|
-
#
|
111
|
-
# @return [Float] a value between 0 and 1
|
112
|
-
attr_reader :confidence
|
113
|
-
|
114
|
-
##
|
115
|
-
# The language detected. This is an [ISO
|
116
|
-
# 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
|
117
|
-
# language code.
|
118
|
-
#
|
119
|
-
# @return [String] the language code
|
120
|
-
attr_reader :language
|
121
|
-
|
122
|
-
##
|
123
|
-
# @private Create a new object.
|
124
|
-
def initialize confidence, language
|
125
|
-
@confidence = confidence
|
126
|
-
@language = language
|
127
|
-
end
|
128
|
-
|
129
|
-
##
|
130
|
-
# @private New Detection::Result from a DetectionsResource object as
|
131
|
-
# defined by the Google API Client object.
|
132
|
-
def self.from_gapi gapi
|
133
|
-
new gapi["confidence"], gapi["language"]
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|