Skip to content

Commit cb8a8f3

Browse files
dizcologyJon Wayne Parrott
authored andcommitted
* add classify text samples and tests * use longer text * move entity sentiment to v1 * flake * year when first written * year first written
1 parent 63e8d46 commit cb8a8f3

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

samples/snippets/snippets.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
"""
2323

2424
import argparse
25+
import sys
2526

2627
from google.cloud import language
2728
from google.cloud.language import enums
2829
from google.cloud.language import types
30+
2931
import six
3032

3133

@@ -192,12 +194,80 @@ def syntax_file(gcs_uri):
192194
# [END def_syntax_file]
193195

194196

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+
195257
if __name__ == '__main__':
196258
parser = argparse.ArgumentParser(
197259
description=__doc__,
198260
formatter_class=argparse.RawDescriptionHelpFormatter)
199261
subparsers = parser.add_subparsers(dest='command')
200262

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+
201271
sentiment_text_parser = subparsers.add_parser(
202272
'sentiment-text', help=sentiment_text.__doc__)
203273
sentiment_text_parser.add_argument('text')
@@ -236,3 +306,7 @@ def syntax_file(gcs_uri):
236306
syntax_text(args.text)
237307
elif args.command == 'syntax-file':
238308
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)

samples/snippets/snippets_test.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Copyright 2016 Google, Inc.
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 Google, Inc.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -56,3 +57,23 @@ def test_syntax_file(capsys):
5657
snippets.syntax_file(TEST_FILE_URL)
5758
out, _ = capsys.readouterr()
5859
assert 'NOUN: President' in out
60+
61+
62+
def test_sentiment_entities_text(capsys):
63+
snippets.entity_sentiment_text(
64+
'President Obama is speaking at the White House.')
65+
out, _ = capsys.readouterr()
66+
assert 'Content : White House' in out
67+
68+
69+
def test_sentiment_entities_file(capsys):
70+
snippets.entity_sentiment_file(TEST_FILE_URL)
71+
out, _ = capsys.readouterr()
72+
assert 'Content : White House' in out
73+
74+
75+
def test_sentiment_entities_utf(capsys):
76+
snippets.entity_sentiment_text(
77+
'foo→bar')
78+
out, _ = capsys.readouterr()
79+
assert 'Begin Offset : 4' in out

0 commit comments

Comments
 (0)