|
22 | 22 | """ |
23 | 23 |
|
24 | 24 | import argparse |
| 25 | +import sys |
25 | 26 |
|
26 | 27 | from google.cloud import language |
27 | 28 | from google.cloud.language import enums |
28 | 29 | from google.cloud.language import types |
| 30 | + |
29 | 31 | import six |
30 | 32 |
|
31 | 33 |
|
@@ -192,12 +194,80 @@ def syntax_file(gcs_uri): |
192 | 194 | # [END def_syntax_file] |
193 | 195 |
|
194 | 196 |
|
| 197 | +# [START def_entity_sentiment_text] |
| 198 | +def entity_sentiment_text(text): |
| 199 | + """Detects entity sentiment in the provided text.""" |
| 200 | + client = language.LanguageServiceClient() |
| 201 | + |
| 202 | + if isinstance(text, six.binary_type): |
| 203 | + text = text.decode('utf-8') |
| 204 | + |
| 205 | + document = types.Document( |
| 206 | + content=text.encode('utf-8'), |
| 207 | + type=enums.Document.Type.PLAIN_TEXT) |
| 208 | + |
| 209 | + # Detect and send native Python encoding to receive correct word offsets. |
| 210 | + encoding = enums.EncodingType.UTF32 |
| 211 | + if sys.maxunicode == 65535: |
| 212 | + encoding = enums.EncodingType.UTF16 |
| 213 | + |
| 214 | + result = client.analyze_entity_sentiment(document, encoding) |
| 215 | + |
| 216 | + for entity in result.entities: |
| 217 | + print('Mentions: ') |
| 218 | + print(u'Name: "{}"'.format(entity.name)) |
| 219 | + for mention in entity.mentions: |
| 220 | + print(u' Begin Offset : {}'.format(mention.text.begin_offset)) |
| 221 | + print(u' Content : {}'.format(mention.text.content)) |
| 222 | + print(u' Magnitude : {}'.format(mention.sentiment.magnitude)) |
| 223 | + print(u' Sentiment : {}'.format(mention.sentiment.score)) |
| 224 | + print(u' Type : {}'.format(mention.type)) |
| 225 | + print(u'Salience: {}'.format(entity.salience)) |
| 226 | + print(u'Sentiment: {}\n'.format(entity.sentiment)) |
| 227 | +# [END def_entity_sentiment_text] |
| 228 | + |
| 229 | + |
| 230 | +def entity_sentiment_file(gcs_uri): |
| 231 | + """Detects entity sentiment in a Google Cloud Storage file.""" |
| 232 | + client = language.LanguageServiceClient() |
| 233 | + |
| 234 | + document = types.Document( |
| 235 | + gcs_content_uri=gcs_uri, |
| 236 | + type=enums.Document.Type.PLAIN_TEXT) |
| 237 | + |
| 238 | + # Detect and send native Python encoding to receive correct word offsets. |
| 239 | + encoding = enums.EncodingType.UTF32 |
| 240 | + if sys.maxunicode == 65535: |
| 241 | + encoding = enums.EncodingType.UTF16 |
| 242 | + |
| 243 | + result = client.analyze_entity_sentiment(document, encoding) |
| 244 | + |
| 245 | + for entity in result.entities: |
| 246 | + print(u'Name: "{}"'.format(entity.name)) |
| 247 | + for mention in entity.mentions: |
| 248 | + print(u' Begin Offset : {}'.format(mention.text.begin_offset)) |
| 249 | + print(u' Content : {}'.format(mention.text.content)) |
| 250 | + print(u' Magnitude : {}'.format(mention.sentiment.magnitude)) |
| 251 | + print(u' Sentiment : {}'.format(mention.sentiment.score)) |
| 252 | + print(u' Type : {}'.format(mention.type)) |
| 253 | + print(u'Salience: {}'.format(entity.salience)) |
| 254 | + print(u'Sentiment: {}\n'.format(entity.sentiment)) |
| 255 | + |
| 256 | + |
195 | 257 | if __name__ == '__main__': |
196 | 258 | parser = argparse.ArgumentParser( |
197 | 259 | description=__doc__, |
198 | 260 | formatter_class=argparse.RawDescriptionHelpFormatter) |
199 | 261 | subparsers = parser.add_subparsers(dest='command') |
200 | 262 |
|
| 263 | + sentiment_entities_text_parser = subparsers.add_parser( |
| 264 | + 'sentiment-entities-text', help=entity_sentiment_text.__doc__) |
| 265 | + sentiment_entities_text_parser.add_argument('text') |
| 266 | + |
| 267 | + sentiment_entities_file_parser = subparsers.add_parser( |
| 268 | + 'sentiment-entities-file', help=entity_sentiment_file.__doc__) |
| 269 | + sentiment_entities_file_parser.add_argument('gcs_uri') |
| 270 | + |
201 | 271 | sentiment_text_parser = subparsers.add_parser( |
202 | 272 | 'sentiment-text', help=sentiment_text.__doc__) |
203 | 273 | sentiment_text_parser.add_argument('text') |
@@ -236,3 +306,7 @@ def syntax_file(gcs_uri): |
236 | 306 | syntax_text(args.text) |
237 | 307 | elif args.command == 'syntax-file': |
238 | 308 | syntax_file(args.gcs_uri) |
| 309 | + elif args.command == 'sentiment-entities-text': |
| 310 | + entity_sentiment_text(args.text) |
| 311 | + elif args.command == 'sentiment-entities-file': |
| 312 | + entity_sentiment_file(args.gcs_uri) |
0 commit comments