google-cloud-language 0.21.0 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +9 -0
- data/LICENSE +201 -0
- data/README.md +66 -0
- data/lib/google/cloud/language.rb +3 -4
- data/lib/google/cloud/language/annotation.rb +445 -35
- data/lib/google/cloud/language/credentials.rb +2 -2
- data/lib/google/cloud/language/document.rb +44 -19
- data/lib/google/cloud/language/project.rb +39 -13
- data/lib/google/cloud/language/service.rb +17 -8
- data/lib/google/cloud/language/v1.rb +15 -0
- data/lib/google/cloud/language/v1/doc/google/cloud/language/v1/language_service.rb +911 -0
- data/lib/google/cloud/language/{v1beta1 → v1}/language_service_api.rb +81 -39
- data/lib/google/cloud/language/{v1beta1 → v1}/language_service_client_config.json +6 -1
- data/lib/google/cloud/language/v1/language_service_pb.rb +361 -0
- data/lib/google/cloud/language/{v1beta1 → v1}/language_service_services_pb.rb +10 -8
- data/lib/google/cloud/language/version.rb +1 -1
- metadata +14 -9
- data/lib/google/cloud/language/v1beta1/language_service_pb.rb +0 -228
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f31769210a6e8e0f5e20e8342f5f325ab270598
|
4
|
+
data.tar.gz: d12adbb2196fe3335eeea256251cb1425ad6c1dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8b08f7aa5c719616704182036f025ced42d37e8673bfe9ccd3589b7733cbefa8047ea3667547801c6a7fc7087ff714d8d20b7c8386464c6ed4926b685da90e3
|
7
|
+
data.tar.gz: d7bb652ae1cbb869fe5f0080738fc6c95787e35c42b2b2ee84985980227362e5bb6d37b66aea2dfaefe9a4185994d908f4a71039e875b6eeabebb0acb93ba3a7
|
data/.yardopts
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# google-cloud-language
|
2
|
+
|
3
|
+
[Google Cloud Natural Language API](https://cloud.google.com/language/) ([docs](https://cloud.google.com/language/docs)) reveals the structure and meaning of text by offering powerful machine learning models in an easy to use REST API. You can use it to extract information about people, places, events and much more, mentioned in text documents, news articles or blog posts. You can use it to understand sentiment about your product on social media or parse intent from customer conversations happening in a call center or a messaging app. You can analyze text uploaded in your request or integrate with your document storage on Google Cloud Storage.
|
4
|
+
|
5
|
+
The Google Cloud Natural Language API is currently a beta release, and might be changed in backward-incompatible ways. It is not subject to any SLA or deprecation policy and is not intended for real-time usage in critical applications.
|
6
|
+
|
7
|
+
- [google-cloud-language API documentation](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-language/master/google/cloud/language)
|
8
|
+
- [google-cloud-language on RubyGems](https://rubygems.org/gems/google-cloud-language)
|
9
|
+
- [Google Cloud Natural Language API documentation](https://cloud.google.com/language/docs)
|
10
|
+
|
11
|
+
## Quick Start
|
12
|
+
|
13
|
+
```sh
|
14
|
+
$ gem install google-cloud-language
|
15
|
+
```
|
16
|
+
|
17
|
+
## Authentication
|
18
|
+
|
19
|
+
This library uses Service Account credentials to connect to Google Cloud services. When running on Compute Engine the credentials will be discovered automatically. When running on other environments the Service Account credentials can be specified by providing the path to the JSON file, or the JSON itself, in environment variables.
|
20
|
+
|
21
|
+
Instructions and configuration options are covered in the [Authentication Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-language/guides/authentication).
|
22
|
+
|
23
|
+
## Example
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "google/cloud/language"
|
27
|
+
|
28
|
+
language = Google::Cloud::Language.new
|
29
|
+
|
30
|
+
content = "Darth Vader is the best villain in Star Wars."
|
31
|
+
document = language.document content
|
32
|
+
annotation = document.annotate
|
33
|
+
|
34
|
+
annotation.sentiment.score #=> 1.0
|
35
|
+
annotation.sentiment.magnitude #=> 0.8999999761581421
|
36
|
+
annotation.entities.count #=> 2
|
37
|
+
annotation.sentences.count #=> 1
|
38
|
+
annotation.tokens.count #=> 10
|
39
|
+
```
|
40
|
+
|
41
|
+
## Supported Ruby Versions
|
42
|
+
|
43
|
+
This library is supported on Ruby 2.0+.
|
44
|
+
|
45
|
+
## Versioning
|
46
|
+
|
47
|
+
This library follows [Semantic Versioning](http://semver.org/).
|
48
|
+
|
49
|
+
It is currently in major version zero (0.y.z), which means that anything may change at any time and the public API should not be considered stable.
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Contributions to this library are always welcome and highly encouraged.
|
54
|
+
|
55
|
+
See the [Contributing Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/contributing) for more information on how to get started.
|
56
|
+
|
57
|
+
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See [Code of Conduct](../CODE_OF_CONDUCT.md) for more information.
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
This library is licensed under Apache 2.0. Full license text is available in [LICENSE](LICENSE).
|
62
|
+
|
63
|
+
## Support
|
64
|
+
|
65
|
+
Please [report bugs at the project on Github](https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues).
|
66
|
+
Don't hesitate to [ask questions](http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby) about the client or APIs on [StackOverflow](http://stackoverflow.com).
|
@@ -57,8 +57,7 @@ module Google
|
|
57
57
|
# (Ruby uses UTF-8 natively, which is the default sent to the API, so unless
|
58
58
|
# you're working with text processed in different platform, you should not
|
59
59
|
# need to set the encoding type.) Be aware that only English, Spanish, and
|
60
|
-
# Japanese language content are supported
|
61
|
-
# supports English text.
|
60
|
+
# Japanese language content are supported.
|
62
61
|
#
|
63
62
|
# Use {Language::Project#document} to create documents for the Cloud Natural
|
64
63
|
# Language service. You can provide text or HTML content as a string:
|
@@ -136,7 +135,7 @@ module Google
|
|
136
135
|
# document = language.document content
|
137
136
|
# sentiment = document.sentiment # API call
|
138
137
|
#
|
139
|
-
# sentiment.
|
138
|
+
# sentiment.score #=> 1.0
|
140
139
|
# sentiment.magnitude #=> 0.8999999761581421
|
141
140
|
# ```
|
142
141
|
#
|
@@ -209,7 +208,7 @@ module Google
|
|
209
208
|
# document = language.document content
|
210
209
|
# annotation = document.annotate
|
211
210
|
#
|
212
|
-
# annotation.sentiment.
|
211
|
+
# annotation.sentiment.score #=> 1.0
|
213
212
|
# annotation.sentiment.magnitude #=> 0.8999999761581421
|
214
213
|
# annotation.entities.count #=> 2
|
215
214
|
# annotation.sentences.count #=> 1
|
@@ -34,7 +34,7 @@ module Google
|
|
34
34
|
# document = language.document content
|
35
35
|
# annotation = document.annotate
|
36
36
|
#
|
37
|
-
# annotation.sentiment.
|
37
|
+
# annotation.sentiment.score #=> 1.0
|
38
38
|
# annotation.sentiment.magnitude #=> 0.8999999761581421
|
39
39
|
# annotation.entities.count #=> 2
|
40
40
|
# annotation.sentences.count #=> 1
|
@@ -66,14 +66,12 @@ module Google
|
|
66
66
|
# document = language.document content
|
67
67
|
# annotation = document.annotate
|
68
68
|
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
69
|
+
# sentence = annotation.sentences.last
|
70
|
+
# sentence.text #=> "I hate cats."
|
71
|
+
# sentence.offset #=> 13
|
72
72
|
#
|
73
73
|
def sentences
|
74
|
-
@sentences ||=
|
75
|
-
Array(grpc.sentences).map { |g| TextSpan.from_grpc g.text }
|
76
|
-
end
|
74
|
+
@sentences ||= Array(grpc.sentences).map { |g| Sentence.from_grpc g }
|
77
75
|
end
|
78
76
|
|
79
77
|
##
|
@@ -94,8 +92,8 @@ module Google
|
|
94
92
|
# annotation.tokens.count #=> 10
|
95
93
|
# token = annotation.tokens.first
|
96
94
|
#
|
97
|
-
# token.
|
98
|
-
# token.
|
95
|
+
# token.text #=> "Darth"
|
96
|
+
# token.offset #=> 0
|
99
97
|
# token.part_of_speech #=> :NOUN
|
100
98
|
# token.head_token_index #=> 1
|
101
99
|
# token.label #=> :NN
|
@@ -105,6 +103,40 @@ module Google
|
|
105
103
|
@tokens ||= Array(grpc.tokens).map { |g| Token.from_grpc g }
|
106
104
|
end
|
107
105
|
|
106
|
+
##
|
107
|
+
# The result of syntax analysis.
|
108
|
+
#
|
109
|
+
# @return [Syntax]
|
110
|
+
#
|
111
|
+
# @example
|
112
|
+
# require "google/cloud/language"
|
113
|
+
#
|
114
|
+
# language = Google::Cloud::Language.new
|
115
|
+
#
|
116
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
117
|
+
# document = language.document content
|
118
|
+
# annotation = document.annotate
|
119
|
+
# syntax = annotation.syntax
|
120
|
+
#
|
121
|
+
# sentence = syntax.sentences.last
|
122
|
+
# sentence.text #=> "Darth Vader is the best villain in Star Wars."
|
123
|
+
# sentence.offset #=> 0
|
124
|
+
#
|
125
|
+
# syntax.tokens.count #=> 10
|
126
|
+
# token = syntax.tokens.first
|
127
|
+
#
|
128
|
+
# token.text #=> "Darth"
|
129
|
+
# token.offset #=> 0
|
130
|
+
# token.part_of_speech #=> :NOUN
|
131
|
+
# token.head_token_index #=> 1
|
132
|
+
# token.label #=> :NN
|
133
|
+
# token.lemma #=> "Darth"
|
134
|
+
#
|
135
|
+
def syntax
|
136
|
+
return nil if @grpc.tokens.nil?
|
137
|
+
@syntax ||= Syntax.from_grpc @grpc
|
138
|
+
end
|
139
|
+
|
108
140
|
##
|
109
141
|
# The entities returned by entity analysis.
|
110
142
|
#
|
@@ -150,10 +182,14 @@ module Google
|
|
150
182
|
# annotation = document.annotate
|
151
183
|
# sentiment = annotation.sentiment
|
152
184
|
#
|
153
|
-
# sentiment.
|
185
|
+
# sentiment.score #=> 1.0
|
154
186
|
# sentiment.magnitude #=> 0.8999999761581421
|
155
187
|
# sentiment.language #=> "en"
|
156
188
|
#
|
189
|
+
# sentence = sentiment.sentences.first
|
190
|
+
# sentence.sentiment.score #=> 1.0
|
191
|
+
# sentence.sentiment.magnitude #=> 0.8999999761581421
|
192
|
+
#
|
157
193
|
def sentiment
|
158
194
|
return nil if @grpc.document_sentiment.nil?
|
159
195
|
@sentiment ||= Sentiment.from_grpc @grpc
|
@@ -194,7 +230,7 @@ module Google
|
|
194
230
|
end
|
195
231
|
|
196
232
|
##
|
197
|
-
# @private New Annotation from a
|
233
|
+
# @private New Annotation from a V1::AnnotateTextResponse object.
|
198
234
|
def self.from_grpc grpc
|
199
235
|
new.tap { |a| a.instance_variable_set :@grpc, grpc }
|
200
236
|
end
|
@@ -216,7 +252,8 @@ module Google
|
|
216
252
|
# document = language.document content
|
217
253
|
# annotation = document.annotate
|
218
254
|
#
|
219
|
-
#
|
255
|
+
# sentence = annotation.sentences.last
|
256
|
+
# text_span = sentence.text_span
|
220
257
|
# text_span.text #=> "I hate cats."
|
221
258
|
# text_span.offset #=> 13
|
222
259
|
#
|
@@ -233,18 +270,221 @@ module Google
|
|
233
270
|
end
|
234
271
|
|
235
272
|
##
|
236
|
-
# @private New TextSpan from a
|
273
|
+
# @private New TextSpan from a V1::TextSpan object.
|
237
274
|
def self.from_grpc grpc
|
238
275
|
new grpc.content, grpc.begin_offset
|
239
276
|
end
|
240
277
|
end
|
241
278
|
|
279
|
+
##
|
280
|
+
# Provides grammatical information, including morphological information,
|
281
|
+
# about a token, such as the token's tense, person, number, gender,
|
282
|
+
# and so on. Only some of these attributes will be applicable to any
|
283
|
+
# given part of speech. Parts of speech are as defined in [A Universal
|
284
|
+
# Part-of-Speech Tagset]http://www.lrec-conf.org/proceedings/lrec2012/pdf/274_Paper.pdf
|
285
|
+
#
|
286
|
+
# @attr_reader [Symbol] tag The part of speech tag.
|
287
|
+
# @attr_reader [Symbol] aspect The grammatical aspect.
|
288
|
+
# @attr_reader [Symbol] case The grammatical case.
|
289
|
+
# @attr_reader [Symbol] form The grammatical form.
|
290
|
+
# @attr_reader [Symbol] gender The grammatical gender.
|
291
|
+
# @attr_reader [Symbol] mood The grammatical mood.
|
292
|
+
# @attr_reader [Symbol] number The grammatical number.
|
293
|
+
# @attr_reader [Symbol] person The grammatical person.
|
294
|
+
# @attr_reader [Symbol] proper The grammatical properness.
|
295
|
+
# @attr_reader [Symbol] reciprocity The grammatical reciprocity.
|
296
|
+
# @attr_reader [Symbol] tense The grammatical tense.
|
297
|
+
# @attr_reader [Symbol] voice The grammatical voice.
|
298
|
+
#
|
299
|
+
# @example
|
300
|
+
# require "google/cloud/language"
|
301
|
+
#
|
302
|
+
# language = Google::Cloud::Language.new
|
303
|
+
#
|
304
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
305
|
+
# document = language.document content
|
306
|
+
# annotation = document.annotate
|
307
|
+
#
|
308
|
+
# annotation.tokens.count #=> 10
|
309
|
+
# token = annotation.tokens.first
|
310
|
+
#
|
311
|
+
# token.text_span.text #=> "Darth"
|
312
|
+
# token.part_of_speech.tag #=> :NOUN
|
313
|
+
# token.part_of_speech.gender #=> :MASCULINE
|
314
|
+
#
|
315
|
+
class PartOfSpeech
|
316
|
+
attr_reader :tag, :aspect, :case, :form, :gender, :mood, :number,
|
317
|
+
:person, :proper, :reciprocity, :tense, :voice
|
318
|
+
|
319
|
+
##
|
320
|
+
# @private Creates a new PartOfSpeech instance.
|
321
|
+
def initialize tag, aspect, kase, form, gender, mood, number, person,
|
322
|
+
proper, reciprocity, tense, voice
|
323
|
+
@tag = tag
|
324
|
+
@aspect = aspect
|
325
|
+
@case = kase
|
326
|
+
@form = form
|
327
|
+
@gender = gender
|
328
|
+
@mood = mood
|
329
|
+
@number = number
|
330
|
+
@person = person
|
331
|
+
@proper = proper
|
332
|
+
@reciprocity = reciprocity
|
333
|
+
@tense = tense
|
334
|
+
@voice = voice
|
335
|
+
end
|
336
|
+
|
337
|
+
##
|
338
|
+
# @private New TextSpan from a V1::PartOfSpeech object.
|
339
|
+
def self.from_grpc grpc
|
340
|
+
new grpc.tag, grpc.aspect, grpc.case, grpc.form, grpc.gender,
|
341
|
+
grpc.mood, grpc.number, grpc.person, grpc.proper,
|
342
|
+
grpc.reciprocity, grpc.tense, grpc.voice
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
# Represents a piece of text including relative location.
|
347
|
+
#
|
348
|
+
# @attr_reader [TextSpan] text_span The sentence text.
|
349
|
+
# @attr_reader [Sentence::Sentiment] sentiment The sentence sentiment.
|
350
|
+
#
|
351
|
+
# @example
|
352
|
+
# require "google/cloud/language"
|
353
|
+
#
|
354
|
+
# language = Google::Cloud::Language.new
|
355
|
+
#
|
356
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
357
|
+
# document = language.document content
|
358
|
+
# annotation = document.annotate
|
359
|
+
#
|
360
|
+
# annotation.sentences.count #=> 1
|
361
|
+
#
|
362
|
+
class Sentence
|
363
|
+
attr_reader :text_span, :sentiment
|
364
|
+
|
365
|
+
##
|
366
|
+
# @private Creates a new Sentence instance.
|
367
|
+
def initialize text_span, sentiment
|
368
|
+
@text_span = text_span
|
369
|
+
@sentiment = sentiment
|
370
|
+
end
|
371
|
+
|
372
|
+
##
|
373
|
+
# The content of the output text. See {TextSpan#text}.
|
374
|
+
#
|
375
|
+
# @return [String]
|
376
|
+
#
|
377
|
+
def text
|
378
|
+
text_span.text
|
379
|
+
end
|
380
|
+
alias_method :content, :text
|
381
|
+
|
382
|
+
##
|
383
|
+
# The API calculates the beginning offset of the content in the
|
384
|
+
# original document according to the `encoding` specified in the
|
385
|
+
# API request. See {TextSpan#offset}.
|
386
|
+
#
|
387
|
+
# @return [Integer]
|
388
|
+
#
|
389
|
+
def offset
|
390
|
+
text_span.offset
|
391
|
+
end
|
392
|
+
alias_method :begin_offset, :offset
|
393
|
+
|
394
|
+
# Returns `true` if the Sentence has a Sentiment.
|
395
|
+
#
|
396
|
+
# @return [Boolean]
|
397
|
+
#
|
398
|
+
def sentiment?
|
399
|
+
!sentiment.nil?
|
400
|
+
end
|
401
|
+
|
402
|
+
##
|
403
|
+
# Score. See {Sentence::Sentiment#score}.
|
404
|
+
#
|
405
|
+
# @return [Float]
|
406
|
+
#
|
407
|
+
def score
|
408
|
+
return nil unless sentiment?
|
409
|
+
sentiment.score
|
410
|
+
end
|
411
|
+
|
412
|
+
##
|
413
|
+
# A non-negative number in the [0, +inf] range, which represents the
|
414
|
+
# absolute magnitude of sentiment regardless of score (positive or
|
415
|
+
# negative). See {Sentence::Sentiment#magnitude}.
|
416
|
+
#
|
417
|
+
# @return [Float]
|
418
|
+
#
|
419
|
+
def magnitude
|
420
|
+
return nil unless sentiment?
|
421
|
+
sentiment.magnitude
|
422
|
+
end
|
423
|
+
|
424
|
+
##
|
425
|
+
# @private New Sentence from a V1::Sentence object.
|
426
|
+
def self.from_grpc grpc
|
427
|
+
text_span = TextSpan.from_grpc grpc.text
|
428
|
+
sentiment = Sentence::Sentiment.from_grpc grpc.sentiment
|
429
|
+
new text_span, sentiment
|
430
|
+
end
|
431
|
+
|
432
|
+
##
|
433
|
+
# Represents the result of sentiment analysis.
|
434
|
+
#
|
435
|
+
# @attr_reader [Float] score The overall emotional leaning of the text
|
436
|
+
# in the [-1.0, 1.0] range. Larger numbers represent more positive
|
437
|
+
# sentiments.
|
438
|
+
# @attr_reader [Float] magnitude A non-negative number in the
|
439
|
+
# [0, +inf] range, which represents the overall strength of emotion
|
440
|
+
# regardless of score (positive or negative). Unlike score,
|
441
|
+
# magnitude is not normalized; each expression of emotion within the
|
442
|
+
# text (both positive and negative) contributes to the text's
|
443
|
+
# magnitude (so longer text blocks may have greater magnitudes).
|
444
|
+
#
|
445
|
+
# @example
|
446
|
+
# require "google/cloud/language"
|
447
|
+
#
|
448
|
+
# language = Google::Cloud::Language.new
|
449
|
+
#
|
450
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
451
|
+
# document = language.document content
|
452
|
+
# annotation = document.annotate
|
453
|
+
# sentiment = annotation.sentiment
|
454
|
+
#
|
455
|
+
# sentiment.score #=> 1.0
|
456
|
+
# sentiment.magnitude #=> 0.8999999761581421
|
457
|
+
# sentiment.language #=> "en"
|
458
|
+
#
|
459
|
+
# sentence = sentiment.sentences.first
|
460
|
+
# sentence.sentiment.score #=> 1.0
|
461
|
+
# sentence.sentiment.magnitude #=> 0.8999999761581421
|
462
|
+
#
|
463
|
+
class Sentiment
|
464
|
+
attr_reader :score, :magnitude
|
465
|
+
|
466
|
+
##
|
467
|
+
# @private Creates a new Sentence::Sentiment instance.
|
468
|
+
def initialize score, magnitude
|
469
|
+
@score = score
|
470
|
+
@magnitude = magnitude
|
471
|
+
end
|
472
|
+
|
473
|
+
##
|
474
|
+
# @private New Sentence::Sentiment from a V1::Sentiment object.
|
475
|
+
def self.from_grpc grpc
|
476
|
+
return nil if grpc.nil?
|
477
|
+
new grpc.score, grpc.magnitude
|
478
|
+
end
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
242
482
|
##
|
243
483
|
# Represents the smallest syntactic building block of the text. Returned
|
244
484
|
# by syntactic analysis.
|
245
485
|
#
|
246
486
|
# @attr_reader [TextSpan] text_span The token text.
|
247
|
-
# @attr_reader [
|
487
|
+
# @attr_reader [PartOfSpeech] part_of_speech Represents part of speech
|
248
488
|
# information for a token.
|
249
489
|
# @attr_reader [Integer] head_token_index Represents the head of this
|
250
490
|
# token in the dependency tree. This is the index of the token which
|
@@ -269,7 +509,8 @@ module Google
|
|
269
509
|
#
|
270
510
|
# token.text_span.text #=> "Darth"
|
271
511
|
# token.text_span.offset #=> 0
|
272
|
-
# token.part_of_speech #=> :NOUN
|
512
|
+
# token.part_of_speech.tag #=> :NOUN
|
513
|
+
# token.part_of_speech.gender #=> :MASCULINE
|
273
514
|
# token.head_token_index #=> 1
|
274
515
|
# token.label #=> :NN
|
275
516
|
# token.lemma #=> "Darth"
|
@@ -300,15 +541,73 @@ module Google
|
|
300
541
|
alias_method :begin_offset, :offset
|
301
542
|
|
302
543
|
##
|
303
|
-
# @private New Token from a
|
544
|
+
# @private New Token from a V1::Token object.
|
304
545
|
def self.from_grpc grpc
|
305
546
|
text_span = TextSpan.from_grpc grpc.text
|
306
|
-
|
547
|
+
part_of_speech = PartOfSpeech.from_grpc grpc.part_of_speech
|
548
|
+
new text_span, part_of_speech,
|
307
549
|
grpc.dependency_edge.head_token_index,
|
308
550
|
grpc.dependency_edge.label, grpc.lemma
|
309
551
|
end
|
310
552
|
end
|
311
553
|
|
554
|
+
##
|
555
|
+
# Represents the result of syntax analysis.
|
556
|
+
#
|
557
|
+
# @attr_reader [Array<Sentence>] sentences The sentences returned by
|
558
|
+
# syntax analysis.
|
559
|
+
# @attr_reader [Array<Token>] sentences The tokens returned by
|
560
|
+
# syntax analysis.
|
561
|
+
# @attr_reader [String] language The language of the document (if not
|
562
|
+
# specified, the language is automatically detected). Both ISO and
|
563
|
+
# BCP-47 language codes are supported.
|
564
|
+
#
|
565
|
+
# @example
|
566
|
+
# require "google/cloud/language"
|
567
|
+
#
|
568
|
+
# language = Google::Cloud::Language.new
|
569
|
+
#
|
570
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
571
|
+
# document = language.document content
|
572
|
+
# annotation = document.annotate
|
573
|
+
#
|
574
|
+
# syntax = annotation.syntax
|
575
|
+
#
|
576
|
+
# sentence = syntax.sentences.last
|
577
|
+
# sentence.text #=> "Darth Vader is the best villain in Star Wars."
|
578
|
+
# sentence.offset #=> 0
|
579
|
+
#
|
580
|
+
# syntax.tokens.count #=> 10
|
581
|
+
# token = syntax.tokens.first
|
582
|
+
#
|
583
|
+
# token.text #=> "Darth"
|
584
|
+
# token.offset #=> 0
|
585
|
+
# token.part_of_speech #=> :NOUN
|
586
|
+
# token.head_token_index #=> 1
|
587
|
+
# token.label #=> :NN
|
588
|
+
# token.lemma #=> "Darth"
|
589
|
+
#
|
590
|
+
class Syntax
|
591
|
+
attr_reader :sentences, :tokens, :language
|
592
|
+
|
593
|
+
##
|
594
|
+
# @private Creates a new Syntax instance.
|
595
|
+
def initialize sentences, tokens, language
|
596
|
+
@sentences = sentences
|
597
|
+
@tokens = tokens
|
598
|
+
@language = language
|
599
|
+
end
|
600
|
+
|
601
|
+
##
|
602
|
+
# @private New Syntax from a V1::AnnotateTextResponse or
|
603
|
+
# V1::AnalyzeSyntaxResponse object.
|
604
|
+
def self.from_grpc grpc
|
605
|
+
new Array(grpc.sentences).map { |g| Sentence.from_grpc g },
|
606
|
+
Array(grpc.tokens).map { |g| Token.from_grpc g },
|
607
|
+
grpc.language
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
312
611
|
##
|
313
612
|
# The entities returned by entity analysis.
|
314
613
|
#
|
@@ -414,8 +713,8 @@ module Google
|
|
414
713
|
end
|
415
714
|
|
416
715
|
##
|
417
|
-
# @private New Entities from a
|
418
|
-
#
|
716
|
+
# @private New Entities from a V1::AnnotateTextResponse or
|
717
|
+
# V1::AnalyzeEntitiesResponse object.
|
419
718
|
def self.from_grpc grpc
|
420
719
|
entities = Array(grpc.entities).map { |g| Entity.from_grpc g }
|
421
720
|
new entities, grpc.language
|
@@ -437,8 +736,9 @@ module Google
|
|
437
736
|
# provides information about the importance or centrality of that
|
438
737
|
# entity to the entire document text. Scores closer to 0 are less
|
439
738
|
# salient, while scores closer to 1.0 are highly salient.
|
440
|
-
# @attr_reader [Array<
|
441
|
-
# the input document. The API currently supports proper noun
|
739
|
+
# @attr_reader [Array<Entity::Mention>] mentions The mentions of this
|
740
|
+
# entity in the input document. The API currently supports proper noun
|
741
|
+
# mentions.
|
442
742
|
#
|
443
743
|
# @example
|
444
744
|
# require "google/cloud/language"
|
@@ -557,25 +857,132 @@ module Google
|
|
557
857
|
end
|
558
858
|
|
559
859
|
##
|
560
|
-
#
|
860
|
+
# Returns the `mid` property of the {#metadata}. The MID
|
861
|
+
# (machine-generated identifier) (MID) correspods to the entity's
|
862
|
+
# [Google Knowledge Graph](https://www.google.com/intl/bn/insidesearch/features/search/knowledge.html)
|
863
|
+
# entry. Note that MID values remain unique across different
|
864
|
+
# languages, so you can use such values to tie entities together from
|
865
|
+
# different languages. For programmatically inspecting these MID
|
866
|
+
# values, please consult the [Google Knowledge Graph Search
|
867
|
+
# API](https://developers.google.com/knowledge-graph/) documentation.
|
868
|
+
#
|
869
|
+
# @return [String]
|
870
|
+
#
|
871
|
+
def mid
|
872
|
+
metadata["mid"]
|
873
|
+
end
|
874
|
+
|
875
|
+
##
|
876
|
+
# @private New Entity from a V1::Entity object.
|
561
877
|
def self.from_grpc grpc
|
562
878
|
metadata = Core::GRPCUtils.map_to_hash grpc.metadata
|
563
879
|
mentions = Array(grpc.mentions).map do |g|
|
564
|
-
TextSpan.from_grpc g.text
|
880
|
+
text_span = TextSpan.from_grpc g.text
|
881
|
+
Mention.new text_span, g.type
|
565
882
|
end
|
566
883
|
new grpc.name, grpc.type, metadata, grpc.salience, mentions
|
567
884
|
end
|
885
|
+
|
886
|
+
##
|
887
|
+
# Represents a piece of text including relative location.
|
888
|
+
#
|
889
|
+
# @attr_reader [TextSpan] text_span The entity mention text.
|
890
|
+
# @attr_reader [Symbol] type The type of the entity mention. The
|
891
|
+
# possible return values are `:TYPE_UNKNOWN`, `:PROPER` (proper
|
892
|
+
# name), and `:COMMON` (Common noun or noun compound).
|
893
|
+
#
|
894
|
+
#
|
895
|
+
# @example
|
896
|
+
# require "google/cloud/language"
|
897
|
+
#
|
898
|
+
# language = Google::Cloud::Language.new
|
899
|
+
#
|
900
|
+
# content = "Darth Vader is the best villain in Star Wars."
|
901
|
+
# document = language.document content
|
902
|
+
# annotation = document.annotate
|
903
|
+
#
|
904
|
+
# entities = annotation.entities
|
905
|
+
# entities.count #=> 2
|
906
|
+
# entity = entities.first
|
907
|
+
#
|
908
|
+
# entity.mentions.count #=> 1
|
909
|
+
# entity.mentions.first.text # => "Darth Vader"
|
910
|
+
# entity.mentions.first.offset # => 0
|
911
|
+
# entity.mentions.first.proper? # => true
|
912
|
+
#
|
913
|
+
class Mention
|
914
|
+
attr_reader :text_span, :type
|
915
|
+
|
916
|
+
##
|
917
|
+
# @private Creates a new Entity::Mention instance.
|
918
|
+
def initialize text_span, type
|
919
|
+
@text_span = text_span
|
920
|
+
@type = type
|
921
|
+
end
|
922
|
+
|
923
|
+
##
|
924
|
+
# The content of the output text. See {TextSpan#text}.
|
925
|
+
#
|
926
|
+
# @return [String]
|
927
|
+
#
|
928
|
+
def text
|
929
|
+
text_span.text
|
930
|
+
end
|
931
|
+
alias_method :content, :text
|
932
|
+
|
933
|
+
##
|
934
|
+
# The API calculates the beginning offset of the content in the
|
935
|
+
# original document according to the `encoding` specified in the
|
936
|
+
# API request. See {TextSpan#offset}.
|
937
|
+
#
|
938
|
+
# @return [Integer]
|
939
|
+
#
|
940
|
+
# @attr_reader [Integer] offset
|
941
|
+
def offset
|
942
|
+
text_span.offset
|
943
|
+
end
|
944
|
+
alias_method :begin_offset, :offset
|
945
|
+
|
946
|
+
##
|
947
|
+
# Returns `true` if {#type} is `:PROPER`.
|
948
|
+
#
|
949
|
+
# @return [Boolean]
|
950
|
+
#
|
951
|
+
def proper?
|
952
|
+
type == :PROPER
|
953
|
+
end
|
954
|
+
|
955
|
+
##
|
956
|
+
# Returns `true` if {#type} is `:COMMON`.
|
957
|
+
#
|
958
|
+
# @return [Boolean]
|
959
|
+
#
|
960
|
+
def common?
|
961
|
+
type == :COMMON
|
962
|
+
end
|
963
|
+
|
964
|
+
##
|
965
|
+
# @private New TextSpan from a V1::TextSpan object.
|
966
|
+
def self.from_grpc grpc
|
967
|
+
new grpc.content, grpc.begin_offset
|
968
|
+
end
|
969
|
+
end
|
568
970
|
end
|
569
971
|
|
570
972
|
##
|
571
973
|
# Represents the result of sentiment analysis.
|
572
974
|
#
|
573
|
-
# @attr_reader [Float]
|
574
|
-
# [-1.0, 1.0] range. Larger numbers represent more positive
|
975
|
+
# @attr_reader [Float] score The overall emotional leaning of the text
|
976
|
+
# in the [-1.0, 1.0] range. Larger numbers represent more positive
|
575
977
|
# sentiments.
|
576
978
|
# @attr_reader [Float] magnitude A non-negative number in the [0, +inf]
|
577
|
-
# range, which represents the
|
578
|
-
# regardless of
|
979
|
+
# range, which represents the overall strength of emotion
|
980
|
+
# regardless of score (positive or negative). Unlike score, magnitude
|
981
|
+
# is not normalized; each expression of emotion within the text (both
|
982
|
+
# positive and negative) contributes to the text's magnitude (so
|
983
|
+
# longer text blocks may have greater magnitudes).
|
984
|
+
# @attr_reader [Array<Sentence>] sentences The sentences returned by
|
985
|
+
# sentiment analysis.
|
579
986
|
# @attr_reader [String] language The language of the document (if not
|
580
987
|
# specified, the language is automatically detected). Both ISO and
|
581
988
|
# BCP-47 language codes are supported.
|
@@ -590,27 +997,30 @@ module Google
|
|
590
997
|
# annotation = document.annotate
|
591
998
|
#
|
592
999
|
# sentiment = annotation.sentiment
|
593
|
-
# sentiment.
|
1000
|
+
# sentiment.score #=> 1.0
|
594
1001
|
# sentiment.magnitude #=> 0.8999999761581421
|
595
1002
|
# sentiment.language #=> "en"
|
596
1003
|
#
|
597
1004
|
class Sentiment
|
598
|
-
attr_reader :
|
1005
|
+
attr_reader :score, :magnitude, :sentences, :language
|
599
1006
|
|
600
1007
|
##
|
601
1008
|
# @private Creates a new Sentiment instance.
|
602
|
-
def initialize
|
603
|
-
@
|
1009
|
+
def initialize score, magnitude, sentences, language
|
1010
|
+
@score = score
|
604
1011
|
@magnitude = magnitude
|
1012
|
+
@sentences = sentences
|
605
1013
|
@language = language
|
606
1014
|
end
|
607
1015
|
|
608
1016
|
##
|
609
|
-
# @private New Sentiment from a
|
610
|
-
#
|
1017
|
+
# @private New Sentiment from a V1::AnnotateTextResponse or
|
1018
|
+
# V1::AnalyzeSentimentResponse object.
|
611
1019
|
def self.from_grpc grpc
|
612
|
-
new grpc.document_sentiment.
|
613
|
-
grpc.document_sentiment.magnitude,
|
1020
|
+
new grpc.document_sentiment.score,
|
1021
|
+
grpc.document_sentiment.magnitude,
|
1022
|
+
Array(grpc.sentences).map { |g| Sentence.from_grpc g },
|
1023
|
+
grpc.language
|
614
1024
|
end
|
615
1025
|
end
|
616
1026
|
end
|