| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
These samples show how to use the Google Cloud Natural Language API from PHP to analyze text.
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
gcloud 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:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
Install dependencies via Composer. Run php composer.phar install (if composer is installed locally) or composer install (if composer is installed globally).
Create a service account at the Service account section in the Cloud Console
Download the json key file of the service account.
Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.
If you are using the Analyze Entity Sentiment or Classify Text features, you will need to install and enable the gRPC extension for PHP.
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"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
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
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
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
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
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.
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:
| Back | FazBrowse Home | New Git URL |