google-cloud-language 0.20.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 +7 -0
- data/lib/google-cloud-language.rb +116 -0
- data/lib/google/cloud/language.rb +229 -0
- data/lib/google/cloud/language/annotation.rb +630 -0
- data/lib/google/cloud/language/credentials.rb +32 -0
- data/lib/google/cloud/language/document.rb +368 -0
- data/lib/google/cloud/language/project.rb +365 -0
- data/lib/google/cloud/language/service.rb +112 -0
- data/lib/google/cloud/language/v1beta1/language_service_api.rb +204 -0
- data/lib/google/cloud/language/v1beta1/language_service_client_config.json +43 -0
- data/lib/google/cloud/language/v1beta1/language_service_pb.rb +228 -0
- data/lib/google/cloud/language/v1beta1/language_service_services_pb.rb +54 -0
- data/lib/google/cloud/language/version.rb +22 -0
- metadata +241 -0
@@ -0,0 +1,32 @@
|
|
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/credentials"
|
17
|
+
require "google/cloud/language/v1beta1/language_service_api"
|
18
|
+
|
19
|
+
module Google
|
20
|
+
module Cloud
|
21
|
+
module Language
|
22
|
+
##
|
23
|
+
# @private Represents the OAuth 2.0 signing logic for Language.
|
24
|
+
class Credentials < Google::Cloud::Credentials
|
25
|
+
SCOPE = Google::Cloud::Language::V1beta1::LanguageServiceApi::ALL_SCOPES
|
26
|
+
PATH_ENV_VARS = %w(LANGUAGE_KEYFILE GOOGLE_CLOUD_KEYFILE GCLOUD_KEYFILE)
|
27
|
+
JSON_ENV_VARS = %w(LANGUAGE_KEYFILE_JSON GOOGLE_CLOUD_KEYFILE_JSON
|
28
|
+
GCLOUD_KEYFILE_JSON)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,368 @@
|
|
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/language/annotation"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Language
|
21
|
+
##
|
22
|
+
# # Document
|
23
|
+
#
|
24
|
+
# Represents a document for the Language service.
|
25
|
+
#
|
26
|
+
# Cloud Natural Language API supports UTF-8, UTF-16, and UTF-32 encodings.
|
27
|
+
# (Ruby uses UTF-8 natively, which is the default sent to the API, so
|
28
|
+
# unless you're working with text processed in different platform, you
|
29
|
+
# should not need to set the encoding type.)
|
30
|
+
#
|
31
|
+
# Be aware that only English, Spanish, and Japanese language content are
|
32
|
+
# supported, and sentiment analysis only supports English text.
|
33
|
+
#
|
34
|
+
# See {Project#document}.
|
35
|
+
#
|
36
|
+
# @example
|
37
|
+
# require "google/cloud"
|
38
|
+
#
|
39
|
+
# gcloud = Google::Cloud.new
|
40
|
+
# language = gcloud.language
|
41
|
+
#
|
42
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
43
|
+
# document = language.document content
|
44
|
+
# annotation = document.annotate
|
45
|
+
#
|
46
|
+
# annotation.entities.count #=> 2
|
47
|
+
# annotation.sentiment.polarity #=> 1.0
|
48
|
+
# annotation.sentiment.magnitude #=> 0.8999999761581421
|
49
|
+
# annotation.sentences.count #=> 1
|
50
|
+
# annotation.tokens.count #=> 10
|
51
|
+
#
|
52
|
+
class Document
|
53
|
+
##
|
54
|
+
# @private The gRPC Service object.
|
55
|
+
attr_accessor :service
|
56
|
+
|
57
|
+
##
|
58
|
+
# @private Creates a new document instance.
|
59
|
+
def initialize
|
60
|
+
@grpc = nil
|
61
|
+
@service = nil
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# @private Whether the document has content.
|
66
|
+
#
|
67
|
+
def content?
|
68
|
+
@grpc.source == :content
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# @private Whether the document source is a Google Cloud Storage URI.
|
73
|
+
#
|
74
|
+
def url?
|
75
|
+
@grpc.source == :gcs_content_uri
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# @private The source of the document's content.
|
80
|
+
#
|
81
|
+
def source
|
82
|
+
return @grpc.content if content?
|
83
|
+
@grpc.gcs_content_uri
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# The document's format.
|
88
|
+
#
|
89
|
+
# @return [Symbol] `:text` or `:html`
|
90
|
+
#
|
91
|
+
def format
|
92
|
+
return :text if text?
|
93
|
+
return :html if html?
|
94
|
+
end
|
95
|
+
|
96
|
+
##
|
97
|
+
# Sets the document's format.
|
98
|
+
#
|
99
|
+
# @param [Symbol, String] new_format Accepted values are `:text` or
|
100
|
+
# `:html`.
|
101
|
+
#
|
102
|
+
# @example
|
103
|
+
# document = language.document "<p>The Old Man and the Sea</p>"
|
104
|
+
# document.format = :html
|
105
|
+
#
|
106
|
+
def format= new_format
|
107
|
+
@grpc.type = :PLAIN_TEXT if new_format.to_s == "text"
|
108
|
+
@grpc.type = :HTML if new_format.to_s == "html"
|
109
|
+
@grpc.type
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# Whether the document is the `TEXT` format.
|
114
|
+
#
|
115
|
+
# @return [Boolean]
|
116
|
+
#
|
117
|
+
def text?
|
118
|
+
@grpc.type == :PLAIN_TEXT
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# Sets the document to the `TEXT` format.
|
123
|
+
#
|
124
|
+
def text!
|
125
|
+
@grpc.type = :PLAIN_TEXT
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# Whether the document is the `HTML` format.
|
130
|
+
#
|
131
|
+
# @return [Boolean]
|
132
|
+
#
|
133
|
+
def html?
|
134
|
+
@grpc.type == :HTML
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# Sets the document to the `HTML` format.
|
139
|
+
#
|
140
|
+
def html!
|
141
|
+
@grpc.type = :HTML
|
142
|
+
end
|
143
|
+
|
144
|
+
##
|
145
|
+
# The document's language. ISO and BCP-47 language codes are supported.
|
146
|
+
#
|
147
|
+
# @return [String]
|
148
|
+
#
|
149
|
+
def language
|
150
|
+
@grpc.language
|
151
|
+
end
|
152
|
+
|
153
|
+
##
|
154
|
+
# Sets the document's language.
|
155
|
+
#
|
156
|
+
# @param [String, Symbol] new_language ISO and BCP-47 language codes are
|
157
|
+
# accepted.
|
158
|
+
#
|
159
|
+
# @example
|
160
|
+
# document = language.document "<p>El viejo y el mar</p>"
|
161
|
+
# document.language = "es"
|
162
|
+
#
|
163
|
+
def language= new_language
|
164
|
+
@grpc.language = new_language.to_s
|
165
|
+
end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Analyzes the document and returns sentiment, entity, and syntactic
|
169
|
+
# feature results, depending on the option flags. Calling `annotate`
|
170
|
+
# with no arguments will perform **all** analysis features. Each feature
|
171
|
+
# is priced separately. See [Pricing](https://cloud.google.com/natural-language/pricing)
|
172
|
+
# for details.
|
173
|
+
#
|
174
|
+
# @param [Boolean] sentiment Whether to perform sentiment analysis.
|
175
|
+
# Optional. The default is `false`. If every feature option is
|
176
|
+
# `false`, **all** features will be performed.
|
177
|
+
# @param [Boolean] entities Whether to perform the entity analysis.
|
178
|
+
# Optional. The default is `false`. If every feature option is
|
179
|
+
# `false`, **all** features will be performed.
|
180
|
+
# @param [Boolean] syntax Whether to perform syntactic analysis.
|
181
|
+
# Optional. The default is `false`. If every feature option is
|
182
|
+
# `false`, **all** features will be performed.
|
183
|
+
# @param [String] encoding The encoding type used by the API to
|
184
|
+
# calculate offsets. Optional.
|
185
|
+
#
|
186
|
+
# @return [Annotation>] The results of the content analysis.
|
187
|
+
#
|
188
|
+
# @example
|
189
|
+
# require "google/cloud"
|
190
|
+
#
|
191
|
+
# gcloud = Google::Cloud.new
|
192
|
+
# language = gcloud.language
|
193
|
+
#
|
194
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
195
|
+
# document = language.document content
|
196
|
+
# annotation = document.annotate
|
197
|
+
#
|
198
|
+
# annotation.sentiment.polarity #=> 1.0
|
199
|
+
# annotation.sentiment.magnitude #=> 0.8999999761581421
|
200
|
+
# annotation.entities.count #=> 2
|
201
|
+
# annotation.sentences.count #=> 1
|
202
|
+
# annotation.tokens.count #=> 10
|
203
|
+
#
|
204
|
+
# @example With feature flags:
|
205
|
+
# require "google/cloud"
|
206
|
+
#
|
207
|
+
# gcloud = Google::Cloud.new
|
208
|
+
# language = gcloud.language
|
209
|
+
#
|
210
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
211
|
+
# document = language.document content
|
212
|
+
# annotation = document.annotate entities: true, text: true
|
213
|
+
#
|
214
|
+
# annotation.sentiment #=> nil
|
215
|
+
# annotation.entities.count #=> 2
|
216
|
+
# annotation.sentences.count #=> 1
|
217
|
+
# annotation.tokens.count #=> 10
|
218
|
+
#
|
219
|
+
def annotate sentiment: false, entities: false, syntax: false,
|
220
|
+
encoding: nil
|
221
|
+
ensure_service!
|
222
|
+
grpc = service.annotate to_grpc, sentiment: sentiment,
|
223
|
+
entities: entities,
|
224
|
+
syntax: syntax,
|
225
|
+
encoding: encoding
|
226
|
+
Annotation.from_grpc grpc
|
227
|
+
end
|
228
|
+
alias_method :mark, :annotate
|
229
|
+
alias_method :detect, :annotate
|
230
|
+
|
231
|
+
##
|
232
|
+
# Syntactic analysis extracts linguistic information, breaking up the
|
233
|
+
# given text into a series of sentences and tokens (generally, word
|
234
|
+
# boundaries), providing further analysis on those tokens.
|
235
|
+
#
|
236
|
+
# @param [String] encoding The encoding type used by the API to
|
237
|
+
# calculate offsets. Optional.
|
238
|
+
#
|
239
|
+
# @return [Annotation>] The results for the content analysis.
|
240
|
+
#
|
241
|
+
# @example
|
242
|
+
# require "google/cloud"
|
243
|
+
#
|
244
|
+
# gcloud = Google::Cloud.new
|
245
|
+
# language = gcloud.language
|
246
|
+
#
|
247
|
+
# document = language.document "Hello world!"
|
248
|
+
#
|
249
|
+
# annotation = document.syntax
|
250
|
+
# annotation.thing #=> Some Result
|
251
|
+
#
|
252
|
+
def syntax encoding: nil
|
253
|
+
annotate syntax: true, encoding: encoding
|
254
|
+
end
|
255
|
+
|
256
|
+
##
|
257
|
+
# Entity analysis inspects the given text for known entities (proper
|
258
|
+
# nouns such as public figures, landmarks, etc.) and returns information
|
259
|
+
# about those entities.
|
260
|
+
#
|
261
|
+
# @param [String] encoding The encoding type used by the API to
|
262
|
+
# calculate offsets. Optional.
|
263
|
+
#
|
264
|
+
# @return [Annotation::Entities>] The results for the entities analysis.
|
265
|
+
#
|
266
|
+
# @example
|
267
|
+
# require "google/cloud"
|
268
|
+
#
|
269
|
+
# gcloud = Google::Cloud.new
|
270
|
+
# language = gcloud.language
|
271
|
+
#
|
272
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
273
|
+
# document = language.document content
|
274
|
+
# entities = document.entities # API call
|
275
|
+
#
|
276
|
+
# entities.count #=> 2
|
277
|
+
# entities.first.name #=> "Darth Vader"
|
278
|
+
# entities.first.type #=> :PERSON
|
279
|
+
# entities.first.name #=> "Star Wars"
|
280
|
+
# entities.first.type #=> :WORK_OF_ART
|
281
|
+
#
|
282
|
+
def entities encoding: nil
|
283
|
+
ensure_service!
|
284
|
+
grpc = service.entities to_grpc, encoding: encoding
|
285
|
+
Annotation::Entities.from_grpc grpc
|
286
|
+
end
|
287
|
+
|
288
|
+
##
|
289
|
+
# Sentiment analysis inspects the given text and identifies the
|
290
|
+
# prevailing emotional opinion within the text, especially to determine
|
291
|
+
# a writer's attitude as positive, negative, or neutral. Currently, only
|
292
|
+
# English is supported for sentiment analysis.
|
293
|
+
#
|
294
|
+
# @return [Annotation::Sentiment>] The results for the sentiment
|
295
|
+
# analysis.
|
296
|
+
#
|
297
|
+
# @example
|
298
|
+
# require "google/cloud"
|
299
|
+
#
|
300
|
+
# gcloud = Google::Cloud.new
|
301
|
+
# language = gcloud.language
|
302
|
+
#
|
303
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
304
|
+
# document = language.document content
|
305
|
+
# sentiment = document.sentiment # API call
|
306
|
+
#
|
307
|
+
# sentiment.polarity #=> 1.0
|
308
|
+
# sentiment.magnitude #=> 0.8999999761581421
|
309
|
+
#
|
310
|
+
def sentiment
|
311
|
+
ensure_service!
|
312
|
+
grpc = service.sentiment to_grpc
|
313
|
+
Annotation::Sentiment.from_grpc grpc
|
314
|
+
end
|
315
|
+
|
316
|
+
# @private
|
317
|
+
def inspect
|
318
|
+
"#<#{self.class.name} (" \
|
319
|
+
"#{(content? ? "\"#{source[0, 16]}...\"" : source)}, " \
|
320
|
+
"format: #{format.inspect}, language: #{language.inspect})>"
|
321
|
+
end
|
322
|
+
|
323
|
+
##
|
324
|
+
# @private New gRPC object.
|
325
|
+
def to_grpc
|
326
|
+
@grpc
|
327
|
+
end
|
328
|
+
|
329
|
+
##
|
330
|
+
# @private
|
331
|
+
def self.from_grpc grpc, service
|
332
|
+
new.tap do |i|
|
333
|
+
i.instance_variable_set :@grpc, grpc
|
334
|
+
i.instance_variable_set :@service, service
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
##
|
339
|
+
# @private
|
340
|
+
def self.from_source source, service, format: nil, language: nil
|
341
|
+
source = String source
|
342
|
+
grpc = Google::Cloud::Language::V1beta1::Document.new
|
343
|
+
if source.start_with? "gs://"
|
344
|
+
grpc.gcs_content_uri = source
|
345
|
+
format ||= :html if source.end_with? ".html"
|
346
|
+
else
|
347
|
+
grpc.content = source
|
348
|
+
end
|
349
|
+
if format.to_s == "html"
|
350
|
+
grpc.type = :HTML
|
351
|
+
else
|
352
|
+
grpc.type = :PLAIN_TEXT
|
353
|
+
end
|
354
|
+
grpc.language = language.to_s unless language.nil?
|
355
|
+
from_grpc grpc, service
|
356
|
+
end
|
357
|
+
|
358
|
+
protected
|
359
|
+
|
360
|
+
##
|
361
|
+
# Raise an error unless an active language project object is available.
|
362
|
+
def ensure_service!
|
363
|
+
fail "Must have active connection" unless service
|
364
|
+
end
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
@@ -0,0 +1,365 @@
|
|
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/core/gce"
|
18
|
+
require "google/cloud/language/service"
|
19
|
+
require "google/cloud/language/document"
|
20
|
+
require "google/cloud/language/annotation"
|
21
|
+
|
22
|
+
module Google
|
23
|
+
module Cloud
|
24
|
+
module Language
|
25
|
+
##
|
26
|
+
# # Project
|
27
|
+
#
|
28
|
+
# Google Cloud Natural Language API reveals the structure and meaning of
|
29
|
+
# text by offering powerful machine learning models in an easy to use REST
|
30
|
+
# API. You can analyze text uploaded in your request or integrate with
|
31
|
+
# your document storage on Google Cloud Storage.
|
32
|
+
#
|
33
|
+
# See {Google::Cloud#language}
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# require "google/cloud"
|
37
|
+
#
|
38
|
+
# gcloud = Google::Cloud.new
|
39
|
+
# language = gcloud.language
|
40
|
+
#
|
41
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
42
|
+
# annotation = language.annotate content
|
43
|
+
#
|
44
|
+
# annotation.sentiment.polarity #=> 1.0
|
45
|
+
# annotation.sentiment.magnitude #=> 0.8999999761581421
|
46
|
+
# annotation.entities.count #=> 2
|
47
|
+
# annotation.sentences.count #=> 1
|
48
|
+
# annotation.tokens.count #=> 10
|
49
|
+
#
|
50
|
+
class Project
|
51
|
+
##
|
52
|
+
# @private The gRPC Service object.
|
53
|
+
attr_accessor :service
|
54
|
+
|
55
|
+
##
|
56
|
+
# @private Creates a new Language Project instance.
|
57
|
+
def initialize service
|
58
|
+
@service = service
|
59
|
+
end
|
60
|
+
|
61
|
+
# The Language project connected to.
|
62
|
+
#
|
63
|
+
# @example
|
64
|
+
# require "google/cloud"
|
65
|
+
#
|
66
|
+
# gcloud = Google::Cloud.new "my-project-id",
|
67
|
+
# "/path/to/keyfile.json"
|
68
|
+
# language = gcloud.language
|
69
|
+
#
|
70
|
+
# language.project #=> "my-project-id"
|
71
|
+
#
|
72
|
+
def project
|
73
|
+
service.project
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# @private Default project.
|
78
|
+
def self.default_project
|
79
|
+
ENV["LANGUAGE_PROJECT"] ||
|
80
|
+
ENV["GOOGLE_CLOUD_PROJECT"] ||
|
81
|
+
ENV["GCLOUD_PROJECT"] ||
|
82
|
+
Google::Cloud::Core::GCE.project_id
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# Returns a new document from the given content. No API call is made.
|
87
|
+
#
|
88
|
+
# @param [String, Google::Cloud::Storage::File] content A string of text
|
89
|
+
# to be annotated, or a Cloud Storage URI of the form
|
90
|
+
# `"gs://bucketname/path/to/document.ext"`; or an instance of
|
91
|
+
# Google::Cloud::Storage::File of the text to be annotated.
|
92
|
+
# @param [String] format The format of the document (TEXT/HTML).
|
93
|
+
# Optional.
|
94
|
+
# @param [String] language The language of the document (if not
|
95
|
+
# specified, the language is automatically detected). Both ISO and
|
96
|
+
# BCP-47 language codes are accepted. Optional.
|
97
|
+
#
|
98
|
+
# @return [Document] An document for the Language service.
|
99
|
+
#
|
100
|
+
# @example
|
101
|
+
# require "google/cloud"
|
102
|
+
#
|
103
|
+
# gcloud = Google::Cloud.new
|
104
|
+
# language = gcloud.language
|
105
|
+
#
|
106
|
+
# document = language.document "It was the best of times, it was..."
|
107
|
+
#
|
108
|
+
# @example With a Google Cloud Storage URI:
|
109
|
+
# require "google/cloud"
|
110
|
+
#
|
111
|
+
# gcloud = Google::Cloud.new
|
112
|
+
# language = gcloud.language
|
113
|
+
#
|
114
|
+
# document = language.document "gs://bucket-name/path/to/document"
|
115
|
+
#
|
116
|
+
# @example With a Google Cloud Storage File object:
|
117
|
+
# require "google/cloud"
|
118
|
+
#
|
119
|
+
# gcloud = Google::Cloud.new
|
120
|
+
# storage = gcloud.storage
|
121
|
+
#
|
122
|
+
# bucket = storage.bucket "bucket-name"
|
123
|
+
# file = bucket.file "path/to/document"
|
124
|
+
#
|
125
|
+
# language = gcloud.language
|
126
|
+
#
|
127
|
+
# document = language.document file
|
128
|
+
#
|
129
|
+
# @example With `format` and `language` options:
|
130
|
+
# require "google/cloud"
|
131
|
+
#
|
132
|
+
# gcloud = Google::Cloud.new
|
133
|
+
# language = gcloud.language
|
134
|
+
#
|
135
|
+
# document = language.document "<p>El viejo y el mar</p>",
|
136
|
+
# format: :html, language: "es"
|
137
|
+
#
|
138
|
+
def document content, format: nil, language: nil
|
139
|
+
content = content.to_gs_url if content.respond_to? :to_gs_url
|
140
|
+
if content.is_a? Document
|
141
|
+
# Create new document with the provided format and language
|
142
|
+
Document.from_source content.source, @service,
|
143
|
+
format: (format || content.format),
|
144
|
+
language: (language || content.language)
|
145
|
+
else
|
146
|
+
Document.from_source content, @service, format: format,
|
147
|
+
language: language
|
148
|
+
end
|
149
|
+
end
|
150
|
+
alias_method :doc, :document
|
151
|
+
|
152
|
+
##
|
153
|
+
# Returns a new document from the given content with the `format` value
|
154
|
+
# `:text`. No API call is made.
|
155
|
+
#
|
156
|
+
# @param [String, Google::Cloud::Storage::File] content A string of text
|
157
|
+
# to be annotated, or a Cloud Storage URI of the form
|
158
|
+
# `"gs://bucketname/path/to/document.ext"`; or an instance of
|
159
|
+
# Google::Cloud::Storage::File of the text to be annotated.
|
160
|
+
# @param [String] language The language of the document (if not
|
161
|
+
# specified, the language is automatically detected). Both ISO and
|
162
|
+
# BCP-47 language codes are accepted. Optional.
|
163
|
+
#
|
164
|
+
# @return [Document] An document for the Language service.
|
165
|
+
#
|
166
|
+
def text content, language: nil
|
167
|
+
document content, format: :text, language: language
|
168
|
+
end
|
169
|
+
|
170
|
+
##
|
171
|
+
# Returns a new document from the given content with the `format` value
|
172
|
+
# `:html`. No API call is made.
|
173
|
+
#
|
174
|
+
# @param [String, Google::Cloud::Storage::File] content A string of text
|
175
|
+
# to be annotated, or a Cloud Storage URI of the form
|
176
|
+
# `"gs://bucketname/path/to/document.ext"`; or an instance of
|
177
|
+
# Google::Cloud::Storage::File of the text to be annotated.
|
178
|
+
# @param [String] language The language of the document (if not
|
179
|
+
# specified, the language is automatically detected). Both ISO and
|
180
|
+
# BCP-47 language codes are accepted. Optional.
|
181
|
+
#
|
182
|
+
# @return [Document] An document for the Language service.
|
183
|
+
#
|
184
|
+
def html content, language: nil
|
185
|
+
document content, format: :html, language: language
|
186
|
+
end
|
187
|
+
|
188
|
+
##
|
189
|
+
# Analyzes the content and returns sentiment, entity, and syntactic
|
190
|
+
# feature results, depending on the option flags. Calling `annotate`
|
191
|
+
# with no arguments will perform **all** analysis features. Each feature
|
192
|
+
# is priced separately. See [Pricing](https://cloud.google.com/natural-language/pricing)
|
193
|
+
# for details.
|
194
|
+
#
|
195
|
+
# @param [String, Document, Google::Cloud::Storage::File] content The
|
196
|
+
# content to annotate. This can be an {Document} instance, or any
|
197
|
+
# other type that converts to an {Document}. See {#document} for
|
198
|
+
# details.
|
199
|
+
# @param [Boolean] sentiment Whether to perform the sentiment analysis.
|
200
|
+
# Optional. The default is `false`. If every feature option is
|
201
|
+
# `false`, **all** features will be performed.
|
202
|
+
# @param [Boolean] entities Whether to perform the entity analysis.
|
203
|
+
# Optional. The default is `false`. If every feature option is
|
204
|
+
# `false`, **all** features will be performed.
|
205
|
+
# @param [Boolean] syntax Whether to perform the syntactic analysis.
|
206
|
+
# Optional. The default is `false`. If every feature option is
|
207
|
+
# `false`, **all** features will be performed.
|
208
|
+
# @param [String] format The format of the document (TEXT/HTML).
|
209
|
+
# Optional.
|
210
|
+
# @param [String] language The language of the document (if not
|
211
|
+
# specified, the language is automatically detected). Both ISO and
|
212
|
+
# BCP-47 language codes are accepted. Optional.
|
213
|
+
# @param [String] encoding The encoding type used by the API to
|
214
|
+
# calculate offsets. Optional.
|
215
|
+
#
|
216
|
+
# @return [Annotation>] The results for the content analysis.
|
217
|
+
#
|
218
|
+
# @example
|
219
|
+
# require "google/cloud"
|
220
|
+
#
|
221
|
+
# gcloud = Google::Cloud.new
|
222
|
+
# language = gcloud.language
|
223
|
+
#
|
224
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
225
|
+
# annotation = language.annotate content
|
226
|
+
#
|
227
|
+
# annotation.sentiment.polarity #=> 1.0
|
228
|
+
# annotation.sentiment.magnitude #=> 0.8999999761581421
|
229
|
+
# annotation.entities.count #=> 2
|
230
|
+
# annotation.sentences.count #=> 1
|
231
|
+
# annotation.tokens.count #=> 10
|
232
|
+
#
|
233
|
+
def annotate content, sentiment: false, entities: false, syntax: false,
|
234
|
+
format: nil, language: nil, encoding: nil
|
235
|
+
ensure_service!
|
236
|
+
doc = document content, language: language, format: format
|
237
|
+
grpc = service.annotate doc.to_grpc, sentiment: sentiment,
|
238
|
+
entities: entities,
|
239
|
+
syntax: syntax,
|
240
|
+
encoding: encoding
|
241
|
+
Annotation.from_grpc grpc
|
242
|
+
end
|
243
|
+
alias_method :mark, :annotate
|
244
|
+
alias_method :detect, :annotate
|
245
|
+
|
246
|
+
##
|
247
|
+
# Syntactic analysis extracts linguistic information, breaking up the
|
248
|
+
# given text into a series of sentences and tokens (generally, word
|
249
|
+
# boundaries), providing further analysis on those tokens.
|
250
|
+
#
|
251
|
+
# @param [String, Document, Google::Cloud::Storage::File] content The
|
252
|
+
# content to annotate. This can be an {Document} instance, or any
|
253
|
+
# other type that converts to an {Document}. See {#document} for
|
254
|
+
# details.
|
255
|
+
# @param [String] format The format of the document (TEXT/HTML).
|
256
|
+
# Optional.
|
257
|
+
# @param [String] language The language of the document (if not
|
258
|
+
# specified, the language is automatically detected). Both ISO and
|
259
|
+
# BCP-47 language codes are accepted. Optional.
|
260
|
+
# @param [String] encoding The encoding type used by the API to
|
261
|
+
# calculate offsets. Optional.
|
262
|
+
#
|
263
|
+
# @return [Annotation>] The results for the content syntax analysis.
|
264
|
+
#
|
265
|
+
# @example
|
266
|
+
# require "google/cloud"
|
267
|
+
#
|
268
|
+
# gcloud = Google::Cloud.new
|
269
|
+
# language = gcloud.language
|
270
|
+
#
|
271
|
+
# document = language.document "Hello world!"
|
272
|
+
#
|
273
|
+
# annotation = language.syntax document
|
274
|
+
# annotation.thing #=> Some Result
|
275
|
+
#
|
276
|
+
def syntax content, format: nil, language: nil, encoding: nil
|
277
|
+
annotate content, syntax: true, format: format, language: language,
|
278
|
+
encoding: encoding
|
279
|
+
end
|
280
|
+
|
281
|
+
##
|
282
|
+
# Entity analysis inspects the given text for known entities (proper
|
283
|
+
# nouns such as public figures, landmarks, etc.) and returns information
|
284
|
+
# about those entities.
|
285
|
+
#
|
286
|
+
# @param [String, Document] content The content to annotate. This
|
287
|
+
# can be an {Document} instance, or any other type that converts to an
|
288
|
+
# {Document}. See {#document} for details.
|
289
|
+
# @param [String] format The format of the document (TEXT/HTML).
|
290
|
+
# Optional.
|
291
|
+
# @param [String] language The language of the document (if not
|
292
|
+
# specified, the language is automatically detected). Both ISO and
|
293
|
+
# BCP-47 language codes are accepted. Optional.
|
294
|
+
# @param [String] encoding The encoding type used by the API to
|
295
|
+
# calculate offsets. Optional.
|
296
|
+
#
|
297
|
+
# @return [Annotation::Entities>] The results for the entities analysis.
|
298
|
+
#
|
299
|
+
# @example
|
300
|
+
# require "google/cloud"
|
301
|
+
#
|
302
|
+
# gcloud = Google::Cloud.new
|
303
|
+
# language = gcloud.language
|
304
|
+
#
|
305
|
+
# document = language.document "Hello Chris and Mike!"
|
306
|
+
#
|
307
|
+
# entities = language.entities document
|
308
|
+
# entities.count #=> 2
|
309
|
+
#
|
310
|
+
def entities content, format: :text, language: nil, encoding: nil
|
311
|
+
ensure_service!
|
312
|
+
doc = document content, language: language, format: format
|
313
|
+
grpc = service.entities doc.to_grpc, encoding: encoding
|
314
|
+
Annotation::Entities.from_grpc grpc
|
315
|
+
end
|
316
|
+
|
317
|
+
##
|
318
|
+
# Sentiment analysis inspects the given text and identifies the
|
319
|
+
# prevailing emotional opinion within the text, especially to determine
|
320
|
+
# a writer's attitude as positive, negative, or neutral. Currently, only
|
321
|
+
# English is supported for sentiment analysis.
|
322
|
+
#
|
323
|
+
# @param [String, Document] content The content to annotate. This
|
324
|
+
# can be an {Document} instance, or any other type that converts to an
|
325
|
+
# {Document}. See {#document} for details.
|
326
|
+
# @param [String] format The format of the document (TEXT/HTML).
|
327
|
+
# Optional.
|
328
|
+
# @param [String] language The language of the document (if not
|
329
|
+
# specified, the language is automatically detected). Both ISO and
|
330
|
+
# BCP-47 language codes are accepted. Optional.
|
331
|
+
#
|
332
|
+
# @return [Annotation::Sentiment>] The results for the sentiment
|
333
|
+
# analysis.
|
334
|
+
#
|
335
|
+
# @example
|
336
|
+
# require "google/cloud"
|
337
|
+
#
|
338
|
+
# gcloud = Google::Cloud.new
|
339
|
+
# language = gcloud.language
|
340
|
+
#
|
341
|
+
# document = language.document "Hello Chris and Mike!"
|
342
|
+
#
|
343
|
+
# sentiment = language.sentiment document
|
344
|
+
# sentiment.polarity #=> 1.0
|
345
|
+
# sentiment.magnitude #=> 0.8999999761581421
|
346
|
+
#
|
347
|
+
def sentiment content, format: :text, language: nil
|
348
|
+
ensure_service!
|
349
|
+
doc = document content, language: language, format: format
|
350
|
+
grpc = service.sentiment doc.to_grpc
|
351
|
+
Annotation::Sentiment.from_grpc grpc
|
352
|
+
end
|
353
|
+
|
354
|
+
protected
|
355
|
+
|
356
|
+
##
|
357
|
+
# @private Raise an error unless an active connection to the service is
|
358
|
+
# available.
|
359
|
+
def ensure_service!
|
360
|
+
fail "Must have active connection to service" unless service
|
361
|
+
end
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|