google-cloud-translate 1.4.0 → 2.0.0

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