Skip to content

Files

Failed to load latest commit information.

Latest commit

 Cannot retrieve latest commit at this time.

History

History

language

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Google Cloud Natural Language API Samples

Open in Cloud Shell

These samples show how to use the Google Cloud Natural Language API from PHP to analyze text.

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:

  1. When running locally, use the Google Cloud SDK

     gcloud auth application-default login
    
  2. 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.

  3. 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:

     export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
    

Install Dependencies

  1. Enable the Cloud Natural Language API.

  2. Install dependencies via Composer. Run php composer.phar install (if composer is installed locally) or composer install (if composer is installed globally).

  3. Create a service account at the Service account section in the Cloud Console

  4. Download the json key file of the service account.

  5. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.

  6. If you are using the Analyze Entity Sentiment or Classify Text features, you will need to install and enable the gRPC extension for PHP.

Samples

To run the Natural Language Samples, run php src/SNIPPET_NAME.php. For example:

$ php src/analyze_all.php "This is some text to analyze"
$ php src/analyze_all_from_file.php "gs://your-gcs-bucket/file-to-analyze.txt"

Run Analyze Entities

To run the Analyze Entities sample:

$ php src/analyze_entities.php 'I know the way to San Jose. Do You?'
Name: way
Type: OTHER
Salience: 0.63484555

Name: San Jose
Type: LOCATION
Salience: 0.36515442

Run Analyze Sentiment

To run the Analyze Sentiment sample:

Document Sentiment:
  Magnitude: 0.1
  Score: 0

Sentence: I know the way to San Jose.
Sentence Sentiment:
Entity Magnitude: 0
Entity Score: 0

Sentence: Do you?
Sentence Sentiment:
Entity Magnitude: 0
Entity Score: 0

Run Analyze Syntax

To run the Analyze Syntax sample:

$ php src/analyze_syntax.php 'I know the way to San Jose. Do you?'
Token text: I
Token part of speech: PRON

Token text: know
Token part of speech: VERB

Token text: the
Token part of speech: DET

Token text: way
Token part of speech: NOUN

Token text: to
Token part of speech: ADP

Token text: San
Token part of speech: NOUN

Token text: Jose
Token part of speech: NOUN

Token text: .
Token part of speech: PUNCT

Token text: Do
Token part of speech: VERB

Token text: you
Token part of speech: PRON

Token text: ?
Token part of speech: PUNCT

Run Analyze Entity Sentiment

To run the Analyze Entity Sentiment sample:

$ php src/analyze_entity_sentiment.php 'New York is great. New York is good.'
Entity Name: New York
Entity Type: LOCATION
Entity Salience: 1
Entity Magnitude: 1.8
Entity Score: 0.9

Run Classify Text

To run the Classify Text sample:

$ php src/classify_text.php 'The first two gubernatorial elections since
President Donald Trump took office went in favor of Democratic candidates
in Virginia and New Jersey.'
Category Name: /News/Politics
Confidence: 0.99

The client library

This sample uses the Cloud Natural Language Client Library for PHP. You can read the documentation for more details on API usage and use GitHub to browse the source and report issues.

Troubleshooting

If you get the following error, set the environment variable GCLOUD_PROJECT to your project ID:

[Google\Cloud\Core\Exception\GoogleException]
No project ID was provided, and we were unable to detect a default project ID.

If you have not set a timezone you may get an error from php. This can be resolved by:

  1. Finding where the php.ini is stored by running php -i | grep 'Configuration File'
  2. Finding out your timezone from the list on this page: http://php.net/manual/en/timezones.php
  3. Editing the php.ini file (or creating one if it doesn't exist)
  4. Adding the timezone to the php.ini file e.g., adding the following line: date.timezone = "America/Los_Angeles"