Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme #1124

Merged
merged 3 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions language/cloud-client/v1/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ To run this sample:
$ python snippets.py

usage: snippets.py [-h]
{sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
...

This application demonstrates how to perform basic operations with the
Expand All @@ -86,7 +86,12 @@ To run this sample:
https://cloud.google.com/natural-language/docs.

positional arguments:
{sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
sentiment-entities-text
Detects entity sentiment in the provided text.
sentiment-entities-file
Detects entity sentiment in a Google Cloud Storage
file.
sentiment-text Detects sentiment in the text.
sentiment-file Detects sentiment in the file located in Google Cloud
Storage.
Expand Down
7 changes: 5 additions & 2 deletions language/cloud-client/v1beta2/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ To run this sample:
$ python snippets.py

usage: snippets.py [-h]
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
{classify-text,classify-file,sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
...

This application demonstrates how to perform basic operations with the
Expand All @@ -86,7 +86,10 @@ To run this sample:
https://cloud.google.com/natural-language/docs.

positional arguments:
{sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
{classify-text,classify-file,sentiment-entities-text,sentiment-entities-file,sentiment-text,sentiment-file,entities-text,entities-file,syntax-text,syntax-file}
classify-text Classifies content categories of the provided text.
classify-file Classifies content categories of the text in a Google
Cloud Storage file.
sentiment-entities-text
Detects entity sentiment in the provided text.
sentiment-entities-file
Expand Down
79 changes: 77 additions & 2 deletions language/cloud-client/v1beta2/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import argparse
import sys

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


# [START def_entity_sentiment_text]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the other blocks don't have includecode blocks.

def entity_sentiment_text(text):
"""Detects entity sentiment in the provided text."""
client = language_v1beta2.LanguageServiceClient()

if isinstance(text, six.binary_type):
text = text.decode('utf-8')

document = types.Document(
content=text.encode('utf-8'),
type=enums.Document.Type.PLAIN_TEXT)

# Detect and send native Python encoding to receive correct word offsets.
encoding = enums.EncodingType.UTF32
if sys.maxunicode == 65535:
encoding = enums.EncodingType.UTF16

result = client.analyze_entity_sentiment(document, encoding)

for entity in result.entities:
print('Mentions: ')
print(u'Name: "{}"'.format(entity.name))
for mention in entity.mentions:
print(u' Begin Offset : {}'.format(mention.text.begin_offset))
print(u' Content : {}'.format(mention.text.content))
print(u' Magnitude : {}'.format(mention.sentiment.magnitude))
print(u' Sentiment : {}'.format(mention.sentiment.score))
print(u' Type : {}'.format(mention.type))
print(u'Salience: {}'.format(entity.salience))
print(u'Sentiment: {}\n'.format(entity.sentiment))
# [END def_entity_sentiment_text]


def entity_sentiment_file(gcs_uri):
"""Detects entity sentiment in a Google Cloud Storage file."""
client = language_v1beta2.LanguageServiceClient()

document = types.Document(
gcs_content_uri=gcs_uri,
type=enums.Document.Type.PLAIN_TEXT)

# Detect and send native Python encoding to receive correct word offsets.
encoding = enums.EncodingType.UTF32
if sys.maxunicode == 65535:
encoding = enums.EncodingType.UTF16

result = client.analyze_entity_sentiment(document, encoding)

for entity in result.entities:
print(u'Name: "{}"'.format(entity.name))
for mention in entity.mentions:
print(u' Begin Offset : {}'.format(mention.text.begin_offset))
print(u' Content : {}'.format(mention.text.content))
print(u' Magnitude : {}'.format(mention.sentiment.magnitude))
print(u' Sentiment : {}'.format(mention.sentiment.score))
print(u' Type : {}'.format(mention.type))
print(u'Salience: {}'.format(entity.salience))
print(u'Sentiment: {}\n'.format(entity.sentiment))


def syntax_text(text):
"""Detects syntax in the text."""
client = language_v1beta2.LanguageServiceClient()
Expand Down Expand Up @@ -174,7 +235,7 @@ def syntax_file(gcs_uri):

# [START def_classify_text]
def classify_text(text):
"""Classifies the provided text."""
"""Classifies content categories of the provided text."""
# [START beta_client]
client = language_v1beta2.LanguageServiceClient()
# [END beta_client]
Expand All @@ -197,7 +258,9 @@ def classify_text(text):

# [START def_classify_file]
def classify_file(gcs_uri):
"""Classifies the text in a Google Cloud Storage file."""
"""Classifies content categories of the text in a Google Cloud Storage
file.
"""
client = language_v1beta2.LanguageServiceClient()

document = types.Document(
Expand Down Expand Up @@ -227,6 +290,14 @@ def classify_file(gcs_uri):
'classify-file', help=classify_file.__doc__)
classify_text_parser.add_argument('gcs_uri')

sentiment_entities_text_parser = subparsers.add_parser(
'sentiment-entities-text', help=entity_sentiment_text.__doc__)
sentiment_entities_text_parser.add_argument('text')

sentiment_entities_file_parser = subparsers.add_parser(
'sentiment-entities-file', help=entity_sentiment_file.__doc__)
sentiment_entities_file_parser.add_argument('gcs_uri')

sentiment_text_parser = subparsers.add_parser(
'sentiment-text', help=sentiment_text.__doc__)
sentiment_text_parser.add_argument('text')
Expand Down Expand Up @@ -265,6 +336,10 @@ def classify_file(gcs_uri):
syntax_text(args.text)
elif args.command == 'syntax-file':
syntax_file(args.gcs_uri)
elif args.command == 'sentiment-entities-text':
entity_sentiment_text(args.text)
elif args.command == 'sentiment-entities-file':
entity_sentiment_file(args.gcs_uri)
elif args.command == 'classify-text':
classify_text(args.text)
elif args.command == 'classify-file':
Expand Down
20 changes: 20 additions & 0 deletions language/cloud-client/v1beta2/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ def test_syntax_file(capsys):
assert 'NOUN: President' in out


def test_sentiment_entities_text(capsys):
snippets.entity_sentiment_text(
'President Obama is speaking at the White House.')
out, _ = capsys.readouterr()
assert 'Content : White House' in out


def test_sentiment_entities_file(capsys):
snippets.entity_sentiment_file(TEST_FILE_URL)
out, _ = capsys.readouterr()
assert 'Content : White House' in out


def test_sentiment_entities_utf(capsys):
snippets.entity_sentiment_text(
'foo→bar')
out, _ = capsys.readouterr()
assert 'Begin Offset : 4' in out


def test_classify_text(capsys):
snippets.classify_text(
'Android is a mobile operating system developed by Google, '
Expand Down