Skip to content
Merged
98 changes: 98 additions & 0 deletions language/api/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
.. This file is automatically generated. Do not edit this file directly.

Google Cloud Natural Language API Python Samples
===============================================================================

This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API.




.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/

Setup
-------------------------------------------------------------------------------


Authentication
++++++++++++++

Authentication is typically done through `Application Default Credentials`_,
which means you do not have to change the code to authenticate as long as
your environment has credentials. You have a few options for setting up
authentication:

#. When running locally, use the `Google Cloud SDK`_

.. code-block:: bash

gcloud beta auth application-default login


#. When running on App Engine or Compute Engine, credentials are already
set-up. However, you may need to configure your Compute Engine instance
with `additional scopes`_.

#. You can create a `Service Account key file`_. This file can be used to
authenticate to Google Cloud Platform services from any environment. To use
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
the path to the key file, for example:

.. code-block:: bash

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

Install Dependencies
++++++++++++++++++++

#. Install `pip`_ and `virtualenv`_ if you do not already have them.

#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

.. code-block:: bash

$ virtualenv env
$ source env/bin/activate

#. Install the dependencies needed to run the samples.

.. code-block:: bash

$ pip install -r requirements.txt

.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/

Samples
-------------------------------------------------------------------------------

Analyze syntax
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



To run this sample:

.. code-block:: bash

$ python analyze.py

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

Analyzes text using the Google Cloud Natural Language API.

positional arguments:
{entities,sentiment,syntax}
text

optional arguments:
-h, --help show this help message and exit




.. _Google Cloud SDK: https://cloud.google.com/sdk/
20 changes: 10 additions & 10 deletions language/api/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def get_service():
['https://www.googleapis.com/auth/cloud-platform'])
http = httplib2.Http()
scoped_credentials.authorize(http)
return discovery.build('language', 'v1beta1', http=http)
return discovery.build('language', 'v1',
http=http,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use credentials=credentials instead of http=http?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not until discovery is public. I have added a TODO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried recently? I think there was a change to the api client recently (possibly made by Jon) that retried getting the discovery doc if it gets a permission-denied error.

Copy link
Contributor Author

@puneith puneith Nov 10, 2016

Choose a reason for hiding this comment

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

Yes, it didn't work. Actually retried before making the comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh well :-/

Copy link
Contributor

Choose a reason for hiding this comment

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

break at opening ( to avoid hanging indents. (I should make this into a t-shirt)

credentials=credentials)


def get_native_encoding_type():
Expand All @@ -48,7 +50,7 @@ def analyze_entities(text, encoding='UTF32'):
'type': 'PLAIN_TEXT',
'content': text,
},
'encodingType': encoding,
'encoding_type': encoding,
}

service = get_service()
Expand All @@ -59,12 +61,13 @@ def analyze_entities(text, encoding='UTF32'):
return response


def analyze_sentiment(text):
def analyze_sentiment(text, encoding='UTF32'):
body = {
'document': {
'type': 'PLAIN_TEXT',
'content': text,
}
},
'encoding_type': encoding
}

service = get_service()
Expand All @@ -81,15 +84,12 @@ def analyze_syntax(text, encoding='UTF32'):
'type': 'PLAIN_TEXT',
'content': text,
},
'features': {
'extract_syntax': True,
},
'encodingType': encoding,
'encoding_type': encoding
}

service = get_service()

request = service.documents().annotateText(body=body)
request = service.documents().analyzeSyntax(body=body)
response = request.execute()

return response
Expand All @@ -108,7 +108,7 @@ def analyze_syntax(text, encoding='UTF32'):
if args.command == 'entities':
result = analyze_entities(args.text, get_native_encoding_type())
elif args.command == 'sentiment':
result = analyze_sentiment(args.text)
result = analyze_sentiment(args.text, get_native_encoding_type())
elif args.command == 'syntax':
result = analyze_syntax(args.text, get_native_encoding_type())

Expand Down
4 changes: 2 additions & 2 deletions language/api/analyze_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def test_analyze_sentiment(capsys):
'your face is really ugly and i hate it.')

sentiment = result['documentSentiment']
assert sentiment['polarity'] < 0
assert sentiment['score'] < 0
assert sentiment['magnitude'] < 1

result = analyze.analyze_sentiment(
'cheerio, mate - I greatly admire the pallor of your visage, and your '
'angle of repose leaves little room for improvement.')

sentiment = result['documentSentiment']
assert sentiment['polarity'] > 0
assert sentiment['score'] > 0
assert sentiment['magnitude'] < 1


Expand Down
102 changes: 102 additions & 0 deletions language/cloud-client/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
.. This file is automatically generated. Do not edit this file directly.

Google Cloud Natural Language API Python Samples
===============================================================================

This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. This API is part of the larger Cloud Machine Learning API.




.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/

Setup
-------------------------------------------------------------------------------


Authentication
++++++++++++++

Authentication is typically done through `Application Default Credentials`_,
which means you do not have to change the code to authenticate as long as
your environment has credentials. You have a few options for setting up
authentication:

#. When running locally, use the `Google Cloud SDK`_

.. code-block:: bash

gcloud beta auth application-default login


#. When running on App Engine or Compute Engine, credentials are already
set-up. However, you may need to configure your Compute Engine instance
with `additional scopes`_.

#. You can create a `Service Account key file`_. This file can be used to
authenticate to Google Cloud Platform services from any environment. To use
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
the path to the key file, for example:

.. code-block:: bash

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

Install Dependencies
++++++++++++++++++++

#. Install `pip`_ and `virtualenv`_ if you do not already have them.

#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

.. code-block:: bash

$ virtualenv env
$ source env/bin/activate

#. Install the dependencies needed to run the samples.

.. code-block:: bash

$ pip install -r requirements.txt

.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/

Samples
-------------------------------------------------------------------------------

Quickstart
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



To run this sample:

.. code-block:: bash

$ python quickstart.py




The client library
-------------------------------------------------------------------------------

This sample uses the `Google Cloud Client Library for Python`_.
You can read the documentation for more details on API usage and use GitHub
to `browse the source`_ and `report issues`_.

.. Google Cloud Client Library for Python:
https://googlecloudplatform.github.io/google-cloud-python/
.. browse the source:
https://github.com/GoogleCloudPlatform/google-cloud-python
.. report issues:
https://github.com/GoogleCloudPlatform/google-cloud-python/issues


.. _Google Cloud SDK: https://cloud.google.com/sdk/