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.
- checksums.yaml +5 -5
- data/.yardopts +2 -6
- data/LICENSE +1 -1
- data/README.md +35 -59
- data/lib/google/cloud/language.rb +81 -230
- data/lib/google/cloud/language/credentials.rb +9 -11
- data/lib/google/cloud/language/v1.rb +107 -0
- data/lib/google/cloud/language/v1/doc/google/cloud/language/v1/language_service.rb +108 -18
- data/lib/google/cloud/language/v1/doc/overview.rb +67 -0
- data/lib/google/cloud/language/v1/language_service_client.rb +157 -67
- data/lib/google/cloud/language/v1/language_service_client_config.json +10 -0
- data/lib/google/cloud/language/v1/language_service_pb.rb +34 -0
- data/lib/google/cloud/language/v1/language_service_services_pb.rb +5 -0
- data/lib/google/cloud/language/v1beta2.rb +107 -0
- data/lib/google/cloud/language/v1beta2/doc/google/cloud/language/v1beta2/language_service.rb +29 -0
- data/lib/google/cloud/language/v1beta2/doc/overview.rb +67 -0
- data/lib/google/cloud/language/v1beta2/language_service_client.rb +5 -6
- data/lib/google/cloud/language/v1beta2/language_service_pb.rb +6 -0
- metadata +24 -100
- data/lib/google-cloud-language.rb +0 -112
- data/lib/google/cloud/language/annotation.rb +0 -1084
- data/lib/google/cloud/language/convert.rb +0 -35
- data/lib/google/cloud/language/document.rb +0 -375
- data/lib/google/cloud/language/project.rb +0 -376
- data/lib/google/cloud/language/service.rb +0 -143
- data/lib/google/cloud/language/version.rb +0 -22
@@ -1,112 +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
|
-
# This file is here to be autorequired by bundler, so that the .language and
|
17
|
-
# #language 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 Language service.
|
28
|
-
# Each call creates a new connection.
|
29
|
-
#
|
30
|
-
# For more information on connecting to Google Cloud see the [Authentication
|
31
|
-
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
32
|
-
#
|
33
|
-
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
34
|
-
# set of resources and operations that the connection can access. See
|
35
|
-
# [Using OAuth 2.0 to Access Google
|
36
|
-
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
37
|
-
#
|
38
|
-
# The default scope is:
|
39
|
-
#
|
40
|
-
# * `"https://www.googleapis.com/auth/cloud-platform"`
|
41
|
-
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
42
|
-
# @param [Hash] client_config A hash of values to override the default
|
43
|
-
# behavior of the API client. Optional.
|
44
|
-
#
|
45
|
-
# @return [Google::Cloud::Language::Project]
|
46
|
-
#
|
47
|
-
# @example
|
48
|
-
# require "google/cloud"
|
49
|
-
#
|
50
|
-
# gcloud = Google::Cloud.new
|
51
|
-
# language = gcloud.language
|
52
|
-
#
|
53
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
54
|
-
# document = language.document content
|
55
|
-
# annotation = document.annotate
|
56
|
-
#
|
57
|
-
# @example The default scope can be overridden with the `scope` option:
|
58
|
-
# require "google/cloud"
|
59
|
-
#
|
60
|
-
# gcloud = Google::Cloud.new
|
61
|
-
# platform_scope = "https://www.googleapis.com/auth/cloud-platform"
|
62
|
-
# language = gcloud.language scope: platform_scope
|
63
|
-
#
|
64
|
-
def language scope: nil, timeout: nil, client_config: nil
|
65
|
-
Google::Cloud.language @project, @keyfile,
|
66
|
-
scope: scope, timeout: (timeout || @timeout),
|
67
|
-
client_config: client_config
|
68
|
-
end
|
69
|
-
|
70
|
-
##
|
71
|
-
# Creates a new object for connecting to the Language service.
|
72
|
-
# Each call creates a new connection.
|
73
|
-
#
|
74
|
-
# For more information on connecting to Google Cloud see the [Authentication
|
75
|
-
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
76
|
-
#
|
77
|
-
# @param [String] project Project identifier for the Language service you
|
78
|
-
# are connecting to.
|
79
|
-
# @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
|
80
|
-
# file path the file must be readable.
|
81
|
-
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
82
|
-
# set of resources and operations that the connection can access. See
|
83
|
-
# [Using OAuth 2.0 to Access Google
|
84
|
-
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
85
|
-
#
|
86
|
-
# The default scope is:
|
87
|
-
#
|
88
|
-
# * `"https://www.googleapis.com/auth/cloud-platform"`
|
89
|
-
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
90
|
-
# @param [Hash] client_config A hash of values to override the default
|
91
|
-
# behavior of the API client. Optional.
|
92
|
-
#
|
93
|
-
# @return [Google::Cloud::Language::Project]
|
94
|
-
#
|
95
|
-
# @example
|
96
|
-
# require "google/cloud"
|
97
|
-
#
|
98
|
-
# language = Google::Cloud.language
|
99
|
-
#
|
100
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
101
|
-
# document = language.document content
|
102
|
-
# annotation = document.annotate
|
103
|
-
#
|
104
|
-
def self.language project = nil, keyfile = nil, scope: nil, timeout: nil,
|
105
|
-
client_config: nil
|
106
|
-
require "google/cloud/language"
|
107
|
-
Google::Cloud::Language.new project: project, keyfile: keyfile,
|
108
|
-
scope: scope, timeout: timeout,
|
109
|
-
client_config: client_config
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
@@ -1,1084 +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 "delegate"
|
17
|
-
require "google/cloud/language/convert"
|
18
|
-
|
19
|
-
module Google
|
20
|
-
module Cloud
|
21
|
-
module Language
|
22
|
-
##
|
23
|
-
# # Annotation
|
24
|
-
#
|
25
|
-
# The results of all requested document analysis features.
|
26
|
-
#
|
27
|
-
# See {Project#annotate} and {Document#annotate}.
|
28
|
-
#
|
29
|
-
# @example
|
30
|
-
# require "google/cloud/language"
|
31
|
-
#
|
32
|
-
# language = Google::Cloud::Language.new
|
33
|
-
#
|
34
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
35
|
-
# document = language.document content
|
36
|
-
# annotation = document.annotate
|
37
|
-
#
|
38
|
-
# annotation.sentiment.score #=> 0.10000000149011612
|
39
|
-
# annotation.sentiment.magnitude #=> 1.100000023841858
|
40
|
-
# annotation.entities.count #=> 3
|
41
|
-
# annotation.sentences.count #=> 2
|
42
|
-
# annotation.tokens.count #=> 13
|
43
|
-
#
|
44
|
-
class Annotation
|
45
|
-
##
|
46
|
-
# @private The AnnotateTextResponse Google API Client object.
|
47
|
-
attr_accessor :grpc
|
48
|
-
|
49
|
-
##
|
50
|
-
# @private Creates a new Annotation instance.
|
51
|
-
def initialize
|
52
|
-
@grpc = nil
|
53
|
-
end
|
54
|
-
|
55
|
-
##
|
56
|
-
# The sentences returned by syntactic analysis.
|
57
|
-
#
|
58
|
-
# @return [Array<TextSpan>] an array of pieces of text including
|
59
|
-
# relative location
|
60
|
-
#
|
61
|
-
# @example
|
62
|
-
# require "google/cloud/language"
|
63
|
-
#
|
64
|
-
# language = Google::Cloud::Language.new
|
65
|
-
#
|
66
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
67
|
-
# document = language.document content
|
68
|
-
# annotation = document.annotate
|
69
|
-
#
|
70
|
-
# sentence = annotation.sentences.last
|
71
|
-
# sentence.text #=> "The Death Star is fearsome."
|
72
|
-
# sentence.offset #=> 28
|
73
|
-
#
|
74
|
-
def sentences
|
75
|
-
@sentences ||= Array(grpc.sentences).map { |g| Sentence.from_grpc g }
|
76
|
-
end
|
77
|
-
|
78
|
-
##
|
79
|
-
# The tokens returned by syntactic analysis.
|
80
|
-
#
|
81
|
-
# @return [Array<Token>] an array of the smallest syntactic building
|
82
|
-
# blocks of the text
|
83
|
-
#
|
84
|
-
# @example
|
85
|
-
# require "google/cloud/language"
|
86
|
-
#
|
87
|
-
# language = Google::Cloud::Language.new
|
88
|
-
#
|
89
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
90
|
-
# document = language.document content
|
91
|
-
# annotation = document.annotate
|
92
|
-
#
|
93
|
-
# annotation.tokens.count #=> 13
|
94
|
-
# token = annotation.tokens.first
|
95
|
-
#
|
96
|
-
# token.text #=> "Star"
|
97
|
-
# token.offset #=> 0
|
98
|
-
# token.part_of_speech.tag #=> :NOUN
|
99
|
-
# token.head_token_index #=> 1
|
100
|
-
# token.label #=> :TITLE
|
101
|
-
# token.lemma #=> "Star"
|
102
|
-
#
|
103
|
-
def tokens
|
104
|
-
@tokens ||= Array(grpc.tokens).map { |g| Token.from_grpc g }
|
105
|
-
end
|
106
|
-
|
107
|
-
##
|
108
|
-
# The result of syntax analysis.
|
109
|
-
#
|
110
|
-
# @return [Syntax]
|
111
|
-
#
|
112
|
-
# @example
|
113
|
-
# require "google/cloud/language"
|
114
|
-
#
|
115
|
-
# language = Google::Cloud::Language.new
|
116
|
-
#
|
117
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
118
|
-
# document = language.document content
|
119
|
-
# annotation = document.annotate
|
120
|
-
# syntax = annotation.syntax
|
121
|
-
#
|
122
|
-
# sentence = syntax.sentences.last
|
123
|
-
# sentence.text #=> "The Death Star is fearsome."
|
124
|
-
# sentence.offset #=> 28
|
125
|
-
#
|
126
|
-
# syntax.tokens.count #=> 13
|
127
|
-
# token = syntax.tokens.first
|
128
|
-
#
|
129
|
-
# token.text #=> "Star"
|
130
|
-
# token.offset #=> 0
|
131
|
-
# token.part_of_speech.tag #=> :NOUN
|
132
|
-
# token.head_token_index #=> 1
|
133
|
-
# token.label #=> :TITLE
|
134
|
-
# token.lemma #=> "Star"
|
135
|
-
#
|
136
|
-
def syntax
|
137
|
-
return nil if @grpc.tokens.nil?
|
138
|
-
@syntax ||= Syntax.from_grpc @grpc
|
139
|
-
end
|
140
|
-
|
141
|
-
##
|
142
|
-
# The entities returned by entity analysis.
|
143
|
-
#
|
144
|
-
# @return [Entities]
|
145
|
-
#
|
146
|
-
# @example
|
147
|
-
# require "google/cloud/language"
|
148
|
-
#
|
149
|
-
# language = Google::Cloud::Language.new
|
150
|
-
#
|
151
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
152
|
-
# document = language.document content
|
153
|
-
# annotation = document.annotate
|
154
|
-
#
|
155
|
-
# entities = annotation.entities
|
156
|
-
# entities.count #=> 3
|
157
|
-
# entity = entities.first
|
158
|
-
#
|
159
|
-
# entity.name #=> "Star Wars"
|
160
|
-
# entity.type #=> :WORK_OF_ART
|
161
|
-
# entity.salience #=> 0.6457656025886536
|
162
|
-
# entity.mentions.count #=> 1
|
163
|
-
# entity.mentions.first.text # => "Star Wars"
|
164
|
-
# entity.mentions.first.offset # => 0
|
165
|
-
# entity.mid #=> "/m/06mmr"
|
166
|
-
# entity.wikipedia_url #=> "http://en.wikipedia.org/wiki/Star_Wars"
|
167
|
-
#
|
168
|
-
def entities
|
169
|
-
@entities ||= Entities.from_grpc @grpc
|
170
|
-
end
|
171
|
-
|
172
|
-
##
|
173
|
-
# The result of sentiment analysis.
|
174
|
-
#
|
175
|
-
# @return [Sentiment]
|
176
|
-
#
|
177
|
-
# @example
|
178
|
-
# require "google/cloud/language"
|
179
|
-
#
|
180
|
-
# language = Google::Cloud::Language.new
|
181
|
-
#
|
182
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
183
|
-
# document = language.document content
|
184
|
-
# annotation = document.annotate
|
185
|
-
# sentiment = annotation.sentiment
|
186
|
-
#
|
187
|
-
# sentiment.score #=> 0.10000000149011612
|
188
|
-
# sentiment.magnitude #=> 1.100000023841858
|
189
|
-
# sentiment.language #=> "en"
|
190
|
-
#
|
191
|
-
# sentence = sentiment.sentences.first
|
192
|
-
# sentence.sentiment.score #=> 0.699999988079071
|
193
|
-
# sentence.sentiment.magnitude #=> 0.699999988079071
|
194
|
-
#
|
195
|
-
def sentiment
|
196
|
-
return nil if @grpc.document_sentiment.nil?
|
197
|
-
@sentiment ||= Sentiment.from_grpc @grpc
|
198
|
-
end
|
199
|
-
|
200
|
-
##
|
201
|
-
# The language of the document (if not specified, the language is
|
202
|
-
# automatically detected). Both ISO and BCP-47 language codes are
|
203
|
-
# supported.
|
204
|
-
#
|
205
|
-
# @return [String] the language code
|
206
|
-
#
|
207
|
-
# @example
|
208
|
-
# require "google/cloud/language"
|
209
|
-
#
|
210
|
-
# language = Google::Cloud::Language.new
|
211
|
-
#
|
212
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
213
|
-
# document = language.document content
|
214
|
-
# annotation = document.annotate
|
215
|
-
# annotation.language #=> "en"
|
216
|
-
#
|
217
|
-
def language
|
218
|
-
@grpc.language
|
219
|
-
end
|
220
|
-
|
221
|
-
# @private
|
222
|
-
def to_s
|
223
|
-
tmplt = "(sentences: %i, tokens: %i, entities: %i," \
|
224
|
-
" sentiment: %s, language: %s)"
|
225
|
-
format tmplt, sentences.count, tokens.count, entities.count,
|
226
|
-
!sentiment.nil?, language.inspect
|
227
|
-
end
|
228
|
-
|
229
|
-
# @private
|
230
|
-
def inspect
|
231
|
-
"#<#{self.class.name} #{self}>"
|
232
|
-
end
|
233
|
-
|
234
|
-
##
|
235
|
-
# @private New Annotation from a V1::AnnotateTextResponse object.
|
236
|
-
def self.from_grpc grpc
|
237
|
-
new.tap { |a| a.instance_variable_set :@grpc, grpc }
|
238
|
-
end
|
239
|
-
|
240
|
-
##
|
241
|
-
# Represents a piece of text including relative location.
|
242
|
-
#
|
243
|
-
# @attr_reader [String] text The content of the output text.
|
244
|
-
# @attr_reader [Integer] offset The beginning offset
|
245
|
-
# of the content in the original document.
|
246
|
-
#
|
247
|
-
# The API calculates the beginning offset according to the client
|
248
|
-
# system's default encoding. In Ruby this defaults to UTF-8. To change
|
249
|
-
# the offset calculation you will need to change Ruby's default
|
250
|
-
# encoding. This is commonly done by setting
|
251
|
-
# `Encoding.default_internal` to `Encoding::UTF_16` or
|
252
|
-
# `Encoding::UTF_32`. If the system is configured to use an encoding
|
253
|
-
# other than UTF-16 or UTF-32 the offset will be calculated using
|
254
|
-
# UTF-8.
|
255
|
-
#
|
256
|
-
# @example
|
257
|
-
# require "google/cloud/language"
|
258
|
-
#
|
259
|
-
# language = Google::Cloud::Language.new
|
260
|
-
#
|
261
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
262
|
-
# document = language.document content
|
263
|
-
# annotation = document.annotate
|
264
|
-
#
|
265
|
-
# sentence = annotation.sentences.last
|
266
|
-
# text_span = sentence.text_span
|
267
|
-
# sentence.text #=> "The Death Star is fearsome."
|
268
|
-
# sentence.offset #=> 28
|
269
|
-
#
|
270
|
-
class TextSpan
|
271
|
-
attr_reader :text, :offset
|
272
|
-
alias_method :content, :text
|
273
|
-
alias_method :begin_offset, :offset
|
274
|
-
|
275
|
-
##
|
276
|
-
# @private Creates a new Token instance.
|
277
|
-
def initialize text, offset
|
278
|
-
@text = text
|
279
|
-
@offset = offset
|
280
|
-
end
|
281
|
-
|
282
|
-
##
|
283
|
-
# @private New TextSpan from a V1::TextSpan object.
|
284
|
-
def self.from_grpc grpc
|
285
|
-
new grpc.content, grpc.begin_offset
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
##
|
290
|
-
# Provides grammatical information, including morphological information,
|
291
|
-
# about a token, such as the token's tense, person, number, gender,
|
292
|
-
# and so on. Only some of these attributes will be applicable to any
|
293
|
-
# given part of speech. Parts of speech are as defined in [A Universal
|
294
|
-
# Part-of-Speech Tagset]http://www.lrec-conf.org/proceedings/lrec2012/pdf/274_Paper.pdf
|
295
|
-
#
|
296
|
-
# @attr_reader [Symbol] tag The part of speech tag.
|
297
|
-
# @attr_reader [Symbol] aspect The grammatical aspect.
|
298
|
-
# @attr_reader [Symbol] case The grammatical case.
|
299
|
-
# @attr_reader [Symbol] form The grammatical form.
|
300
|
-
# @attr_reader [Symbol] gender The grammatical gender.
|
301
|
-
# @attr_reader [Symbol] mood The grammatical mood.
|
302
|
-
# @attr_reader [Symbol] number The grammatical number.
|
303
|
-
# @attr_reader [Symbol] person The grammatical person.
|
304
|
-
# @attr_reader [Symbol] proper The grammatical properness.
|
305
|
-
# @attr_reader [Symbol] reciprocity The grammatical reciprocity.
|
306
|
-
# @attr_reader [Symbol] tense The grammatical tense.
|
307
|
-
# @attr_reader [Symbol] voice The grammatical voice.
|
308
|
-
#
|
309
|
-
# @example
|
310
|
-
# require "google/cloud/language"
|
311
|
-
#
|
312
|
-
# language = Google::Cloud::Language.new
|
313
|
-
#
|
314
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
315
|
-
# document = language.document content
|
316
|
-
# annotation = document.annotate
|
317
|
-
#
|
318
|
-
# annotation.tokens.count #=> 13
|
319
|
-
# token = annotation.tokens.first
|
320
|
-
#
|
321
|
-
# token.text_span.text #=> "Star"
|
322
|
-
# token.part_of_speech.tag #=> :NOUN
|
323
|
-
# token.part_of_speech.number #=> :SINGULAR
|
324
|
-
#
|
325
|
-
class PartOfSpeech
|
326
|
-
attr_reader :tag, :aspect, :case, :form, :gender, :mood, :number,
|
327
|
-
:person, :proper, :reciprocity, :tense, :voice
|
328
|
-
|
329
|
-
##
|
330
|
-
# @private Creates a new PartOfSpeech instance.
|
331
|
-
def initialize tag, aspect, kase, form, gender, mood, number, person,
|
332
|
-
proper, reciprocity, tense, voice
|
333
|
-
@tag = tag
|
334
|
-
@aspect = aspect
|
335
|
-
@case = kase
|
336
|
-
@form = form
|
337
|
-
@gender = gender
|
338
|
-
@mood = mood
|
339
|
-
@number = number
|
340
|
-
@person = person
|
341
|
-
@proper = proper
|
342
|
-
@reciprocity = reciprocity
|
343
|
-
@tense = tense
|
344
|
-
@voice = voice
|
345
|
-
end
|
346
|
-
|
347
|
-
##
|
348
|
-
# @private New TextSpan from a V1::PartOfSpeech object.
|
349
|
-
def self.from_grpc grpc
|
350
|
-
new grpc.tag, grpc.aspect, grpc.case, grpc.form, grpc.gender,
|
351
|
-
grpc.mood, grpc.number, grpc.person, grpc.proper,
|
352
|
-
grpc.reciprocity, grpc.tense, grpc.voice
|
353
|
-
end
|
354
|
-
end
|
355
|
-
|
356
|
-
# Represents a piece of text including relative location.
|
357
|
-
#
|
358
|
-
# @attr_reader [TextSpan] text_span The sentence text.
|
359
|
-
# @attr_reader [Sentence::Sentiment] sentiment The sentence sentiment.
|
360
|
-
#
|
361
|
-
# @example
|
362
|
-
# require "google/cloud/language"
|
363
|
-
#
|
364
|
-
# language = Google::Cloud::Language.new
|
365
|
-
#
|
366
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
367
|
-
# document = language.document content
|
368
|
-
# annotation = document.annotate
|
369
|
-
#
|
370
|
-
# annotation.sentences.count #=> 2
|
371
|
-
# sentence = annotation.sentences.first
|
372
|
-
# sentence.text #=> "Star Wars is a great movie."
|
373
|
-
# sentence.offset #=> 0
|
374
|
-
#
|
375
|
-
class Sentence
|
376
|
-
attr_reader :text_span, :sentiment
|
377
|
-
|
378
|
-
##
|
379
|
-
# @private Creates a new Sentence instance.
|
380
|
-
def initialize text_span, sentiment
|
381
|
-
@text_span = text_span
|
382
|
-
@sentiment = sentiment
|
383
|
-
end
|
384
|
-
|
385
|
-
##
|
386
|
-
# The content of the output text. See {TextSpan#text}.
|
387
|
-
#
|
388
|
-
# @return [String]
|
389
|
-
#
|
390
|
-
def text
|
391
|
-
text_span.text
|
392
|
-
end
|
393
|
-
alias_method :content, :text
|
394
|
-
|
395
|
-
##
|
396
|
-
# The beginning offset of the content in the original document. See
|
397
|
-
# {TextSpan#offset}.
|
398
|
-
#
|
399
|
-
# The API calculates the beginning offset according to the client
|
400
|
-
# system's default encoding. In Ruby this defaults to UTF-8. To change
|
401
|
-
# the offset calculation you will need to change Ruby's default
|
402
|
-
# encoding. This is commonly done by setting
|
403
|
-
# `Encoding.default_internal` to `Encoding::UTF_16` or
|
404
|
-
# `Encoding::UTF_32`. If the system is configured to use an encoding
|
405
|
-
# other than UTF-16 or UTF-32 the offset will be calculated using
|
406
|
-
# UTF-8.
|
407
|
-
#
|
408
|
-
# @return [Integer]
|
409
|
-
#
|
410
|
-
def offset
|
411
|
-
text_span.offset
|
412
|
-
end
|
413
|
-
alias_method :begin_offset, :offset
|
414
|
-
|
415
|
-
# Returns `true` if the Sentence has a Sentiment.
|
416
|
-
#
|
417
|
-
# @return [Boolean]
|
418
|
-
#
|
419
|
-
def sentiment?
|
420
|
-
!sentiment.nil?
|
421
|
-
end
|
422
|
-
|
423
|
-
##
|
424
|
-
# Score. See {Sentence::Sentiment#score}.
|
425
|
-
#
|
426
|
-
# @return [Float]
|
427
|
-
#
|
428
|
-
def score
|
429
|
-
return nil unless sentiment?
|
430
|
-
sentiment.score
|
431
|
-
end
|
432
|
-
|
433
|
-
##
|
434
|
-
# A non-negative number in the [0, +inf] range, which represents the
|
435
|
-
# absolute magnitude of sentiment regardless of score (positive or
|
436
|
-
# negative). See {Sentence::Sentiment#magnitude}.
|
437
|
-
#
|
438
|
-
# @return [Float]
|
439
|
-
#
|
440
|
-
def magnitude
|
441
|
-
return nil unless sentiment?
|
442
|
-
sentiment.magnitude
|
443
|
-
end
|
444
|
-
|
445
|
-
##
|
446
|
-
# @private New Sentence from a V1::Sentence object.
|
447
|
-
def self.from_grpc grpc
|
448
|
-
text_span = TextSpan.from_grpc grpc.text
|
449
|
-
sentiment = Sentence::Sentiment.from_grpc grpc.sentiment
|
450
|
-
new text_span, sentiment
|
451
|
-
end
|
452
|
-
|
453
|
-
##
|
454
|
-
# Represents the result of sentiment analysis.
|
455
|
-
#
|
456
|
-
# @attr_reader [Float] score The overall emotional leaning of the text
|
457
|
-
# in the [-1.0, 1.0] range. Larger numbers represent more positive
|
458
|
-
# sentiments.
|
459
|
-
# @attr_reader [Float] magnitude A non-negative number in the
|
460
|
-
# [0, +inf] range, which represents the overall strength of emotion
|
461
|
-
# regardless of score (positive or negative). Unlike score,
|
462
|
-
# magnitude is not normalized; each expression of emotion within the
|
463
|
-
# text (both positive and negative) contributes to the text's
|
464
|
-
# magnitude (so longer text blocks may have greater magnitudes).
|
465
|
-
#
|
466
|
-
# @example
|
467
|
-
# require "google/cloud/language"
|
468
|
-
#
|
469
|
-
# language = Google::Cloud::Language.new
|
470
|
-
#
|
471
|
-
# content = "Star Wars is a great movie. \
|
472
|
-
# The Death Star is fearsome."
|
473
|
-
# document = language.document content
|
474
|
-
# annotation = document.annotate
|
475
|
-
# sentiment = annotation.sentiment
|
476
|
-
#
|
477
|
-
# sentiment.score #=> 0.10000000149011612
|
478
|
-
# sentiment.magnitude #=> 1.100000023841858
|
479
|
-
# sentiment.language #=> "en"
|
480
|
-
#
|
481
|
-
# sentence = sentiment.sentences.first
|
482
|
-
# sentence.sentiment.score #=> 0.699999988079071
|
483
|
-
# sentence.sentiment.magnitude #=> 0.699999988079071
|
484
|
-
#
|
485
|
-
class Sentiment
|
486
|
-
attr_reader :score, :magnitude
|
487
|
-
|
488
|
-
##
|
489
|
-
# @private Creates a new Sentence::Sentiment instance.
|
490
|
-
def initialize score, magnitude
|
491
|
-
@score = score
|
492
|
-
@magnitude = magnitude
|
493
|
-
end
|
494
|
-
|
495
|
-
##
|
496
|
-
# @private New Sentence::Sentiment from a V1::Sentiment object.
|
497
|
-
def self.from_grpc grpc
|
498
|
-
return nil if grpc.nil?
|
499
|
-
new grpc.score, grpc.magnitude
|
500
|
-
end
|
501
|
-
end
|
502
|
-
end
|
503
|
-
|
504
|
-
##
|
505
|
-
# Represents the smallest syntactic building block of the text. Returned
|
506
|
-
# by syntactic analysis.
|
507
|
-
#
|
508
|
-
# @attr_reader [TextSpan] text_span The token text.
|
509
|
-
# @attr_reader [PartOfSpeech] part_of_speech Represents part of speech
|
510
|
-
# information for a token.
|
511
|
-
# @attr_reader [Integer] head_token_index Represents the head of this
|
512
|
-
# token in the dependency tree. This is the index of the token which
|
513
|
-
# has an arc going to this token. The index is the position of the
|
514
|
-
# token in the array of tokens returned by the API method. If this
|
515
|
-
# token is a root token, then the headTokenIndex is its own index.
|
516
|
-
# @attr_reader [Symbol] label The parse label for the token.
|
517
|
-
# @attr_reader [String] lemma [Lemma](https://en.wikipedia.org/wiki/Lemma_(morphology))
|
518
|
-
# of the token.
|
519
|
-
#
|
520
|
-
# @example
|
521
|
-
# require "google/cloud/language"
|
522
|
-
#
|
523
|
-
# language = Google::Cloud::Language.new
|
524
|
-
#
|
525
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
526
|
-
# document = language.document content
|
527
|
-
# annotation = document.annotate
|
528
|
-
#
|
529
|
-
# annotation.tokens.count #=> 13
|
530
|
-
# token = annotation.tokens.first
|
531
|
-
#
|
532
|
-
# token.text_span.text #=> "Star"
|
533
|
-
# token.text_span.offset #=> 0
|
534
|
-
# token.part_of_speech.tag #=> :NOUN
|
535
|
-
# token.part_of_speech.number #=> :SINGULAR
|
536
|
-
# token.head_token_index #=> 1
|
537
|
-
# token.label #=> :TITLE
|
538
|
-
# token.lemma #=> "Star"
|
539
|
-
#
|
540
|
-
class Token
|
541
|
-
attr_reader :text_span, :part_of_speech, :head_token_index, :label,
|
542
|
-
:lemma
|
543
|
-
|
544
|
-
##
|
545
|
-
# @private Creates a new Token instance.
|
546
|
-
def initialize text_span, part_of_speech, head_token_index, label,
|
547
|
-
lemma
|
548
|
-
@text_span = text_span
|
549
|
-
@part_of_speech = part_of_speech
|
550
|
-
@head_token_index = head_token_index
|
551
|
-
@label = label
|
552
|
-
@lemma = lemma
|
553
|
-
end
|
554
|
-
|
555
|
-
##
|
556
|
-
# The content of the output text. See {TextSpan#text}.
|
557
|
-
#
|
558
|
-
# @return [String]
|
559
|
-
#
|
560
|
-
def text
|
561
|
-
@text_span.text
|
562
|
-
end
|
563
|
-
alias_method :content, :text
|
564
|
-
|
565
|
-
##
|
566
|
-
# The beginning offset of the content in the original document. See
|
567
|
-
# {TextSpan#offset}.
|
568
|
-
#
|
569
|
-
# The API calculates the beginning offset according to the client
|
570
|
-
# system's default encoding. In Ruby this defaults to UTF-8. To change
|
571
|
-
# the offset calculation you will need to change Ruby's default
|
572
|
-
# encoding. This is commonly done by setting
|
573
|
-
# `Encoding.default_internal` to `Encoding::UTF_16` or
|
574
|
-
# `Encoding::UTF_32`. If the system is configured to use an encoding
|
575
|
-
# other than UTF-16 or UTF-32 the offset will be calculated using
|
576
|
-
# UTF-8.
|
577
|
-
#
|
578
|
-
# @return [Integer]
|
579
|
-
#
|
580
|
-
def offset
|
581
|
-
@text_span.offset
|
582
|
-
end
|
583
|
-
alias_method :begin_offset, :offset
|
584
|
-
|
585
|
-
##
|
586
|
-
# @private New Token from a V1::Token object.
|
587
|
-
def self.from_grpc grpc
|
588
|
-
text_span = TextSpan.from_grpc grpc.text
|
589
|
-
part_of_speech = PartOfSpeech.from_grpc grpc.part_of_speech
|
590
|
-
new text_span, part_of_speech,
|
591
|
-
grpc.dependency_edge.head_token_index,
|
592
|
-
grpc.dependency_edge.label, grpc.lemma
|
593
|
-
end
|
594
|
-
end
|
595
|
-
|
596
|
-
##
|
597
|
-
# Represents the result of syntax analysis.
|
598
|
-
#
|
599
|
-
# @attr_reader [Array<Sentence>] sentences The sentences returned by
|
600
|
-
# syntax analysis.
|
601
|
-
# @attr_reader [Array<Token>] sentences The tokens returned by
|
602
|
-
# syntax analysis.
|
603
|
-
# @attr_reader [String] language The language of the document (if not
|
604
|
-
# specified, the language is automatically detected). Both ISO and
|
605
|
-
# BCP-47 language codes are supported.
|
606
|
-
#
|
607
|
-
# @example
|
608
|
-
# require "google/cloud/language"
|
609
|
-
#
|
610
|
-
# language = Google::Cloud::Language.new
|
611
|
-
#
|
612
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
613
|
-
# document = language.document content
|
614
|
-
# annotation = document.annotate
|
615
|
-
#
|
616
|
-
# syntax = annotation.syntax
|
617
|
-
#
|
618
|
-
# sentence = syntax.sentences.last
|
619
|
-
# sentence.text #=> "The Death Star is fearsome."
|
620
|
-
# sentence.offset #=> 28
|
621
|
-
#
|
622
|
-
# syntax.tokens.count #=> 13
|
623
|
-
# token = syntax.tokens.first
|
624
|
-
#
|
625
|
-
# token.text_span.text #=> "Star"
|
626
|
-
# token.text_span.offset #=> 0
|
627
|
-
# token.part_of_speech.tag #=> :NOUN
|
628
|
-
# token.part_of_speech.number #=> :SINGULAR
|
629
|
-
# token.head_token_index #=> 1
|
630
|
-
# token.label #=> :TITLE
|
631
|
-
# token.lemma #=> "Star"
|
632
|
-
#
|
633
|
-
class Syntax
|
634
|
-
attr_reader :sentences, :tokens, :language
|
635
|
-
|
636
|
-
##
|
637
|
-
# @private Creates a new Syntax instance.
|
638
|
-
def initialize sentences, tokens, language
|
639
|
-
@sentences = sentences
|
640
|
-
@tokens = tokens
|
641
|
-
@language = language
|
642
|
-
end
|
643
|
-
|
644
|
-
##
|
645
|
-
# @private New Syntax from a V1::AnnotateTextResponse or
|
646
|
-
# V1::AnalyzeSyntaxResponse object.
|
647
|
-
def self.from_grpc grpc
|
648
|
-
new Array(grpc.sentences).map { |g| Sentence.from_grpc g },
|
649
|
-
Array(grpc.tokens).map { |g| Token.from_grpc g },
|
650
|
-
grpc.language
|
651
|
-
end
|
652
|
-
end
|
653
|
-
|
654
|
-
##
|
655
|
-
# The entities returned by entity analysis.
|
656
|
-
#
|
657
|
-
# @attr_reader [String] language The language of the document (if not
|
658
|
-
# specified, the language is automatically detected). Both ISO and
|
659
|
-
# BCP-47 language codes are supported.
|
660
|
-
#
|
661
|
-
# @example
|
662
|
-
# require "google/cloud/language"
|
663
|
-
#
|
664
|
-
# language = Google::Cloud::Language.new
|
665
|
-
#
|
666
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
667
|
-
# document = language.document content
|
668
|
-
# annotation = document.annotate
|
669
|
-
#
|
670
|
-
# entities = annotation.entities
|
671
|
-
# entities.count #=> 3
|
672
|
-
# entities.artwork.first.name #=> "Star Wars"
|
673
|
-
#
|
674
|
-
class Entities < DelegateClass(::Array)
|
675
|
-
attr_accessor :language
|
676
|
-
|
677
|
-
##
|
678
|
-
# @private Create a new Entities with an array of Entity instances.
|
679
|
-
def initialize entities = [], language = nil
|
680
|
-
super entities
|
681
|
-
@language = language
|
682
|
-
end
|
683
|
-
|
684
|
-
##
|
685
|
-
# Returns the entities for which {Entity#type} is `:UNKNOWN`.
|
686
|
-
#
|
687
|
-
# @return [Array<Entity>]
|
688
|
-
#
|
689
|
-
def unknown
|
690
|
-
select(&:unknown?)
|
691
|
-
end
|
692
|
-
|
693
|
-
##
|
694
|
-
# Returns the entities for which {Entity#type} is `:PERSON`.
|
695
|
-
#
|
696
|
-
# @return [Array<Entity>]
|
697
|
-
#
|
698
|
-
def people
|
699
|
-
select(&:person?)
|
700
|
-
end
|
701
|
-
|
702
|
-
##
|
703
|
-
# Returns the entities for which {Entity#type} is `:LOCATION`.
|
704
|
-
#
|
705
|
-
# @return [Array<Entity>]
|
706
|
-
#
|
707
|
-
def locations
|
708
|
-
select(&:location?)
|
709
|
-
end
|
710
|
-
alias_method :places, :locations
|
711
|
-
|
712
|
-
##
|
713
|
-
# Returns the entities for which {Entity#type} is `:ORGANIZATION`.
|
714
|
-
#
|
715
|
-
# @return [Array<Entity>]
|
716
|
-
#
|
717
|
-
def organizations
|
718
|
-
select(&:organization?)
|
719
|
-
end
|
720
|
-
|
721
|
-
##
|
722
|
-
# Returns the entities for which {Entity#type} is `:EVENT`.
|
723
|
-
#
|
724
|
-
# @return [Array<Entity>]
|
725
|
-
#
|
726
|
-
def events
|
727
|
-
select(&:event?)
|
728
|
-
end
|
729
|
-
|
730
|
-
##
|
731
|
-
# Returns the entities for which {Entity#type} is `:WORK_OF_ART`.
|
732
|
-
#
|
733
|
-
# @return [Array<Entity>]
|
734
|
-
#
|
735
|
-
def artwork
|
736
|
-
select(&:artwork?)
|
737
|
-
end
|
738
|
-
|
739
|
-
##
|
740
|
-
# Returns the entities for which {Entity#type} is `:CONSUMER_GOOD`.
|
741
|
-
#
|
742
|
-
# @return [Array<Entity>]
|
743
|
-
#
|
744
|
-
def goods
|
745
|
-
select(&:good?)
|
746
|
-
end
|
747
|
-
|
748
|
-
##
|
749
|
-
# Returns the entities for which {Entity#type} is `:OTHER`.
|
750
|
-
#
|
751
|
-
# @return [Array<Entity>]
|
752
|
-
#
|
753
|
-
def other
|
754
|
-
select(&:other?)
|
755
|
-
end
|
756
|
-
|
757
|
-
##
|
758
|
-
# @private New Entities from a V1::AnnotateTextResponse or
|
759
|
-
# V1::AnalyzeEntitiesResponse object.
|
760
|
-
def self.from_grpc grpc
|
761
|
-
entities = Array(grpc.entities).map { |g| Entity.from_grpc g }
|
762
|
-
new entities, grpc.language
|
763
|
-
end
|
764
|
-
end
|
765
|
-
|
766
|
-
##
|
767
|
-
# Represents a phrase in the text that is a known entity, such as a
|
768
|
-
# person, an organization, or location. The API associates information,
|
769
|
-
# such as salience and mentions, with entities.
|
770
|
-
#
|
771
|
-
# @attr_reader [String] name The representative name for the entity.
|
772
|
-
# @attr_reader [Symbol] type The type of the entity.
|
773
|
-
# @attr_reader [Hash<String,String>] metadata Metadata associated with
|
774
|
-
# the entity. Currently, only Wikipedia URLs are provided, if
|
775
|
-
# available. The associated key is "wikipedia_url".
|
776
|
-
# @attr_reader [Float] salience The salience score associated with the
|
777
|
-
# entity in the [0, 1.0] range. The salience score for an entity
|
778
|
-
# provides information about the importance or centrality of that
|
779
|
-
# entity to the entire document text. Scores closer to 0 are less
|
780
|
-
# salient, while scores closer to 1.0 are highly salient.
|
781
|
-
# @attr_reader [Array<Entity::Mention>] mentions The mentions of this
|
782
|
-
# entity in the input document. The API currently supports proper noun
|
783
|
-
# mentions.
|
784
|
-
#
|
785
|
-
# @example
|
786
|
-
# require "google/cloud/language"
|
787
|
-
#
|
788
|
-
# language = Google::Cloud::Language.new
|
789
|
-
#
|
790
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
791
|
-
# document = language.document content
|
792
|
-
# annotation = document.annotate
|
793
|
-
#
|
794
|
-
# entities = annotation.entities
|
795
|
-
# entities.count #=> 3
|
796
|
-
# entity = entities.first
|
797
|
-
#
|
798
|
-
# entity.name #=> "Star Wars"
|
799
|
-
# entity.type #=> :WORK_OF_ART
|
800
|
-
# entity.salience #=> 0.6457656025886536
|
801
|
-
# entity.mentions.count #=> 1
|
802
|
-
# entity.mentions.first.text # => "Star Wars"
|
803
|
-
# entity.mentions.first.offset # => 0
|
804
|
-
# entity.mid #=> "/m/06mmr"
|
805
|
-
# entity.wikipedia_url #=> "http://en.wikipedia.org/wiki/Star_Wars"
|
806
|
-
#
|
807
|
-
class Entity
|
808
|
-
attr_reader :name, :type, :metadata, :salience, :mentions
|
809
|
-
|
810
|
-
##
|
811
|
-
# @private Creates a new Entity instance.
|
812
|
-
def initialize name, type, metadata, salience, mentions
|
813
|
-
@name = name
|
814
|
-
@type = type
|
815
|
-
@metadata = metadata
|
816
|
-
@salience = salience
|
817
|
-
@mentions = mentions
|
818
|
-
end
|
819
|
-
|
820
|
-
##
|
821
|
-
# Returns `true` if {#type} is `:UNKNOWN`.
|
822
|
-
#
|
823
|
-
# @return [Boolean]
|
824
|
-
#
|
825
|
-
def unknown?
|
826
|
-
type == :UNKNOWN
|
827
|
-
end
|
828
|
-
|
829
|
-
##
|
830
|
-
# Returns `true` if {#type} is `:PERSON`.
|
831
|
-
#
|
832
|
-
# @return [Boolean]
|
833
|
-
#
|
834
|
-
def person?
|
835
|
-
type == :PERSON
|
836
|
-
end
|
837
|
-
|
838
|
-
##
|
839
|
-
# Returns `true` if {#type} is `:LOCATION`.
|
840
|
-
#
|
841
|
-
# @return [Boolean]
|
842
|
-
#
|
843
|
-
def location?
|
844
|
-
type == :LOCATION
|
845
|
-
end
|
846
|
-
alias_method :place?, :location?
|
847
|
-
|
848
|
-
##
|
849
|
-
# Returns `true` if {#type} is `:ORGANIZATION`.
|
850
|
-
#
|
851
|
-
# @return [Boolean]
|
852
|
-
#
|
853
|
-
def organization?
|
854
|
-
type == :ORGANIZATION
|
855
|
-
end
|
856
|
-
|
857
|
-
##
|
858
|
-
# Returns `true` if {#type} is `:EVENT`.
|
859
|
-
#
|
860
|
-
# @return [Boolean]
|
861
|
-
#
|
862
|
-
def event?
|
863
|
-
type == :EVENT
|
864
|
-
end
|
865
|
-
|
866
|
-
##
|
867
|
-
# Returns `true` if {#type} is `:WORK_OF_ART`.
|
868
|
-
#
|
869
|
-
# @return [Boolean]
|
870
|
-
#
|
871
|
-
def artwork?
|
872
|
-
type == :WORK_OF_ART
|
873
|
-
end
|
874
|
-
|
875
|
-
##
|
876
|
-
# Returns `true` if {#type} is `:CONSUMER_GOOD`.
|
877
|
-
#
|
878
|
-
# @return [Boolean]
|
879
|
-
#
|
880
|
-
def good?
|
881
|
-
type == :CONSUMER_GOOD
|
882
|
-
end
|
883
|
-
|
884
|
-
##
|
885
|
-
# Returns `true` if {#type} is `:OTHER`.
|
886
|
-
#
|
887
|
-
# @return [Boolean]
|
888
|
-
#
|
889
|
-
def other?
|
890
|
-
type == :OTHER
|
891
|
-
end
|
892
|
-
|
893
|
-
##
|
894
|
-
# Returns the `wikipedia_url` property of the {#metadata}.
|
895
|
-
#
|
896
|
-
# @return [String]
|
897
|
-
#
|
898
|
-
def wikipedia_url
|
899
|
-
metadata["wikipedia_url"]
|
900
|
-
end
|
901
|
-
|
902
|
-
##
|
903
|
-
# Returns the `mid` property of the {#metadata}. The MID
|
904
|
-
# (machine-generated identifier) (MID) correspods to the entity's
|
905
|
-
# [Google Knowledge Graph](https://www.google.com/intl/bn/insidesearch/features/search/knowledge.html)
|
906
|
-
# entry. Note that MID values remain unique across different
|
907
|
-
# languages, so you can use such values to tie entities together from
|
908
|
-
# different languages. For programmatically inspecting these MID
|
909
|
-
# values, please consult the [Google Knowledge Graph Search
|
910
|
-
# API](https://developers.google.com/knowledge-graph/) documentation.
|
911
|
-
#
|
912
|
-
# @return [String]
|
913
|
-
#
|
914
|
-
def mid
|
915
|
-
metadata["mid"]
|
916
|
-
end
|
917
|
-
|
918
|
-
##
|
919
|
-
# @private New Entity from a V1::Entity object.
|
920
|
-
def self.from_grpc grpc
|
921
|
-
metadata = Convert.map_to_hash grpc.metadata
|
922
|
-
mentions = Array(grpc.mentions).map do |g|
|
923
|
-
text_span = TextSpan.from_grpc g.text
|
924
|
-
Mention.new text_span, g.type
|
925
|
-
end
|
926
|
-
new grpc.name, grpc.type, metadata, grpc.salience, mentions
|
927
|
-
end
|
928
|
-
|
929
|
-
##
|
930
|
-
# Represents a piece of text including relative location.
|
931
|
-
#
|
932
|
-
# @attr_reader [TextSpan] text_span The entity mention text.
|
933
|
-
# @attr_reader [Symbol] type The type of the entity mention. The
|
934
|
-
# possible return values are `:TYPE_UNKNOWN`, `:PROPER` (proper
|
935
|
-
# name), and `:COMMON` (Common noun or noun compound).
|
936
|
-
#
|
937
|
-
#
|
938
|
-
# @example
|
939
|
-
# require "google/cloud/language"
|
940
|
-
#
|
941
|
-
# language = Google::Cloud::Language.new
|
942
|
-
#
|
943
|
-
# content = "Star Wars is a great movie. \
|
944
|
-
# The Death Star is fearsome."
|
945
|
-
# document = language.document content
|
946
|
-
# annotation = document.annotate
|
947
|
-
#
|
948
|
-
# entities = annotation.entities
|
949
|
-
# entities.count #=> 3
|
950
|
-
# entity = entities.first
|
951
|
-
#
|
952
|
-
# entity.mentions.count #=> 1
|
953
|
-
# mention = entity.mentions.first
|
954
|
-
# mention.text # => "Star Wars"
|
955
|
-
# mention.offset # => 0
|
956
|
-
# mention.proper? # => true
|
957
|
-
# mention.type # => :PROPER
|
958
|
-
#
|
959
|
-
class Mention
|
960
|
-
attr_reader :text_span, :type
|
961
|
-
|
962
|
-
##
|
963
|
-
# @private Creates a new Entity::Mention instance.
|
964
|
-
def initialize text_span, type
|
965
|
-
@text_span = text_span
|
966
|
-
@type = type
|
967
|
-
end
|
968
|
-
|
969
|
-
##
|
970
|
-
# The content of the output text. See {TextSpan#text}.
|
971
|
-
#
|
972
|
-
# @return [String]
|
973
|
-
#
|
974
|
-
def text
|
975
|
-
text_span.text
|
976
|
-
end
|
977
|
-
alias_method :content, :text
|
978
|
-
|
979
|
-
##
|
980
|
-
# The beginning offset of the content in the original document. See
|
981
|
-
# {TextSpan#offset}.
|
982
|
-
#
|
983
|
-
# The API calculates the beginning offset according to the client
|
984
|
-
# system's default encoding. In Ruby this defaults to UTF-8. To
|
985
|
-
# change the offset calculation you will need to change Ruby's
|
986
|
-
# default encoding. This is commonly done by setting
|
987
|
-
# `Encoding.default_internal` to `Encoding::UTF_16` or
|
988
|
-
# `Encoding::UTF_32`. If the system is configured to use an encoding
|
989
|
-
# other than UTF-16 or UTF-32 the offset will be calculated using
|
990
|
-
# UTF-8.
|
991
|
-
#
|
992
|
-
# @return [Integer]
|
993
|
-
#
|
994
|
-
# @attr_reader [Integer] offset
|
995
|
-
def offset
|
996
|
-
text_span.offset
|
997
|
-
end
|
998
|
-
alias_method :begin_offset, :offset
|
999
|
-
|
1000
|
-
##
|
1001
|
-
# Returns `true` if {#type} is `:PROPER`.
|
1002
|
-
#
|
1003
|
-
# @return [Boolean]
|
1004
|
-
#
|
1005
|
-
def proper?
|
1006
|
-
type == :PROPER
|
1007
|
-
end
|
1008
|
-
|
1009
|
-
##
|
1010
|
-
# Returns `true` if {#type} is `:COMMON`.
|
1011
|
-
#
|
1012
|
-
# @return [Boolean]
|
1013
|
-
#
|
1014
|
-
def common?
|
1015
|
-
type == :COMMON
|
1016
|
-
end
|
1017
|
-
|
1018
|
-
##
|
1019
|
-
# @private New Mention from a V1::Mention object.
|
1020
|
-
def self.from_grpc grpc
|
1021
|
-
text_span = TextSpan.from_grpc grpc.text
|
1022
|
-
new text_span, grpc.type
|
1023
|
-
end
|
1024
|
-
end
|
1025
|
-
end
|
1026
|
-
|
1027
|
-
##
|
1028
|
-
# Represents the result of sentiment analysis.
|
1029
|
-
#
|
1030
|
-
# @attr_reader [Float] score The overall emotional leaning of the text
|
1031
|
-
# in the [-1.0, 1.0] range. Larger numbers represent more positive
|
1032
|
-
# sentiments.
|
1033
|
-
# @attr_reader [Float] magnitude A non-negative number in the [0, +inf]
|
1034
|
-
# range, which represents the overall strength of emotion
|
1035
|
-
# regardless of score (positive or negative). Unlike score, magnitude
|
1036
|
-
# is not normalized; each expression of emotion within the text (both
|
1037
|
-
# positive and negative) contributes to the text's magnitude (so
|
1038
|
-
# longer text blocks may have greater magnitudes).
|
1039
|
-
# @attr_reader [Array<Sentence>] sentences The sentences returned by
|
1040
|
-
# sentiment analysis.
|
1041
|
-
# @attr_reader [String] language The language of the document (if not
|
1042
|
-
# specified, the language is automatically detected). Both ISO and
|
1043
|
-
# BCP-47 language codes are supported.
|
1044
|
-
#
|
1045
|
-
# @example
|
1046
|
-
# require "google/cloud/language"
|
1047
|
-
#
|
1048
|
-
# language = Google::Cloud::Language.new
|
1049
|
-
#
|
1050
|
-
# content = "Star Wars is a great movie. The Death Star is fearsome."
|
1051
|
-
# document = language.document content
|
1052
|
-
# annotation = document.annotate
|
1053
|
-
#
|
1054
|
-
# sentiment = annotation.sentiment
|
1055
|
-
# sentiment.score #=> 0.10000000149011612
|
1056
|
-
# sentiment.magnitude #=> 1.100000023841858
|
1057
|
-
# sentiment.language #=> "en"
|
1058
|
-
#
|
1059
|
-
class Sentiment
|
1060
|
-
attr_reader :score, :magnitude, :sentences, :language
|
1061
|
-
|
1062
|
-
##
|
1063
|
-
# @private Creates a new Sentiment instance.
|
1064
|
-
def initialize score, magnitude, sentences, language
|
1065
|
-
@score = score
|
1066
|
-
@magnitude = magnitude
|
1067
|
-
@sentences = sentences
|
1068
|
-
@language = language
|
1069
|
-
end
|
1070
|
-
|
1071
|
-
##
|
1072
|
-
# @private New Sentiment from a V1::AnnotateTextResponse or
|
1073
|
-
# V1::AnalyzeSentimentResponse object.
|
1074
|
-
def self.from_grpc grpc
|
1075
|
-
new grpc.document_sentiment.score,
|
1076
|
-
grpc.document_sentiment.magnitude,
|
1077
|
-
Array(grpc.sentences).map { |g| Sentence.from_grpc g },
|
1078
|
-
grpc.language
|
1079
|
-
end
|
1080
|
-
end
|
1081
|
-
end
|
1082
|
-
end
|
1083
|
-
end
|
1084
|
-
end
|