Skip to content

Commit 4311cff

Browse files
authored
Merge pull request GoogleCloudPlatform#787 from gogasca/entitiy_sentiment
[entity_sentiment] Migrate to new client LanguageServiceClient in 0.16.2
2 parents edaca52 + 80d0645 commit 4311cff

File tree

2 files changed

+74
-45
lines changed

2 files changed

+74
-45
lines changed

language/src/analyze_entity_sentiment.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
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\Document\Type;
29+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2830

2931
/**
3032
* Find the entities in text.
@@ -39,27 +41,39 @@
3941

4042
function analyze_entity_sentiment($text, $projectId = null)
4143
{
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);
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+
// Add text as content and set document type to PLAIN_TEXT
59+
$document->setContent($text)->setType(Type::PLAIN_TEXT);
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+
print(PHP_EOL);
74+
}
75+
} finally {
76+
$languageServiceClient->close();
6377
}
6478
}
6579
# [END language_entity_sentiment_text]

language/src/analyze_entity_sentiment_from_file.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,57 @@
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\Document\Type;
29+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2830

2931
/**
3032
* Find the entities in text.
3133
* ```
3234
* analyze_entity_sentiment_from_file('gs://storage-bucket/file-name');
3335
* ```
3436
*
35-
* @param string $cloud_storage_uri Your Cloud Storage bucket URI
37+
* @param string $gcsUri Your Cloud Storage bucket URI
3638
* @param string $projectId (optional) Your Google Cloud Project ID
3739
*
3840
*/
3941

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

0 commit comments

Comments
 (0)