Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 9aa7b37

Browse files
dizcologybusunkim96
authored andcommitted
* update readme * keep entity sentiment in v1beta2 as well * update readme
1 parent 0eb8502 commit 9aa7b37

File tree

4 files changed

+109
-6
lines changed

4 files changed

+109
-6
lines changed

samples/snippets/cloud-client/v1/README.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ To run this sample:
7676
$ python snippets.py
7777
7878
usage: snippets.py [-h]
79-
{sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
79+
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
8080
...
8181
8282
This application demonstrates how to perform basic operations with the
@@ -86,7 +86,12 @@ To run this sample:
8686
https://cloud.google.com/natural-language/docs.
8787
8888
positional arguments:
89-
{sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
89+
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
90+
sentiment-entities-text
91+
Detects entity sentiment in the provided text.
92+
sentiment-entities-file
93+
Detects entity sentiment in a Google Cloud Storage
94+
file.
9095
sentiment-text Detects sentiment in the text.
9196
sentiment-file Detects sentiment in the file located in Google Cloud
9297
Storage.

samples/snippets/cloud-client/v1beta2/README.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ To run this sample:
7676
$ python snippets.py
7777
7878
usage: snippets.py [-h]
79-
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
79+
{classify-text,classify-file,sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
8080
...
8181
8282
This application demonstrates how to perform basic operations with the
@@ -86,7 +86,10 @@ To run this sample:
8686
https://cloud.google.com/natural-language/docs.
8787
8888
positional arguments:
89-
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
89+
{classify-text,classify-file,sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
90+
classify-text Classifies content categories of the provided text.
91+
classify-file Classifies content categories of the text in a Google
92+
Cloud Storage file.
9093
sentiment-entities-text
9194
Detects entity sentiment in the provided text.
9295
sentiment-entities-file

samples/snippets/cloud-client/v1beta2/snippets.py

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323

2424
import argparse
25+
import sys
2526

2627
# [START beta_import]
2728
from google.cloud import language_v1beta2
@@ -125,6 +126,66 @@ def entities_file(gcs_uri):
125126
entity.metadata.get('wikipedia_url', '-')))
126127

127128

129+
# [START def_entity_sentiment_text]
130+
def entity_sentiment_text(text):
131+
"""Detects entity sentiment in the provided text."""
132+
client = language_v1beta2.LanguageServiceClient()
133+
134+
if isinstance(text, six.binary_type):
135+
text = text.decode('utf-8')
136+
137+
document = types.Document(
138+
content=text.encode('utf-8'),
139+
type=enums.Document.Type.PLAIN_TEXT)
140+
141+
# Detect and send native Python encoding to receive correct word offsets.
142+
encoding = enums.EncodingType.UTF32
143+
if sys.maxunicode == 65535:
144+
encoding = enums.EncodingType.UTF16
145+
146+
result = client.analyze_entity_sentiment(document, encoding)
147+
148+
for entity in result.entities:
149+
print('Mentions: ')
150+
print(u'Name: "{}"'.format(entity.name))
151+
for mention in entity.mentions:
152+
print(u' Begin Offset : {}'.format(mention.text.begin_offset))
153+
print(u' Content : {}'.format(mention.text.content))
154+
print(u' Magnitude : {}'.format(mention.sentiment.magnitude))
155+
print(u' Sentiment : {}'.format(mention.sentiment.score))
156+
print(u' Type : {}'.format(mention.type))
157+
print(u'Salience: {}'.format(entity.salience))
158+
print(u'Sentiment: {}\n'.format(entity.sentiment))
159+
# [END def_entity_sentiment_text]
160+
161+
162+
def entity_sentiment_file(gcs_uri):
163+
"""Detects entity sentiment in a Google Cloud Storage file."""
164+
client = language_v1beta2.LanguageServiceClient()
165+
166+
document = types.Document(
167+
gcs_content_uri=gcs_uri,
168+
type=enums.Document.Type.PLAIN_TEXT)
169+
170+
# Detect and send native Python encoding to receive correct word offsets.
171+
encoding = enums.EncodingType.UTF32
172+
if sys.maxunicode == 65535:
173+
encoding = enums.EncodingType.UTF16
174+
175+
result = client.analyze_entity_sentiment(document, encoding)
176+
177+
for entity in result.entities:
178+
print(u'Name: "{}"'.format(entity.name))
179+
for mention in entity.mentions:
180+
print(u' Begin Offset : {}'.format(mention.text.begin_offset))
181+
print(u' Content : {}'.format(mention.text.content))
182+
print(u' Magnitude : {}'.format(mention.sentiment.magnitude))
183+
print(u' Sentiment : {}'.format(mention.sentiment.score))
184+
print(u' Type : {}'.format(mention.type))
185+
print(u'Salience: {}'.format(entity.salience))
186+
print(u'Sentiment: {}\n'.format(entity.sentiment))
187+
188+
128189
def syntax_text(text):
129190
"""Detects syntax in the text."""
130191
client = language_v1beta2.LanguageServiceClient()
@@ -174,7 +235,7 @@ def syntax_file(gcs_uri):
174235

175236
# [START def_classify_text]
176237
def classify_text(text):
177-
"""Classifies the provided text."""
238+
"""Classifies content categories of the provided text."""
178239
# [START beta_client]
179240
client = language_v1beta2.LanguageServiceClient()
180241
# [END beta_client]
@@ -197,7 +258,9 @@ def classify_text(text):
197258

198259
# [START def_classify_file]
199260
def classify_file(gcs_uri):
200-
"""Classifies the text in a Google Cloud Storage file."""
261+
"""Classifies content categories of the text in a Google Cloud Storage
262+
file.
263+
"""
201264
client = language_v1beta2.LanguageServiceClient()
202265

203266
document = types.Document(
@@ -227,6 +290,14 @@ def classify_file(gcs_uri):
227290
'classify-file', help=classify_file.__doc__)
228291
classify_text_parser.add_argument('gcs_uri')
229292

293+
sentiment_entities_text_parser = subparsers.add_parser(
294+
'sentiment-entities-text', help=entity_sentiment_text.__doc__)
295+
sentiment_entities_text_parser.add_argument('text')
296+
297+
sentiment_entities_file_parser = subparsers.add_parser(
298+
'sentiment-entities-file', help=entity_sentiment_file.__doc__)
299+
sentiment_entities_file_parser.add_argument('gcs_uri')
300+
230301
sentiment_text_parser = subparsers.add_parser(
231302
'sentiment-text', help=sentiment_text.__doc__)
232303
sentiment_text_parser.add_argument('text')
@@ -265,6 +336,10 @@ def classify_file(gcs_uri):
265336
syntax_text(args.text)
266337
elif args.command == 'syntax-file':
267338
syntax_file(args.gcs_uri)
339+
elif args.command == 'sentiment-entities-text':
340+
entity_sentiment_text(args.text)
341+
elif args.command == 'sentiment-entities-file':
342+
entity_sentiment_file(args.gcs_uri)
268343
elif args.command == 'classify-text':
269344
classify_text(args.text)
270345
elif args.command == 'classify-file':

samples/snippets/cloud-client/v1beta2/snippets_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ def test_syntax_file(capsys):
6969
assert 'NOUN: President' in out
7070

7171

72+
def test_sentiment_entities_text(capsys):
73+
snippets.entity_sentiment_text(
74+
'President Obama is speaking at the White House.')
75+
out, _ = capsys.readouterr()
76+
assert 'Content : White House' in out
77+
78+
79+
def test_sentiment_entities_file(capsys):
80+
snippets.entity_sentiment_file(TEST_FILE_URL)
81+
out, _ = capsys.readouterr()
82+
assert 'Content : White House' in out
83+
84+
85+
def test_sentiment_entities_utf(capsys):
86+
snippets.entity_sentiment_text(
87+
'foo→bar')
88+
out, _ = capsys.readouterr()
89+
assert 'Begin Offset : 4' in out
90+
91+
7292
def test_classify_text(capsys):
7393
snippets.classify_text(
7494
'Android is a mobile operating system developed by Google, '

0 commit comments

Comments
 (0)