Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Outline

Google Cloud Natural Language API Sample

This Python sample demonstrates the use of the Google Cloud Natural Language API for sentiment, entity, and syntax analysis.

Setup

Please follow the Set Up Your Project steps in the Quickstart doc to create a project and enable the Cloud Natural Language API. Following those steps, make sure that you Set Up a Service Account, and export the following environment variable:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json

Run the sample

Install pip if not already installed.

To run the example, install the necessary libraries using pip:

$ pip install -r requirements.txt

Then, run the script:

$ python analyze.py <command> <text-string>

where <command> is one of: entities, sentiment, or syntax.

The script will write to STDOUT the json returned from the API for the requested feature.

For example, if you run:

$ python analyze.py entities "Tom Sawyer is a book written by a guy known as Mark Twain."

You will see something like the following returned:

{
  "entities": [
    {
      "salience": 0.49785897,
      "mentions": [
        {
          "text": {
            "content": "Tom Sawyer",
            "beginOffset": 0
          }
        }
      ],
      "type": "PERSON",
      "name": "Tom Sawyer",
      "metadata": {
        "wikipedia_url": "http://en.wikipedia.org/wiki/The_Adventures_of_Tom_Sawyer"
      }
    },
    {
      "salience": 0.12209519,
      "mentions": [
        {
          "text": {
            "content": "Mark Twain",
            "beginOffset": 47
          }
        }
      ],
      "type": "PERSON",
      "name": "Mark Twain",
      "metadata": {
        "wikipedia_url": "http://en.wikipedia.org/wiki/Mark_Twain"
      }
    }
  ],
  "language": "en"
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.