google-cloud-translate 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e06b7f7e4e48d78576b68e7918f645f55b552cb
4
+ data.tar.gz: efabd14fecbf35fdd359f153313639d0e031941a
5
+ SHA512:
6
+ metadata.gz: e4a537004207777396d4a6a078b87d3bb4ab64c4f8e558e09a6a91172f343d2f0ed3a9c5789b8a4c74be833a2a51e5846b84f6817f3f1be39c21bace11c65a0b
7
+ data.tar.gz: 3e4c0ad24a9c8c5d5847a5efaa904d398ec3f12ea06ebaf8096595e093e03d87c72d87ec18d74d8f2aa44b3ae70dff179e628dd88448e20f8a642831fdff7ea8
@@ -0,0 +1,121 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+ ##
16
+ # This file is here to be autorequired by bundler, so that the .bigquery and
17
+ # #bigquery methods can be available, but the library and all dependencies won't
18
+ # be loaded until required and used.
19
+
20
+
21
+ gem "google-cloud-core"
22
+ require "google/cloud"
23
+
24
+ module Google
25
+ module Cloud
26
+ ##
27
+ # Creates a new object for connecting to the Translate service.
28
+ # Each call creates a new connection.
29
+ #
30
+ # Unlike other Cloud Platform services, which authenticate using a project
31
+ # ID and OAuth 2.0 credentials, Google Translate API requires a public API
32
+ # access key. (This may change in future releases of Google Translate API.)
33
+ # Follow the general instructions at [Identifying your application to
34
+ # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
35
+ # specific instructions for [Server
36
+ # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
37
+ #
38
+ # @param [String] key a public API access key (not an OAuth 2.0 token)
39
+ # @param [Integer] retries Number of times to retry requests on server
40
+ # error. The default value is `3`. Optional.
41
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
42
+ #
43
+ # @return [Google::Cloud::Translate::Api]
44
+ #
45
+ # @example
46
+ # require "google/cloud"
47
+ #
48
+ # gcloud = Google::Cloud.new
49
+ # translate = gcloud.translate "api-key-abc123XYZ789"
50
+ #
51
+ # translation = translate.translate "Hello world!", to: "la"
52
+ # translation.text #=> "Salve mundi!"
53
+ #
54
+ # @example Using API Key from the environment variable.
55
+ # require "google/cloud"
56
+ #
57
+ # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
58
+ #
59
+ # gcloud = Google::Cloud.new
60
+ # translate = gcloud.translate
61
+ #
62
+ # translation = translate.translate "Hello world!", to: "la"
63
+ # translation.text #=> "Salve mundi!"
64
+ #
65
+ def translate key = nil, retries: nil, timeout: nil
66
+ Google::Cloud.translate key, retries: (retries || @retries),
67
+ timeout: (timeout || @timeout)
68
+ end
69
+
70
+ ##
71
+ # Creates a new object for connecting to the Translate service.
72
+ # Each call creates a new connection.
73
+ #
74
+ # Unlike other Cloud Platform services, which authenticate using a project
75
+ # ID and OAuth 2.0 credentials, Google Translate API requires a public API
76
+ # access key. (This may change in future releases of Google Translate API.)
77
+ # Follow the general instructions at [Identifying your application to
78
+ # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
79
+ # specific instructions for [Server
80
+ # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
81
+ #
82
+ # @param [String] key a public API access key (not an OAuth 2.0 token)
83
+ # @param [Integer] retries Number of times to retry requests on server
84
+ # error. The default value is `3`. Optional.
85
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
86
+ #
87
+ # @return [Google::Cloud::Translate::Api]
88
+ #
89
+ # @example
90
+ # require "google/cloud"
91
+ #
92
+ # translate = Google::Cloud.translate "api-key-abc123XYZ789"
93
+ #
94
+ # translation = translate.translate "Hello world!", to: "la"
95
+ # translation.text #=> "Salve mundi!"
96
+ #
97
+ # @example Using API Key from the environment variable.
98
+ # require "google/cloud"
99
+ #
100
+ # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789"
101
+ #
102
+ # translate = Google::Cloud.translate
103
+ #
104
+ # translation = translate.translate "Hello world!", to: "la"
105
+ # translation.text #=> "Salve mundi!"
106
+ #
107
+ def self.translate key = nil, retries: nil, timeout: nil
108
+ require "google/cloud/translate"
109
+ key ||= ENV["TRANSLATE_KEY"]
110
+ key ||= ENV["GOOGLE_CLOUD_KEY"]
111
+ if key.nil?
112
+ key_missing_msg = "An API key is required to use the Translate API."
113
+ fail ArgumentError, key_missing_msg
114
+ end
115
+
116
+ Google::Cloud::Translate::Api.new(
117
+ Google::Cloud::Translate::Service.new(
118
+ key, retries: retries, timeout: timeout))
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,221 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+
16
+ require "google-cloud-translate"
17
+ require "google/cloud/translate/api"
18
+
19
+ module Google
20
+ module Cloud
21
+ ##
22
+ # # Google Translate API
23
+ #
24
+ # [Google Translate API](https://cloud.google.com/translate/) provides a
25
+ # simple, programmatic interface for translating an arbitrary string into
26
+ # any supported language. It is highly responsive, so websites and
27
+ # applications can integrate with Translate API for fast, dynamic
28
+ # translation of source text. Language detection is also available in cases
29
+ # where the source language is unknown.
30
+ #
31
+ # Translate API supports more than ninety different languages, from
32
+ # Afrikaans to Zulu. Used in combination, this enables translation between
33
+ # thousands of language pairs. Also, you can send in HTML and receive HTML
34
+ # with translated text back. You don't need to extract your source text or
35
+ # reassemble the translated content.
36
+ #
37
+ # ## Authenticating
38
+ #
39
+ # Unlike other Cloud Platform services, which authenticate using a project
40
+ # ID and OAuth 2.0 credentials, Translate API requires a public API access
41
+ # key. (This may change in future releases of Translate API.) Follow the
42
+ # general instructions at [Identifying your application to
43
+ # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
44
+ # specific instructions for [Server
45
+ # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
46
+ #
47
+ # ## Translating texts
48
+ #
49
+ # Translating text from one language to another is easy (and extremely
50
+ # fast.) The only required arguments to
51
+ # {Google::Cloud::Translate::Api#translate} are a string and the [ISO
52
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) code of the
53
+ # language to which you wish to translate.
54
+ #
55
+ # ```ruby
56
+ # require "google/cloud"
57
+ #
58
+ # gcloud = Google::Cloud.new
59
+ # translate = gcloud.translate
60
+ #
61
+ # translation = translate.translate "Hello world!", to: "la"
62
+ #
63
+ # puts translation #=> Salve mundi!
64
+ #
65
+ # translation.from #=> "en"
66
+ # translation.origin #=> "Hello world!"
67
+ # translation.to #=> "la"
68
+ # translation.text #=> "Salve mundi!"
69
+ # ```
70
+ #
71
+ # You may want to use the `from` option to specify the language of the
72
+ # source text, as the following example illustrates. (Single words do not
73
+ # give Translate API much to work with.)
74
+ #
75
+ # ```ruby
76
+ # require "google/cloud"
77
+ #
78
+ # gcloud = Google::Cloud.new
79
+ # translate = gcloud.translate
80
+ #
81
+ # translation = translate.translate "chat", to: "en"
82
+ #
83
+ # translation.detected? #=> true
84
+ # translation.from #=> "en"
85
+ # translation.text #=> "chat"
86
+ #
87
+ # translation = translate.translate "chat", from: "fr", to: "en"
88
+ #
89
+ # translation.detected? #=> false
90
+ # translation.from #=> "fr"
91
+ # translation.text #=> "cat"
92
+ # ```
93
+ #
94
+ # You can pass multiple texts to {Google::Cloud::Translate::Api#translate}.
95
+ #
96
+ # ```ruby
97
+ # require "google/cloud"
98
+ #
99
+ # gcloud = Google::Cloud.new
100
+ # translate = gcloud.translate
101
+ #
102
+ # translations = translate.translate "chien", "chat", from: "fr", to: "en"
103
+ #
104
+ # translations.size #=> 2
105
+ # translations[0].origin #=> "chien"
106
+ # translations[0].text #=> "dog"
107
+ # translations[1].origin #=> "chat"
108
+ # translations[1].text #=> "cat"
109
+ # ```
110
+ #
111
+ # By default, any HTML in your source text will be preserved.
112
+ #
113
+ # ```ruby
114
+ # require "google/cloud"
115
+ #
116
+ # gcloud = Google::Cloud.new
117
+ # translate = gcloud.translate
118
+ #
119
+ # translation = translate.translate "<strong>Hello</strong> world!",
120
+ # to: :la
121
+ # translation.text #=> "<strong>Salve</strong> mundi!"
122
+ # ```
123
+ #
124
+ # ## Detecting languages
125
+ #
126
+ # You can use {Google::Cloud::Translate::Api#detect} to see which language
127
+ # the Translate API ranks as the most likely source language for a text. The
128
+ # `confidence` score is a float value between `0` and `1`.
129
+ #
130
+ # ```ruby
131
+ # require "google/cloud"
132
+ #
133
+ # gcloud = Google::Cloud.new
134
+ # translate = gcloud.translate
135
+ #
136
+ # detection = translate.detect "chat"
137
+ #
138
+ # detection.text #=> "chat"
139
+ # detection.language #=> "en"
140
+ # detection.confidence #=> 0.59922177
141
+ # ```
142
+ #
143
+ # You can pass multiple texts to {Google::Cloud::Translate::Api#detect}.
144
+ #
145
+ # ```ruby
146
+ # require "google/cloud"
147
+ #
148
+ # gcloud = Google::Cloud.new
149
+ # translate = gcloud.translate
150
+ #
151
+ # detections = translate.detect "chien", "chat"
152
+ #
153
+ # detections.size #=> 2
154
+ # detections[0].text #=> "chien"
155
+ # detections[0].language #=> "fr"
156
+ # detections[0].confidence #=> 0.7109375
157
+ # detections[1].text #=> "chat"
158
+ # detections[1].language #=> "en"
159
+ # detections[1].confidence #=> 0.59922177
160
+ # ```
161
+ #
162
+ # ## Listing supported languages
163
+ #
164
+ # Translate API adds new languages frequently. You can use
165
+ # {Google::Cloud::Translate::Api#languages} to query the list of supported
166
+ # languages.
167
+ #
168
+ # ```ruby
169
+ # require "google/cloud"
170
+ #
171
+ # gcloud = Google::Cloud.new
172
+ # translate = gcloud.translate
173
+ #
174
+ # languages = translate.languages
175
+ #
176
+ # languages.size #=> 104
177
+ # languages[0].code #=> "af"
178
+ # languages[0].name #=> nil
179
+ # ```
180
+ #
181
+ # To receive the names of the supported languages, as well as their [ISO
182
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) codes,
183
+ # provide the code for the language in which you wish to receive the names.
184
+ #
185
+ # ```ruby
186
+ # require "google/cloud"
187
+ #
188
+ # gcloud = Google::Cloud.new
189
+ # translate = gcloud.translate
190
+ #
191
+ # languages = translate.languages "en"
192
+ #
193
+ # languages.size #=> 104
194
+ # languages[0].code #=> "af"
195
+ # languages[0].name #=> "Afrikaans"
196
+ # ```
197
+ #
198
+ # ## Configuring retries and timeout
199
+ #
200
+ # You can configure how many times API requests may be automatically
201
+ # retried. When an API request fails, the response will be inspected to see
202
+ # if the request meets criteria indicating that it may succeed on retry,
203
+ # such as `500` and `503` status codes or a specific internal error code
204
+ # such as `rateLimitExceeded`. If it meets the criteria, the request will be
205
+ # retried after a delay. If another error occurs, the delay will be
206
+ # increased before a subsequent attempt, until the `retries` limit is
207
+ # reached.
208
+ #
209
+ # You can also set the request `timeout` value in seconds.
210
+ #
211
+ # ```ruby
212
+ # require "google/cloud"
213
+ #
214
+ # gcloud = Google::Cloud.new
215
+ # translate = gcloud.translate retries: 10, timeout: 120
216
+ # ```
217
+ #
218
+ module Translate
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,244 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+
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 Translate API. Each instance
28
+ # requires a public API access key. To create a key, follow the general
29
+ # instructions at [Identifying your application to
30
+ # Google](https://cloud.google.com/translate/v2/using_rest#auth), and the
31
+ # specific instructions for [Server
32
+ # keys](https://cloud.google.com/translate/v2/using_rest#creating-server-api-keys).
33
+ # See {Google::Cloud#translate}.
34
+ #
35
+ # @see https://cloud.google.com/translate/v2/getting_started Translate API
36
+ # Getting Started
37
+ #
38
+ # @example
39
+ # require "google/cloud"
40
+ #
41
+ # gcloud = Google::Cloud.new
42
+ # translate = gcloud.translate
43
+ #
44
+ # translation = translate.translate "Hello world!", to: "la"
45
+ #
46
+ # puts translation #=> Salve mundi!
47
+ #
48
+ # translation.from #=> "en"
49
+ # translation.origin #=> "Hello world!"
50
+ # translation.to #=> "la"
51
+ # translation.text #=> "Salve mundi!"
52
+ #
53
+ class Api
54
+ ##
55
+ # @private The Service object.
56
+ attr_accessor :service
57
+
58
+ ##
59
+ # @private Creates a new Translate Api instance.
60
+ #
61
+ # See {Google::Cloud.translate}
62
+ def initialize service
63
+ @service = service
64
+ end
65
+
66
+ ##
67
+ # Returns text translations from one language to another.
68
+ #
69
+ # @see https://cloud.google.com/translate/v2/using_rest#Translate
70
+ # Translate Text
71
+ #
72
+ # @param [String] text The text or texts to translate.
73
+ # @param [String] to The target language into which the text should be
74
+ # translated. This is required. The value must be an [ISO
75
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
76
+ # language code.
77
+ # @param [String] from The source language of the text or texts. This is
78
+ # an [ISO
79
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
80
+ # language code. This is optional.
81
+ # @param [String] format The format of the text. Possible values include
82
+ # `:text` and `:html`. This is optional. The Translate API default is
83
+ # `:html`.
84
+ # @param [String] cid The customization id for translate. This is
85
+ # optional.
86
+ #
87
+ # @return [Translation, Array<Translation>] A single {Translation}
88
+ # object if just one text was given, or an array of {Translation}
89
+ # objects if multiple texts were given.
90
+ #
91
+ # @example
92
+ # require "google/cloud"
93
+ #
94
+ # gcloud = Google::Cloud.new
95
+ # translate = gcloud.translate
96
+ #
97
+ # translation = translate.translate "Hello world!", to: "la"
98
+ #
99
+ # puts translation #=> Salve mundi!
100
+ #
101
+ # translation.detected? #=> true
102
+ # translation.from #=> "en"
103
+ # translation.origin #=> "Hello world!"
104
+ # translation.to #=> "la"
105
+ # translation.text #=> "Salve mundi!"
106
+ #
107
+ # @example Setting the `from` language.
108
+ # require "google/cloud"
109
+ #
110
+ # gcloud = Google::Cloud.new
111
+ # translate = gcloud.translate
112
+ #
113
+ # translation = translate.translate "Hello world!",
114
+ # from: :en, to: :la
115
+ # translation.detected? #=> false
116
+ # translation.text #=> "Salve mundi!"
117
+ #
118
+ # @example Retrieving multiple translations.
119
+ # require "google/cloud"
120
+ #
121
+ # gcloud = Google::Cloud.new
122
+ # translate = gcloud.translate
123
+ #
124
+ # translations = translate.translate "Hello my friend.",
125
+ # "See you soon.",
126
+ # from: "en", to: "la"
127
+ # translations.count #=> 2
128
+ # translations[0].text #=> "Salve amice."
129
+ # translations[1].text #=> "Vide te mox."
130
+ #
131
+ # @example Preserving HTML tags.
132
+ # require "google/cloud"
133
+ #
134
+ # gcloud = Google::Cloud.new
135
+ # translate = gcloud.translate
136
+ #
137
+ # translation = translate.translate "<strong>Hello</strong> world!",
138
+ # to: :la
139
+ # translation.text #=> "<strong>Salve</strong> mundi!"
140
+ #
141
+ def translate *text, to: nil, from: nil, format: nil, cid: nil
142
+ return nil if text.empty?
143
+ fail ArgumentError, "to is required" if to.nil?
144
+ to = to.to_s
145
+ from = from.to_s if from
146
+ format = format.to_s if format
147
+ text = Array(text).flatten
148
+ gapi = service.translate text, to: to, from: from,
149
+ format: format, cid: cid
150
+ Translation.from_gapi_list gapi, text, to, from
151
+ end
152
+
153
+ ##
154
+ # Detect the most likely language or languages of a text or multiple
155
+ # texts.
156
+ #
157
+ # @see https://cloud.google.com/translate/v2/using_rest#detect-language
158
+ # Detect Language
159
+ #
160
+ # @param [String] text The text or texts upon which language detection
161
+ # should be performed.
162
+ #
163
+ # @return [Detection, Array<Detection>] A single {Detection} object if
164
+ # just one text was given, or an array of {Detection} objects if
165
+ # multiple texts were given.
166
+ #
167
+ # @example
168
+ # require "google/cloud"
169
+ #
170
+ # gcloud = Google::Cloud.new
171
+ # translate = gcloud.translate
172
+ #
173
+ # detection = translate.detect "Hello world!"
174
+ # puts detection.language #=> en
175
+ # puts detection.confidence #=> 0.7100697
176
+ #
177
+ # @example Detecting multiple texts.
178
+ # require "google/cloud"
179
+ #
180
+ # gcloud = Google::Cloud.new
181
+ # translate = gcloud.translate
182
+ #
183
+ # detections = translate.detect "Hello world!",
184
+ # "Bonjour le monde!"
185
+ # puts detections.count #=> 2
186
+ # puts detection.first.language #=> en
187
+ # puts detection.first.confidence #=> 0.7100697
188
+ # puts detection.last.language #=> fr
189
+ # puts detection.last.confidence #=> 0.40440267
190
+ #
191
+ def detect *text
192
+ return nil if text.empty?
193
+ text = Array(text).flatten
194
+ gapi = service.detect(text)
195
+ Detection.from_gapi gapi, text
196
+ end
197
+
198
+ ##
199
+ # List the languages supported by the API. These are the languages to
200
+ # and from which text can be translated.
201
+ #
202
+ # @see https://cloud.google.com/translate/v2/using_rest#supported-languages
203
+ # Discover Supported Languages
204
+ #
205
+ # @param [String] language The language and collation in which the names
206
+ # of the languages are returned. If this is `nil` then no names are
207
+ # returned.
208
+ #
209
+ # @return [Array<Language>] An array of {Language} objects supported by
210
+ # the API.
211
+ #
212
+ # @example
213
+ # require "google/cloud"
214
+ #
215
+ # gcloud = Google::Cloud.new
216
+ # translate = gcloud.translate
217
+ #
218
+ # languages = translate.languages
219
+ # languages.count #=> 104
220
+ #
221
+ # english = languages.detect { |l| l.code == "en" }
222
+ # english.name #=> nil
223
+ #
224
+ # @example Get all languages with their names in French.
225
+ # require "google/cloud"
226
+ #
227
+ # gcloud = Google::Cloud.new
228
+ # translate = gcloud.translate
229
+ #
230
+ # languages = translate.languages "fr"
231
+ # languages.count #=> 104
232
+ #
233
+ # english = languages.detect { |l| l.code == "en" }
234
+ # english.name #=> "Anglais"
235
+ #
236
+ def languages language = nil
237
+ language = language.to_s if language
238
+ gapi = service.languages language
239
+ Array(gapi.languages).map { |g| Language.from_gapi g }
240
+ end
241
+ end
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,140 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+
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/translate/v2/using_rest#detect-language
26
+ # Detect Language
27
+ #
28
+ # @example
29
+ # require "google/cloud"
30
+ #
31
+ # gcloud = Google::Cloud.new
32
+ # translate = gcloud.translate
33
+ #
34
+ # detections = translate.detect "chien", "chat"
35
+ #
36
+ # detections.size #=> 2
37
+ # detections[0].text #=> "chien"
38
+ # detections[0].language #=> "fr"
39
+ # detections[0].confidence #=> 0.7109375
40
+ # detections[1].text #=> "chat"
41
+ # detections[1].language #=> "en"
42
+ # detections[1].confidence #=> 0.59922177
43
+ #
44
+ class Detection
45
+ ##
46
+ # The text upon which the language detection was performed.
47
+ #
48
+ # @return [String]
49
+ attr_reader :text
50
+
51
+ ##
52
+ # The list of detection results for the given text. The most likely
53
+ # language is listed first, and its attributes can be accessed through
54
+ # {#language} and {#confidence}.
55
+ #
56
+ # @return [Array<Detection::Result>]
57
+ attr_reader :results
58
+
59
+ ##
60
+ # @private Create a new object.
61
+ def initialize text, results
62
+ @text = text
63
+ @results = results
64
+ end
65
+
66
+ ##
67
+ # The confidence that the language detection result is correct. The
68
+ # closer this value is to 1, the higher the confidence in language
69
+ # detection.
70
+ #
71
+ # @return [Float] a value between 0 and 1
72
+ def confidence
73
+ return nil if results.empty?
74
+ results.first.confidence
75
+ end
76
+
77
+ ##
78
+ # The most likely language that was detected. This is an [ISO
79
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language
80
+ # code.
81
+ #
82
+ # @return [String] the language code
83
+ def language
84
+ return nil if results.empty?
85
+ results.first.language
86
+ end
87
+
88
+ ##
89
+ # @private New Detection from a ListDetectionsResponse object as
90
+ # defined by the Google API Client object.
91
+ def self.from_gapi gapi, text
92
+ res = text.zip(Array(gapi.detections)).map do |txt, detections_gapi|
93
+ results = detections_gapi.map { |g| Result.from_gapi g }
94
+ new txt, results
95
+ end
96
+ return res.first if res.size == 1
97
+ res
98
+ end
99
+
100
+ ##
101
+ # # Result
102
+ #
103
+ # Represents an individual result in a
104
+ # {Google::Cloud::Translate::Detection} result.
105
+ #
106
+ class Result
107
+ ##
108
+ # The confidence that the language detection result is correct. The
109
+ # closer this value is to 1, the higher the confidence in language
110
+ # detection.
111
+ #
112
+ # @return [Float] a value between 0 and 1
113
+ attr_reader :confidence
114
+
115
+ ##
116
+ # The language detected. This is an [ISO
117
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
118
+ # language code.
119
+ #
120
+ # @return [String] the language code
121
+ attr_reader :language
122
+
123
+ ##
124
+ # @private Create a new object.
125
+ def initialize confidence, language
126
+ @confidence = confidence
127
+ @language = language
128
+ end
129
+
130
+ ##
131
+ # @private New Detection::Result from a DetectionsResource object as
132
+ # defined by the Google API Client object.
133
+ def self.from_gapi gapi
134
+ new gapi.confidence, gapi.language
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,71 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+
16
+ module Google
17
+ module Cloud
18
+ module Translate
19
+ ##
20
+ # # Language
21
+ #
22
+ # Represents a supported languages query result. Returned by
23
+ # {Google::Cloud::Translate::Api#languages}.
24
+ #
25
+ # @see https://cloud.google.com/translate/v2/using_rest#supported-languages
26
+ # Discover Supported Languages
27
+ #
28
+ # @example
29
+ # require "google/cloud"
30
+ #
31
+ # gcloud = Google::Cloud.new
32
+ # translate = gcloud.translate
33
+ #
34
+ # languages = translate.languages "en"
35
+ #
36
+ # languages.size #=> 104
37
+ # languages[0].code #=> "af"
38
+ # languages[0].name #=> "Afrikaans"
39
+ #
40
+ class Language
41
+ ##
42
+ # The language code. This is an [ISO
43
+ # 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language
44
+ # code.
45
+ #
46
+ # @return [String]
47
+ attr_reader :code
48
+
49
+ ##
50
+ # The localized name of the language, if available.
51
+ #
52
+ # @return [String]
53
+ attr_reader :name
54
+
55
+ ##
56
+ # @private Create a new object.
57
+ def initialize code, name
58
+ @code = code
59
+ @name = name
60
+ end
61
+
62
+ ##
63
+ # @private New Language from a LanguagesResource object as defined by
64
+ # the Google API Client object.
65
+ def self.from_gapi gapi
66
+ new gapi.language, gapi.name
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,87 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+
16
+ require "google/cloud/errors"
17
+ require "google/cloud/translate/version"
18
+ require "google/apis/translate_v2"
19
+
20
+ module Google
21
+ module Cloud
22
+ module Translate
23
+ ##
24
+ # @private
25
+ # Represents the service to Translate, exposing the API calls.
26
+ class Service
27
+ ##
28
+ # Alias to the Google Client API module
29
+ API = Google::Apis::TranslateV2
30
+
31
+ attr_accessor :credentials
32
+
33
+ ##
34
+ # Creates a new Service instance.
35
+ def initialize key, retries: nil, timeout: nil
36
+ @service = API::TranslateService.new
37
+ @service.client_options.application_name = "google-cloud-translate"
38
+ @service.client_options.application_version = \
39
+ Google::Cloud::Translate::VERSION
40
+ @service.request_options.retries = retries || 3
41
+ @service.request_options.timeout_sec = timeout if timeout
42
+ @service.authorization = nil
43
+ @service.key = key
44
+ end
45
+
46
+ def service
47
+ return mocked_service if mocked_service
48
+ @service
49
+ end
50
+ attr_accessor :mocked_service
51
+
52
+ ##
53
+ # Returns API::ListTranslationsResponse
54
+ def translate text, to: nil, from: nil, format: nil, cid: nil
55
+ execute do
56
+ service.list_translations Array(text), to, cid: cid, format: format,
57
+ source: from
58
+ end
59
+ end
60
+
61
+ ##
62
+ # Returns API::ListDetectionsResponse
63
+ def detect text
64
+ execute { service.list_detections Array(text) }
65
+ end
66
+
67
+ ##
68
+ # Returns API::ListLanguagesResponse
69
+ def languages language = nil
70
+ execute { service.list_languages target: language }
71
+ end
72
+
73
+ def inspect
74
+ "#{self.class}"
75
+ end
76
+
77
+ protected
78
+
79
+ def execute
80
+ yield
81
+ rescue Google::Apis::Error => e
82
+ raise Google::Cloud::Error.from_error(e)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,114 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+
16
+ module Google
17
+ module Cloud
18
+ module Translate
19
+ ##
20
+ # # Translation
21
+ #
22
+ # Represents a translation query result. Returned by
23
+ # {Google::Cloud::Translate::Api#translate}.
24
+ #
25
+ # @see https://cloud.google.com/translate/v2/using_rest#Translate
26
+ # Translate Text
27
+ #
28
+ # @example
29
+ # require "google/cloud"
30
+ #
31
+ # gcloud = Google::Cloud.new
32
+ # translate = gcloud.translate
33
+ #
34
+ # translation = translate.translate "Hello world!", to: "la"
35
+ #
36
+ # puts translation #=> Salve mundi!
37
+ #
38
+ # translation.from #=> "en"
39
+ # translation.origin #=> "Hello world!"
40
+ # translation.to #=> "la"
41
+ # translation.text #=> "Salve mundi!"
42
+ #
43
+ class Translation
44
+ ##
45
+ # The translated result.
46
+ #
47
+ # @return [String]
48
+ attr_reader :text
49
+ alias_method :to_s, :text
50
+ alias_method :to_str, :text
51
+
52
+ ##
53
+ # The original query text that was translated.
54
+ #
55
+ # @return [String]
56
+ attr_reader :origin
57
+
58
+ ##
59
+ # The target language into which the text was translated.
60
+ #
61
+ # @return [String]
62
+ attr_reader :to
63
+ alias_method :language, :to
64
+ alias_method :target, :to
65
+
66
+ ##
67
+ # The source language from which the text was translated.
68
+ attr_reader :from
69
+ alias_method :source, :from
70
+
71
+ ##
72
+ # @private Create a new object.
73
+ def initialize text, to, origin, from, detected
74
+ @text = text
75
+ @to = to
76
+ @origin = origin
77
+ @from = from
78
+ @detected = detected
79
+ end
80
+
81
+ ##
82
+ # Determines if the source language was detected by the Google Cloud
83
+ # Translate API.
84
+ #
85
+ # @return [Boolean] `true` if the source language was detected by the
86
+ # Translate service, `false` if the source language was provided in
87
+ # the request
88
+ def detected?
89
+ @detected
90
+ end
91
+
92
+ ##
93
+ # @private New Translation from a TranslationsListResponse object as
94
+ # defined by the Google API Client object.
95
+ def self.from_gapi_list gapi, text, to, from
96
+ res = text.zip(Array(gapi.translations)).map do |origin, g|
97
+ from_gapi g, to, origin, from
98
+ end
99
+ return res.first if res.size == 1
100
+ res
101
+ end
102
+
103
+ ##
104
+ # @private New Translation from a TranslationsResource object as defined
105
+ # by the Google API Client object.
106
+ def self.from_gapi gapi, to, origin, from
107
+ from ||= gapi.detected_source_language
108
+ detected = !gapi.detected_source_language.nil?
109
+ new gapi.translated_text, to, origin, from, detected
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+
16
+ module Google
17
+ module Cloud
18
+ module Translate
19
+ VERSION = "0.20.0"
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-cloud-translate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.20.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Moore
8
+ - Chris Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-08-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: google-cloud-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.20.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.20.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: google-api-client
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.9.11
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.9.11
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '5.9'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '5.9'
56
+ - !ruby/object:Gem::Dependency
57
+ name: minitest-autotest
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: minitest-focus
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.1'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.1'
84
+ - !ruby/object:Gem::Dependency
85
+ name: minitest-rg
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '5.2'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '5.2'
98
+ - !ruby/object:Gem::Dependency
99
+ name: autotest-suffix
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '1.1'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '1.1'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rubocop
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "<="
117
+ - !ruby/object:Gem::Version
118
+ version: 0.35.1
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "<="
124
+ - !ruby/object:Gem::Version
125
+ version: 0.35.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: simplecov
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '0.9'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '0.9'
140
+ - !ruby/object:Gem::Dependency
141
+ name: yard
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '0.9'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: '0.9'
154
+ description: google-cloud-translate is the official library for Google Translate API.
155
+ email:
156
+ - mike@blowmage.com
157
+ - quartzmo@gmail.com
158
+ executables: []
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - lib/google-cloud-translate.rb
163
+ - lib/google/cloud/translate.rb
164
+ - lib/google/cloud/translate/api.rb
165
+ - lib/google/cloud/translate/detection.rb
166
+ - lib/google/cloud/translate/language.rb
167
+ - lib/google/cloud/translate/service.rb
168
+ - lib/google/cloud/translate/translation.rb
169
+ - lib/google/cloud/translate/version.rb
170
+ homepage: http://googlecloudplatform.github.io/google-cloud-ruby/
171
+ licenses:
172
+ - Apache-2.0
173
+ metadata: {}
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: 2.0.0
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubyforge_project:
190
+ rubygems_version: 2.6.4
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: API Client library for Google Translate API
194
+ test_files: []