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();
Namespace
Google \ Cloud \ LanguageMethods
__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.
Parameters | |
---|---|
Name | Description |
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 to a 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: |
↳ 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 |
↳ retries |
int
Number of retries for a failed request. Defaults to |
↳ 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'];
}
Parameters | |
---|---|
Name | Description |
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
|
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 |
↳ type |
string
The document type. Acceptable values are |
↳ 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 |
Returns | |
---|---|
Type | Description |
Google\Cloud\Language\Annotation |
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.';
}
Parameters | |
---|---|
Name | Description |
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
|
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 |
↳ type |
string
The document type. Acceptable values are |
↳ 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 |
Returns | |
---|---|
Type | Description |
Google\Cloud\Language\Annotation |
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.';
}
Parameters | |
---|---|
Name | Description |
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
|
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 |
↳ type |
string
The document type. Acceptable values are |
↳ 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 |
Returns | |
---|---|
Type | Description |
Google\Cloud\Language\Annotation |
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'];
}
Parameters | |
---|---|
Name | Description |
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
|
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 |
↳ type |
string
The document type. Acceptable values are |
↳ 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 |
Returns | |
---|---|
Type | Description |
Google\Cloud\Language\Annotation |
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'];
}
Parameters | |
---|---|
Name | Description |
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
|
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 |
↳ type |
string
The document type. Acceptable values are |
↳ 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. |
Returns | |
---|---|
Type | Description |
Google\Cloud\Language\Annotation |
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'];
}
Parameters | |
---|---|
Name | Description |
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
|
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 |
↳ features |
array
Features to apply to the request. Valid values are |
↳ type |
string
The document type. Acceptable values are |
↳ 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 |
Returns | |
---|---|
Type | Description |
Google\Cloud\Language\Annotation |
Constants
VERSION
Value: '0.30.2'
FULL_CONTROL_SCOPE
Value: 'https://www.googleapis.com/auth/cloud-platform'