google-cloud-language 0.27.1 → 0.28.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.
@@ -1,35 +0,0 @@
1
- # Copyright 2017 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 Language
19
- ##
20
- # @private Conversion to/from Language GRPC objects.
21
- module Convert
22
- ##
23
- # @private Convert a Google::Protobuf::Map to a Hash
24
- def self.map_to_hash map
25
- if map.respond_to? :to_h
26
- map.to_h
27
- else
28
- # Enumerable doesn't have to_h on ruby 2.0...
29
- Hash[map.to_a]
30
- end
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,375 +0,0 @@
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
- # Be aware that only English, Spanish, and Japanese language content are
27
- # supported.
28
- #
29
- # See {Project#document}.
30
- #
31
- # @example
32
- # require "google/cloud/language"
33
- #
34
- # language = Google::Cloud::Language.new
35
- #
36
- # content = "Star Wars is a great movie. The Death Star is fearsome."
37
- # document = language.document content
38
- # annotation = document.annotate
39
- #
40
- # annotation.entities.count #=> 3
41
- # annotation.sentiment.score #=> 0.10000000149011612
42
- # annotation.sentiment.magnitude #=> 1.100000023841858
43
- # annotation.sentences.count #=> 2
44
- # annotation.tokens.count #=> 13
45
- #
46
- class Document
47
- ##
48
- # @private The gRPC Service object.
49
- attr_accessor :service
50
-
51
- ##
52
- # @private Creates a new document instance.
53
- def initialize
54
- @grpc = nil
55
- @service = nil
56
- end
57
-
58
- ##
59
- # @private Whether the document has content.
60
- #
61
- def content?
62
- @grpc.source == :content
63
- end
64
-
65
- ##
66
- # @private Whether the document source is a Google Cloud Storage URI.
67
- #
68
- def gcs_url?
69
- @grpc.source == :gcs_content_uri
70
- end
71
-
72
- ##
73
- # @private The source of the document's content.
74
- #
75
- def source
76
- return @grpc.content if content?
77
- @grpc.gcs_content_uri
78
- end
79
-
80
- ##
81
- # The document's format.
82
- #
83
- # @return [Symbol] `:text` or `:html`
84
- #
85
- def format
86
- return :text if text?
87
- return :html if html?
88
- end
89
-
90
- ##
91
- # Sets the document's format.
92
- #
93
- # @param [Symbol, String] new_format Accepted values are `:text` or
94
- # `:html`.
95
- #
96
- # @example
97
- # require "google/cloud/language"
98
- #
99
- # language = Google::Cloud::Language.new
100
- #
101
- # document = language.document "<p>The Old Man and the Sea</p>"
102
- # document.format = :html
103
- #
104
- def format= new_format
105
- @grpc.type = :PLAIN_TEXT if new_format.to_s == "text"
106
- @grpc.type = :HTML if new_format.to_s == "html"
107
- @grpc.type
108
- end
109
-
110
- ##
111
- # Whether the document is the `TEXT` format.
112
- #
113
- # @return [Boolean]
114
- #
115
- def text?
116
- @grpc.type == :PLAIN_TEXT
117
- end
118
-
119
- ##
120
- # Sets the document to the `TEXT` format.
121
- #
122
- def text!
123
- @grpc.type = :PLAIN_TEXT
124
- end
125
-
126
- ##
127
- # Whether the document is the `HTML` format.
128
- #
129
- # @return [Boolean]
130
- #
131
- def html?
132
- @grpc.type == :HTML
133
- end
134
-
135
- ##
136
- # Sets the document to the `HTML` format.
137
- #
138
- def html!
139
- @grpc.type = :HTML
140
- end
141
-
142
- ##
143
- # The document's language. ISO and BCP-47 language codes are supported.
144
- #
145
- # @return [String]
146
- #
147
- def language
148
- @grpc.language
149
- end
150
-
151
- ##
152
- # Sets the document's language.
153
- #
154
- # @param [String, Symbol] new_language ISO and BCP-47 language codes are
155
- # accepted.
156
- #
157
- # @example
158
- # require "google/cloud/language"
159
- #
160
- # language = Google::Cloud::Language.new
161
- #
162
- # document = language.document "<p>El viejo y el mar</p>"
163
- # document.language = "es"
164
- #
165
- def language= new_language
166
- @grpc.language = new_language.to_s
167
- end
168
-
169
- ##
170
- # Analyzes the document and returns sentiment, entity, and syntactic
171
- # feature results, depending on the option flags. Calling `annotate`
172
- # with no arguments will perform **all** analysis features. Each feature
173
- # is priced separately. See [Pricing](https://cloud.google.com/natural-language/pricing)
174
- # for details.
175
- #
176
- # @param [Boolean] sentiment Whether to perform sentiment analysis.
177
- # Optional. The default is `false`. If every feature option is
178
- # `false`, **all** features will be performed.
179
- # @param [Boolean] entities Whether to perform the entity analysis.
180
- # Optional. The default is `false`. If every feature option is
181
- # `false`, **all** features will be performed.
182
- # @param [Boolean] syntax Whether to perform syntactic analysis.
183
- # Optional. The default is `false`. If every feature option is
184
- # `false`, **all** features will be performed.
185
- #
186
- # @return [Annotation] The results of the content analysis.
187
- #
188
- # @example
189
- # require "google/cloud/language"
190
- #
191
- # language = Google::Cloud::Language.new
192
- #
193
- # content = "Star Wars is a great movie. The Death Star is fearsome."
194
- # document = language.document content
195
- # annotation = document.annotate
196
- #
197
- # annotation.sentiment.score #=> 0.10000000149011612
198
- # annotation.sentiment.magnitude #=> 1.100000023841858
199
- # annotation.entities.count #=> 3
200
- # annotation.sentences.count #=> 2
201
- # annotation.tokens.count #=> 13
202
- #
203
- # @example With feature flags:
204
- # require "google/cloud/language"
205
- #
206
- # language = Google::Cloud::Language.new
207
- #
208
- # content = "Star Wars is a great movie. The Death Star is fearsome."
209
- # document = language.document content
210
- # annotation = document.annotate entities: true, syntax: true
211
- #
212
- # annotation.entities.count #=> 3
213
- # annotation.sentences.count #=> 2
214
- # annotation.tokens.count #=> 13
215
- #
216
- def annotate sentiment: false, entities: false, syntax: false
217
- ensure_service!
218
- grpc = service.annotate to_grpc, sentiment: sentiment,
219
- entities: entities,
220
- syntax: syntax
221
- Annotation.from_grpc grpc
222
- end
223
- alias_method :mark, :annotate
224
- alias_method :detect, :annotate
225
-
226
- ##
227
- # Syntactic analysis extracts linguistic information, breaking up the
228
- # given text into a series of sentences and tokens (generally, word
229
- # boundaries), providing further analysis on those tokens.
230
- #
231
- # @return [Annotation::Syntax] The results for the content analysis.
232
- #
233
- # @example
234
- # require "google/cloud/language"
235
- #
236
- # language = Google::Cloud::Language.new
237
- #
238
- # content = "Star Wars is a great movie. The Death Star is fearsome."
239
- # document = language.document content
240
- #
241
- # syntax = document.syntax
242
- #
243
- # sentence = syntax.sentences.last
244
- # sentence.text #=> "The Death Star is fearsome."
245
- # sentence.offset #=> 28
246
- #
247
- # syntax.tokens.count #=> 13
248
- # token = syntax.tokens.first
249
- #
250
- # token.text #=> "Star"
251
- # token.offset #=> 0
252
- # token.part_of_speech.tag #=> :NOUN
253
- # token.head_token_index #=> 1
254
- # token.label #=> :TITLE
255
- # token.lemma #=> "Star"
256
- #
257
- def syntax
258
- ensure_service!
259
- grpc = service.syntax to_grpc
260
- Annotation::Syntax.from_grpc grpc
261
- end
262
-
263
- ##
264
- # Entity analysis inspects the given text for known entities (proper
265
- # nouns such as public figures, landmarks, etc.) and returns information
266
- # about those entities.
267
- #
268
- # @return [Annotation::Entities] The results for the entities analysis.
269
- #
270
- # @example
271
- # require "google/cloud/language"
272
- #
273
- # language = Google::Cloud::Language.new
274
- #
275
- # content = "Star Wars is a great movie. The Death Star is fearsome."
276
- # document = language.document content
277
- # entities = document.entities # API call
278
- #
279
- # entities.count #=> 3
280
- # entities.first.name #=> "Star Wars"
281
- # entities.first.type #=> :WORK_OF_ART
282
- # entities.first.mid #=> "/m/06mmr"
283
- #
284
- def entities
285
- ensure_service!
286
- grpc = service.entities to_grpc
287
- Annotation::Entities.from_grpc grpc
288
- end
289
-
290
- ##
291
- # Sentiment analysis inspects the given text and identifies the
292
- # prevailing emotional opinion within the text, especially to determine
293
- # a writer's attitude as positive, negative, or neutral. Currently, only
294
- # English is supported for sentiment analysis.
295
- #
296
- # @return [Annotation::Sentiment] The results for the sentiment
297
- # analysis.
298
- #
299
- # @example
300
- # require "google/cloud/language"
301
- #
302
- # language = Google::Cloud::Language.new
303
- #
304
- # content = "Star Wars is a great movie. The Death Star is fearsome."
305
- # document = language.document content
306
- #
307
- # sentiment = document.sentiment
308
- #
309
- # sentiment.score #=> 0.10000000149011612
310
- # sentiment.magnitude #=> 1.100000023841858
311
- # sentiment.language #=> "en"
312
- #
313
- # sentence = sentiment.sentences.first
314
- # sentence.sentiment.score #=> 0.699999988079071
315
- # sentence.sentiment.magnitude #=> 0.699999988079071
316
- #
317
- def sentiment
318
- ensure_service!
319
- grpc = service.sentiment to_grpc
320
- Annotation::Sentiment.from_grpc grpc
321
- end
322
-
323
- # @private
324
- def inspect
325
- "#<#{self.class.name} (" \
326
- "#{(content? ? "\"#{source[0, 16]}...\"" : source)}, " \
327
- "format: #{format.inspect}, language: #{language.inspect})>"
328
- end
329
-
330
- ##
331
- # @private New gRPC object.
332
- def to_grpc
333
- @grpc
334
- end
335
-
336
- ##
337
- # @private
338
- def self.from_grpc grpc, service
339
- new.tap do |i|
340
- i.instance_variable_set :@grpc, grpc
341
- i.instance_variable_set :@service, service
342
- end
343
- end
344
-
345
- ##
346
- # @private
347
- def self.from_source source, service, format: nil, language: nil
348
- source = String source
349
- grpc = Google::Cloud::Language::V1::Document.new
350
- if source.start_with? "gs://"
351
- grpc.gcs_content_uri = source
352
- format ||= :html if source.end_with? ".html"
353
- else
354
- grpc.content = source
355
- end
356
- if format.to_s == "html"
357
- grpc.type = :HTML
358
- else
359
- grpc.type = :PLAIN_TEXT
360
- end
361
- grpc.language = language.to_s unless language.nil?
362
- from_grpc grpc, service
363
- end
364
-
365
- protected
366
-
367
- ##
368
- # Raise an error unless an active language project object is available.
369
- def ensure_service!
370
- fail "Must have active connection" unless service
371
- end
372
- end
373
- end
374
- end
375
- end
@@ -1,376 +0,0 @@
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/env"
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/language"
37
- #
38
- # language = Google::Cloud::Language.new
39
- #
40
- # content = "Star Wars is a great movie. The Death Star is fearsome."
41
- # annotation = language.annotate content
42
- #
43
- # annotation.sentiment.score #=> 0.10000000149011612
44
- # annotation.sentiment.magnitude #=> 1.100000023841858
45
- # annotation.entities.count #=> 3
46
- # annotation.sentences.count #=> 2
47
- # annotation.tokens.count #=> 13
48
- #
49
- class Project
50
- ##
51
- # @private The gRPC Service object.
52
- attr_accessor :service
53
-
54
- ##
55
- # @private Creates a new Language Project instance.
56
- def initialize service
57
- @service = service
58
- end
59
-
60
- # The Language project connected to.
61
- #
62
- # @example
63
- # require "google/cloud/language"
64
- #
65
- # language = Google::Cloud::Language.new(
66
- # project: "my-project-id",
67
- # keyfile: "/path/to/keyfile.json"
68
- # )
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.env.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, Symbol] format The format of the document. Acceptable
93
- # values are: `text` or `html`. If no format is provided, the document
94
- # will default to `text`. Optional.
95
- # @param [String, Symbol] language The language of the document (if not
96
- # specified, the language is automatically detected). Both ISO and
97
- # BCP-47 language codes are accepted. Optional.
98
- #
99
- # @return [Document] An document for the Language service.
100
- #
101
- # @example
102
- # require "google/cloud/language"
103
- #
104
- # language = Google::Cloud::Language.new
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/language"
110
- #
111
- # language = Google::Cloud::Language.new
112
- #
113
- # document = language.document "gs://bucket-name/path/to/document"
114
- #
115
- # @example With a Google Cloud Storage File object:
116
- # require "google/cloud/storage"
117
- # storage = Google::Cloud::Storage.new
118
- #
119
- # bucket = storage.bucket "bucket-name"
120
- # file = bucket.file "path/to/document"
121
- #
122
- # require "google/cloud/language"
123
- # language = Google::Cloud::Language.new
124
- #
125
- # document = language.document file
126
- #
127
- # @example With `format` and `language` options:
128
- # require "google/cloud/language"
129
- #
130
- # language = Google::Cloud::Language.new
131
- #
132
- # document = language.document "<p>El viejo y el mar</p>",
133
- # format: :html, language: "es"
134
- #
135
- def document content, format: nil, language: nil
136
- content = content.to_gs_url if content.respond_to? :to_gs_url
137
- if content.is_a? Document
138
- # Create new document with the provided format and language
139
- Document.from_source content.source, @service,
140
- format: (format || content.format),
141
- language: (language || content.language)
142
- else
143
- Document.from_source content, @service, format: format,
144
- language: language
145
- end
146
- end
147
- alias_method :doc, :document
148
-
149
- ##
150
- # Returns a new document from the given content with the `format` value
151
- # `:text`. No API call is made.
152
- #
153
- # @param [String, Google::Cloud::Storage::File] content A string of text
154
- # to be annotated, or a Cloud Storage URI of the form
155
- # `"gs://bucketname/path/to/document.ext"`; or an instance of
156
- # Google::Cloud::Storage::File of the text to be annotated.
157
- # @param [String, Symbol] language The language of the document (if not
158
- # specified, the language is automatically detected). Both ISO and
159
- # BCP-47 language codes are accepted. Optional.
160
- #
161
- # @return [Document] An document for the Language service.
162
- #
163
- def text content, language: nil
164
- document content, format: :text, language: language
165
- end
166
-
167
- ##
168
- # Returns a new document from the given content with the `format` value
169
- # `:html`. No API call is made.
170
- #
171
- # @param [String, Google::Cloud::Storage::File] content A string of text
172
- # to be annotated, or a Cloud Storage URI of the form
173
- # `"gs://bucketname/path/to/document.ext"`; or an instance of
174
- # Google::Cloud::Storage::File of the text to be annotated.
175
- # @param [String, Symbol] language The language of the document (if not
176
- # specified, the language is automatically detected). Both ISO and
177
- # BCP-47 language codes are accepted. Optional.
178
- #
179
- # @return [Document] An document for the Language service.
180
- #
181
- def html content, language: nil
182
- document content, format: :html, language: language
183
- end
184
-
185
- ##
186
- # Analyzes the content and returns sentiment, entity, and syntactic
187
- # feature results, depending on the option flags. Calling `annotate`
188
- # with no arguments will perform **all** analysis features. Each feature
189
- # is priced separately. See [Pricing](https://cloud.google.com/natural-language/pricing)
190
- # for details.
191
- #
192
- # @param [String, Document, Google::Cloud::Storage::File] content The
193
- # content to annotate. This can be an {Document} instance, or any
194
- # other type that converts to an {Document}. See {#document} for
195
- # details.
196
- # @param [Boolean] sentiment Whether to perform the sentiment analysis.
197
- # Optional. The default is `false`. If every feature option is
198
- # `false`, **all** features will be performed.
199
- # @param [Boolean] entities Whether to perform the entity analysis.
200
- # Optional. The default is `false`. If every feature option is
201
- # `false`, **all** features will be performed.
202
- # @param [Boolean] syntax Whether to perform the syntactic analysis.
203
- # Optional. The default is `false`. If every feature option is
204
- # `false`, **all** features will be performed.
205
- # @param [String, Symbol] format The format of the document. Acceptable
206
- # values are: `text` or `html`. Optional.
207
- # @param [String, Symbol] language The language of the document (if not
208
- # specified, the language is automatically detected). Both ISO and
209
- # BCP-47 language codes are accepted. Optional.
210
- #
211
- # @return [Annotation] The results for the content analysis.
212
- #
213
- # @example
214
- # require "google/cloud/language"
215
- #
216
- # language = Google::Cloud::Language.new
217
- #
218
- # content = "Star Wars is a great movie. The Death Star is fearsome."
219
- # annotation = language.annotate content
220
- #
221
- # annotation.sentiment.score #=> 0.10000000149011612
222
- # annotation.sentiment.magnitude #=> 1.100000023841858
223
- # annotation.entities.count #=> 3
224
- # annotation.sentences.count #=> 2
225
- # annotation.tokens.count #=> 13
226
- #
227
- def annotate content, sentiment: false, entities: false, syntax: false,
228
- format: nil, language: nil
229
- ensure_service!
230
- doc = document content, language: language, format: format
231
- grpc = service.annotate doc.to_grpc, sentiment: sentiment,
232
- entities: entities,
233
- syntax: syntax
234
- Annotation.from_grpc grpc
235
- end
236
- alias_method :mark, :annotate
237
- alias_method :detect, :annotate
238
-
239
- ##
240
- # Syntactic analysis extracts linguistic information, breaking up the
241
- # given text into a series of sentences and tokens (generally, word
242
- # boundaries), providing further analysis on those tokens.
243
- #
244
- # @param [String, Document, Google::Cloud::Storage::File] content The
245
- # content to annotate. This can be an {Document} instance, or any
246
- # other type that converts to an {Document}. See {#document} for
247
- # details.
248
- # @param [String, Symbol] format The format of the document. Acceptable
249
- # values are: `text` or `html`. Optional.
250
- # @param [String, Symbol] language The language of the document (if not
251
- # specified, the language is automatically detected). Both ISO and
252
- # BCP-47 language codes are accepted. Optional.
253
- #
254
- # @return [Annotation::Syntax] The results for the content syntax
255
- # analysis.
256
- #
257
- # @example
258
- # require "google/cloud/language"
259
- #
260
- # language = Google::Cloud::Language.new
261
- #
262
- # content = "Star Wars is a great movie. The Death Star is fearsome."
263
- # document = language.document content
264
- #
265
- # syntax = language.syntax document
266
- #
267
- # sentence = syntax.sentences.last
268
- # sentence.text #=> "The Death Star is fearsome."
269
- # sentence.offset #=> 28
270
- #
271
- # syntax.tokens.count #=> 13
272
- # token = syntax.tokens.first
273
- #
274
- # token.text #=> "Star"
275
- # token.offset #=> 0
276
- # token.part_of_speech.tag #=> :NOUN
277
- # token.head_token_index #=> 1
278
- # token.label #=> :TITLE
279
- # token.lemma #=> "Star"
280
- #
281
- def syntax content, format: nil, language: nil
282
- ensure_service!
283
- doc = document content, language: language, format: format
284
- grpc = service.syntax doc.to_grpc
285
- Annotation::Syntax.from_grpc grpc
286
- end
287
-
288
- ##
289
- # Entity analysis inspects the given text for known entities (proper
290
- # nouns such as public figures, landmarks, etc.) and returns information
291
- # about those entities.
292
- #
293
- # @param [String, Document] content The content to annotate. This
294
- # can be an {Document} instance, or any other type that converts to an
295
- # {Document}. See {#document} for details.
296
- # @param [String, Symbol] format The format of the document. Acceptable
297
- # values are: `text` or `html`. Optional.
298
- # @param [String, Symbol] language The language of the document (if not
299
- # specified, the language is automatically detected). Both ISO and
300
- # BCP-47 language codes are accepted. Optional.
301
- #
302
- # @return [Annotation::Entities] The results for the entities analysis.
303
- #
304
- # @example
305
- # require "google/cloud/language"
306
- #
307
- # language = Google::Cloud::Language.new
308
- #
309
- # content = "Star Wars is a great movie. The Death Star is fearsome."
310
- # document = language.document content
311
- #
312
- # entities = language.entities document
313
- # entities.count #=> 3
314
- #
315
- def entities content, format: :text, language: nil
316
- ensure_service!
317
- doc = document content, language: language, format: format
318
- grpc = service.entities doc.to_grpc
319
- Annotation::Entities.from_grpc grpc
320
- end
321
-
322
- ##
323
- # Sentiment analysis inspects the given text and identifies the
324
- # prevailing emotional opinion within the text, especially to determine
325
- # a writer's attitude as positive, negative, or neutral. Currently, only
326
- # English is supported for sentiment analysis.
327
- #
328
- # @param [String, Document] content The content to annotate. This
329
- # can be an {Document} instance, or any other type that converts to an
330
- # {Document}. See {#document} for details.
331
- # @param [String, Symbol] format The format of the document. Acceptable
332
- # values are: `text` or `html`. Optional.
333
- # @param [String, Symbol] language The language of the document (if not
334
- # specified, the language is automatically detected). Both ISO and
335
- # BCP-47 language codes are accepted. Optional.
336
- #
337
- # @return [Annotation::Sentiment] The results for the sentiment
338
- # analysis.
339
- #
340
- # @example
341
- # require "google/cloud/language"
342
- #
343
- # language = Google::Cloud::Language.new
344
- #
345
- # content = "Star Wars is a great movie. The Death Star is fearsome."
346
- # document = language.document content
347
- #
348
- # sentiment = language.sentiment document
349
- #
350
- # sentiment.score #=> 0.10000000149011612
351
- # sentiment.magnitude #=> 1.100000023841858
352
- # sentiment.language #=> "en"
353
- #
354
- # sentence = sentiment.sentences.first
355
- # sentence.sentiment.score #=> 0.699999988079071
356
- # sentence.sentiment.magnitude #=> 0.699999988079071
357
- #
358
- def sentiment content, format: :text, language: nil
359
- ensure_service!
360
- doc = document content, language: language, format: format
361
- grpc = service.sentiment doc.to_grpc
362
- Annotation::Sentiment.from_grpc grpc
363
- end
364
-
365
- protected
366
-
367
- ##
368
- # @private Raise an error unless an active connection to the service is
369
- # available.
370
- def ensure_service!
371
- fail "Must have active connection to service" unless service
372
- end
373
- end
374
- end
375
- end
376
- end