Reference documentation and code samples for the Cloud Natural Language Client class LanguageClient.
Google Cloud Natural Language provides natural language understanding technologies to developers, including sentiment analysis, entity recognition, and syntax analysis. Currently only English, Spanish, and Japanese textual context are supported. Find more information at the Google Cloud Natural Language docs .
Example:
use Google\Cloud\Language\LanguageClient;
$language = new LanguageClient();
Methods
__construct
Create a Language client.
Note that when creating a LanguageClient instance, setting $config.projectId
is not supported. To switch between projects, you
must provide credentials with access to the project.
config
array
Configuration Options.
↳ apiEndpoint
string
A hostname with optional port to use in place of the service's default endpoint.
↳ authCache
CacheItemPoolInterface
A cache for storing access tokens. Defaults toa simple in memory implementation.
↳ authCacheOptions
array
Cache configuration options.
↳ authHttpHandler
callable
A handler used to deliver Psr7 requests specifically for authentication.
↳ credentialsFetcher
FetchAuthTokenInterface
A credentials fetcher instance.
↳ httpHandler
callable
A handler used to deliver Psr7 requests. Only valid for requests sent over REST.
↳ keyFile
array
The contents of the service account credentials .json file retrieved from the Google Developer's Console. Ex: json_decode(file_get_contents($path), true)
.
↳ keyFilePath
string
The full path to your service account credentials .json file retrieved from the Google Developers Console.
↳ requestTimeout
float
Seconds to wait before timing out the request. Defaults to 0
with REST and 60
with gRPC.
↳ retries
int
Number of retries for a failed request. Defaults to 3
.
↳ scopes
array
Scopes to be used for the request.
↳ quotaProject
string
Specifies a user project to bill for access charges associated with the request.
analyzeEntities
Finds named entities (currently finds proper names) in the text, entity types, salience, mentions for each entity, and other properties in the document.
Example:
$annotation = $language->analyzeEntities('Google Cloud Platform is a powerful tool.');
foreach ($annotation->entities() as $entity) {
echo $entity['type'];
}
content
string| Google\Cloud\Storage\StorageObject
The content to analyze. May be
either a string of UTF-8 encoded content, a URI pointing to a
Google Cloud Storage object in the format of gs://{bucket-name}/{object-name}
or a
{@see \Google\Cloud\Language\Google\Cloud\Storage\StorageObject}.
options
array
Configuration options.
↳ detectGcsUri
bool
When providing $content as a string, this flag determines whether or not to attempt to detect if the string represents a Google Cloud Storage URI in the format of gs://{bucket-name}/{object-name}
. Defaults to true
.
↳ type
string
The document type. Acceptable values are PLAIN_TEXT
or HTML
. Defaults to "PLAIN_TEXT"
.
↳ language
string
The language of the document. Both ISO (e.g., en, es) and BCP-47 (e.g., en-US, es-ES) language codes are accepted. If no value is provided, the language will be detected by the service.
↳ encodingType
string
The text encoding type used by the API to calculate offsets. Acceptable values are "NONE"
, "UTF8"
, "UTF16"
and "UTF32"
. Defaults to "UTF8"
. Please note the following behaviors for the encoding type setting: "NONE"
will return a value of "-1" for offsets. "UTF8"
will return byte offsets. "UTF16"
will return code unit
offsets. "UTF32"
will return unicode character
offsets.
analyzeSentiment
Analyzes the sentiment of the provided document.
Example:
$annotation = $language->analyzeSentiment('Google Cloud Platform is a powerful tool.');
$sentiment = $annotation->sentiment();
if ($sentiment['score'] > 0) {
echo 'This is a positive message.';
}
content
string| Google\Cloud\Storage\StorageObject
The content to analyze. May be
either a string of UTF-8 encoded content, a URI pointing to a
Google Cloud Storage object in the format of gs://{bucket-name}/{object-name}
or a
{@see \Google\Cloud\Language\Google\Cloud\Storage\StorageObject}.
options
array
Configuration options.
↳ detectGcsUri
bool
When providing $content as a string, this flag determines whether or not to attempt to detect if the string represents a Google Cloud Storage URI in the format of gs://{bucket-name}/{object-name}
. Defaults to true
.
↳ type
string
The document type. Acceptable values are PLAIN_TEXT
or HTML
. Defaults to "PLAIN_TEXT"
.
↳ language
string
The language of the document. Both ISO (e.g., en, es) and BCP-47 (e.g., en-US, es-ES) language codes are accepted. If no value is provided, the language will be detected by the service.
↳ encodingType
string
The text encoding type used by the API to calculate offsets. Acceptable values are "NONE"
, "UTF8"
, "UTF16"
and "UTF32"
. Defaults to "UTF8"
. Please note the following behaviors for the encoding type setting: "NONE"
will return a value of "-1" for offsets. "UTF8"
will return byte offsets. "UTF16"
will return code unit
offsets. "UTF32"
will return unicode character
offsets.
analyzeEntitySentiment
Finds entities in the text and analyzes sentiment associated with each entity and its mentions.
Example:
$annotation = $language->analyzeEntitySentiment('Google Cloud Platform is a powerful tool.');
$entities = $annotation->entities();
echo 'Entity name: ' . $entities[0]['name'] . PHP_EOL;
if ($entities[0]['sentiment']['score'] > 0) {
echo 'This is a positive message.';
}
content
string| Google\Cloud\Storage\StorageObject
The content to analyze. May be
either a string of UTF-8 encoded content, a URI pointing to a
Google Cloud Storage object in the format of gs://{bucket-name}/{object-name}
or a
{@see \Google\Cloud\Language\Google\Cloud\Storage\StorageObject}.
options
array
Configuration options.
↳ detectGcsUri
bool
When providing $content as a string, this flag determines whether or not to attempt to detect if the string represents a Google Cloud Storage URI in the format of gs://{bucket-name}/{object-name}
. Defaults to true
.
↳ type
string
The document type. Acceptable values are PLAIN_TEXT
or HTML
. Defaults to "PLAIN_TEXT"
.
↳ language
string
The language of the document. Both ISO (e.g., en, es) and BCP-47 (e.g., en-US, es-ES) language codes are accepted. If no value is provided, the language will be detected by the service.
↳ encodingType
string
The text encoding type used by the API to calculate offsets. Acceptable values are "NONE"
, "UTF8"
, "UTF16"
and "UTF32"
. Defaults to "UTF8"
. Please note the following behaviors for the encoding type setting: "NONE"
will return a value of "-1" for offsets. "UTF8"
will return byte offsets. "UTF16"
will return code unit
offsets. "UTF32"
will return unicode character
offsets.
analyzeSyntax
Analyzes the document and provides a full set of text annotations.
Example:
$annotation = $language->analyzeSyntax('Google Cloud Platform is a powerful tool.');
foreach ($annotation->sentences() as $sentence) {
echo $sentence['text']['beginOffset'];
}
content
string| Google\Cloud\Storage\StorageObject
The content to analyze. May be
either a string of UTF-8 encoded content, a URI pointing to a
Google Cloud Storage object in the format of gs://{bucket-name}/{object-name}
or a
{@see \Google\Cloud\Language\Google\Cloud\Storage\StorageObject}.
options
array
Configuration options.
↳ detectGcsUri
bool
When providing $content as a string, this flag determines whether or not to attempt to detect if the string represents a Google Cloud Storage URI in the format of gs://{bucket-name}/{object-name}
. Defaults to true
.
↳ type
string
The document type. Acceptable values are PLAIN_TEXT
or HTML
. Defaults to "PLAIN_TEXT"
.
↳ language
string
The language of the document. Both ISO (e.g., en, es) and BCP-47 (e.g., en-US, es-ES) language codes are accepted. If no value is provided, the language will be detected by the service.
↳ encodingType
string
The text encoding type used by the API to calculate offsets. Acceptable values are "NONE"
, "UTF8"
, "UTF16"
and "UTF32"
. Defaults to "UTF8"
. Please note the following behaviors for the encoding type setting: "NONE"
will return a value of "-1" for offsets. "UTF8"
will return byte offsets. "UTF16"
will return code unit
offsets. "UTF32"
will return unicode character
offsets.
classifyText
Analyzes the document and provides a full set of text annotations, including semantic, syntactic, and sentiment information.
Example:
$annotation = $language->classifyText('Google Cloud Platform is a powerful tool.');
foreach ($annotation->categories() as $category) {
echo $category['name'];
}
content
string| Google\Cloud\Storage\StorageObject
The content to analyze. May be
either a string of UTF-8 encoded content, a URI pointing to a
Google Cloud Storage object in the format of gs://{bucket-name}/{object-name}
or a
{@see \Google\Cloud\Language\Google\Cloud\Storage\StorageObject}.
options
array
Configuration options.
↳ detectGcsUri
bool
When providing $content as a string, this flag determines whether or not to attempt to detect if the string represents a Google Cloud Storage URI in the format of gs://{bucket-name}/{object-name}
. Defaults to true
.
↳ type
string
The document type. Acceptable values are PLAIN_TEXT
or HTML
. Defaults to "PLAIN_TEXT"
.
↳ language
string
The language of the document. Both ISO (e.g., en, es) and BCP-47 (e.g., en-US, es-ES) language codes are accepted. If no value is provided, the language will be detected by the service.
annotateText
Analyzes the document and provides a full set of text annotations, including semantic, syntactic, and sentiment information.
Example:
// Annotate text with all features enabled.
$annotation = $language->annotateText('Google Cloud Platform is a powerful tool.');
$sentiment = $annotation->sentiment();
echo $sentiment['magnitude'];
// Annotate text with syntax and sentiment features enabled.
$annotation = $language->annotateText('Google Cloud Platform is a powerful tool.', [
'features' => ['syntax', 'sentiment']
]);
foreach ($annotation->tokens() as $token) {
echo $token['text']['beginOffset'];
}
content
string| Google\Cloud\Storage\StorageObject
The content to analyze. May be
either a string of UTF-8 encoded content, a URI pointing to a
Google Cloud Storage object in the format of gs://{bucket-name}/{object-name}
or a
{@see \Google\Cloud\Language\Google\Cloud\Storage\StorageObject}.
options
array
Configuration options.
↳ detectGcsUri
bool
When providing $content as a string, this flag determines whether or not to attempt to detect if the string represents a Google Cloud Storage URI in the format of gs://{bucket-name}/{object-name}
. Defaults to true
.
↳ features
array
Features to apply to the request. Valid values are syntax
, sentiment
, entities
, entitySentiment
, and classify
. If no features are provided the request will run with all features enabled.
↳ type
string
The document type. Acceptable values are PLAIN_TEXT
or HTML
. Defaults to "PLAIN_TEXT"
.
↳ language
string
The language of the document. Both ISO (e.g., en, es) and BCP-47 (e.g., en-US, es-ES) language codes are accepted. If no value is provided, the language will be detected by the service.
↳ encodingType
string
The text encoding type used by the API to calculate offsets. Acceptable values are "NONE"
, "UTF8"
, "UTF16"
and "UTF32"
. Defaults to "UTF8"
. Please note the following behaviors for the encoding type setting: "NONE"
will return a value of "-1" for offsets. "UTF8"
will return byte offsets. "UTF16"
will return code unit
offsets. "UTF32"
will return unicode character
offsets.
Constants
VERSION
Value: '0.28.5'
FULL_CONTROL_SCOPE
Value: 'https://www.googleapis.com/auth/cloud-platform'