Skip to content

Commit 32c81c6

Browse files
committed
[entity_sentiment] Migrate to new client LanguageServiceClient in 0.16.2
1 parent 00f6623 commit 32c81c6

File tree

3 files changed

+73
-46
lines changed

3 files changed

+73
-46
lines changed

language/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-language": "~0.12",
3+
"google/cloud-language": "~0.16.2",
44
"google/cloud-storage": "^1.3",
55
"symfony/console": "^3.0"
66
},

language/src/analyze_entity_sentiment.php

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
# [START language_entity_sentiment_text]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\LanguageClient;
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2829

2930
/**
3031
* Find the entities in text.
@@ -39,27 +40,39 @@
3940

4041
function analyze_entity_sentiment($text, $projectId = null)
4142
{
42-
// Create the Natural Language client
43-
$language = new LanguageClient([
44-
'projectId' => $projectId,
45-
]);
46-
47-
// Call the analyzeEntitySentiment function
48-
$response = $language->analyzeEntitySentiment($text);
49-
$info = $response->info();
50-
$entities = $info['entities'];
51-
52-
$entity_types = array('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', 'EVENT',
53-
'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER');
54-
55-
// Print out information about each entity
56-
foreach ($entities as $entity) {
57-
printf('Entity Name: %s' . PHP_EOL, $entity['name']);
58-
printf('Entity Type: %s' . PHP_EOL, $entity['type']);
59-
printf('Entity Salience: %s' . PHP_EOL, $entity['salience']);
60-
printf('Entity Magnitude: %s' . PHP_EOL, $entity['sentiment']['magnitude']);
61-
printf('Entity Score: %s' . PHP_EOL, $entity['sentiment']['score']);
62-
printf(PHP_EOL);
43+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
44+
try {
45+
$entity_types = [
46+
0 => 'UNKNOWN',
47+
1 => 'PERSON',
48+
2 => 'LOCATION',
49+
3 => 'ORGANIZATION',
50+
4 => 'EVENT',
51+
5 => 'WORK_OF_ART',
52+
6 => 'CONSUMER_GOOD',
53+
7 => 'OTHER',
54+
];
55+
// Create a new Document
56+
$document = new Document();
57+
// Add text as content and set document type to PLAIN_TEXT
58+
$document->setContent($text)->setType(1);
59+
// Call the analyzeEntitySentiment function
60+
$response = $languageServiceClient->analyzeEntitySentiment($document);
61+
$entities = $response->getEntities();
62+
// Print out information about each entity
63+
foreach ($entities as $entity) {
64+
printf('Entity Name: %s' . PHP_EOL, $entity->getName());
65+
printf('Entity Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
66+
printf('Entity Salience: %s' . PHP_EOL, $entity->getSalience());
67+
$sentiment = $entity->getSentiment();
68+
if ($sentiment) {
69+
printf('Entity Magnitude: %s' . PHP_EOL, $sentiment->getMagnitude());
70+
printf('Entity Score: %s' . PHP_EOL, $sentiment->getScore());
71+
}
72+
printf(PHP_EOL);
73+
}
74+
} finally {
75+
$languageServiceClient->close();
6376
}
6477
}
6578
# [END language_entity_sentiment_text]

language/src/analyze_entity_sentiment_from_file.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,56 @@
2424
# [START language_entity_sentiment_gcs]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\LanguageClient;
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2829

2930
/**
3031
* Find the entities in text.
3132
* ```
3233
* analyze_entity_sentiment_from_file('gs://storage-bucket/file-name');
3334
* ```
3435
*
35-
* @param string $cloud_storage_uri Your Cloud Storage bucket URI
36+
* @param string $gcsUri Your Cloud Storage bucket URI
3637
* @param string $projectId (optional) Your Google Cloud Project ID
3738
*
3839
*/
3940

40-
function analyze_entity_sentiment_from_file($cloud_storage_uri, $projectId = null)
41+
function analyze_entity_sentiment_from_file($gcsUri, $projectId = null)
4142
{
4243
// Create the Natural Language client
43-
$language = new LanguageClient([
44-
'projectId' => $projectId,
45-
]);
46-
47-
// Call the analyzeEntitySentiment function
48-
$response = $language->analyzeEntitySentiment($cloud_storage_uri);
49-
$info = $response->info();
50-
$entities = $info['entities'];
51-
52-
$entity_types = array('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', 'EVENT',
53-
'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER');
54-
55-
// Print out information about each entity
56-
foreach ($entities as $entity) {
57-
printf('Entity Name: %s' . PHP_EOL, $entity['name']);
58-
printf('Entity Type: %s' . PHP_EOL, $entity['type']);
59-
printf('Entity Salience: %s' . PHP_EOL, $entity['salience']);
60-
printf('Entity Magnitude: %s' . PHP_EOL, $entity['sentiment']['magnitude']);
61-
printf('Entity Score: %s' . PHP_EOL, $entity['sentiment']['score']);
62-
printf(PHP_EOL);
44+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
45+
try {
46+
$entity_types = [
47+
0 => 'UNKNOWN',
48+
1 => 'PERSON',
49+
2 => 'LOCATION',
50+
3 => 'ORGANIZATION',
51+
4 => 'EVENT',
52+
5 => 'WORK_OF_ART',
53+
6 => 'CONSUMER_GOOD',
54+
7 => 'OTHER',
55+
];
56+
// Create a new Document
57+
$document = new Document();
58+
// Pass GCS URI and set document type to PLAIN_TEXT
59+
$document->setGcsContentUri($gcsUri)->setType(1);
60+
// Call the analyzeEntitySentiment function
61+
$response = $languageServiceClient->analyzeEntitySentiment($document);
62+
$entities = $response->getEntities();
63+
// Print out information about each entity
64+
foreach ($entities as $entity) {
65+
printf('Entity Name: %s' . PHP_EOL, $entity->getName());
66+
printf('Entity Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
67+
printf('Entity Salience: %s' . PHP_EOL, $entity->getSalience());
68+
$sentiment = $entity->getSentiment();
69+
if ($sentiment) {
70+
printf('Entity Magnitude: %s' . PHP_EOL, $sentiment->getMagnitude());
71+
printf('Entity Score: %s' . PHP_EOL, $sentiment->getScore());
72+
}
73+
printf(PHP_EOL);
74+
}
75+
} finally {
76+
$languageServiceClient->close();
6377
}
6478
}
6579
# [END language_entity_sentiment_gcs]

0 commit comments

Comments
 (0)