Skip to content

Commit b9eeb12

Browse files
authored
Merge pull request GoogleCloudPlatform#786 from gogasca/entities
[entities] Migrate to new client LanguageServiceClient in 0.16.2
2 parents 3ca8d3a + ba1d0ab commit b9eeb12

File tree

4 files changed

+78
-55
lines changed

4 files changed

+78
-55
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/language.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
// Regex to match a Cloud Storage path as the first argument
128128
// e.g "gs://my-bucket/file_with_text.txt"
129129
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
130-
analyze_entities_from_file($matches[1], $matches[2], $projectId);
130+
analyze_entities_from_file($matches[0], $projectId);
131131
} else {
132132
analyze_entities($content, $projectId);
133133
}

language/src/analyze_entities.php

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

27-
use Google\Cloud\Language\LanguageClient;
28-
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2929
/**
3030
* Find the entities in text.
3131
* ```
@@ -39,26 +39,41 @@
3939
function analyze_entities($text, $projectId = null)
4040
{
4141
// Create the Natural Language client
42-
$language = new LanguageClient([
43-
'projectId' => $projectId,
44-
]);
45-
46-
// Call the analyzeEntities function
47-
$annotation = $language->analyzeEntities($text);
48-
49-
// Print out information about each entity
50-
$entities = $annotation->entities();
51-
foreach ($entities as $entity) {
52-
printf('Name: %s' . PHP_EOL, $entity['name']);
53-
printf('Type: %s' . PHP_EOL, $entity['type']);
54-
printf('Salience: %s' . PHP_EOL, $entity['salience']);
55-
if (array_key_exists('wikipedia_url', $entity['metadata'])) {
56-
printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']);
42+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
43+
try {
44+
$entity_types = [
45+
0 => 'UNKNOWN',
46+
1 => 'PERSON',
47+
2 => 'LOCATION',
48+
3 => 'ORGANIZATION',
49+
4 => 'EVENT',
50+
5 => 'WORK_OF_ART',
51+
6 => 'CONSUMER_GOOD',
52+
7 => 'OTHER',
53+
];
54+
$document = new Document();
55+
// Add text as content and set document type to PLAIN_TEXT
56+
$document->setContent($text)->setType(1);
57+
// Call the analyzeEntities function
58+
$response = $languageServiceClient->analyzeEntities($document, []);
59+
$entities = $response->getEntities();
60+
// Print out information about each entity
61+
foreach ($entities as $entity) {
62+
printf('Name: %s' . PHP_EOL, $entity->getName());
63+
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
64+
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
65+
if($entity->getMetadata()->offsetExists('wikipedia_url')) {
66+
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
67+
}
68+
if($entity->getMetadata()->offsetExists('mid')) {
69+
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid'));
70+
}
71+
printf(PHP_EOL);
5772
}
58-
if (array_key_exists('mid', $entity['metadata'])) {
59-
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']);
60-
}
61-
printf(PHP_EOL);
73+
74+
} finally {
75+
$languageServiceClient->close();
6276
}
77+
6378
}
64-
# [END language_entities_text]
79+
# [END language_entities_text]

language/src/analyze_entities_from_file.php

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,56 @@
2424
# [START language_entities_gcs]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\LanguageClient;
28-
use Google\Cloud\Storage\StorageClient;
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2929

3030
/**
3131
* Find the entities in text stored in a Cloud Storage bucket.
3232
* ```
33-
* analyze_entities_from_file('my-bucket', 'file_with_text.txt');
33+
* analyze_entities_from_file('gs://my-bucket/text.txt');
3434
* ```
3535
*
36-
* @param string $bucketName The Cloud Storage bucket.
37-
* @param string $objectName The Cloud Storage object with text.
36+
* @param string $gcsUri The Cloud Storage path with text.
3837
* @param string $projectId (optional) Your Google Cloud Project ID
3938
*
4039
*/
41-
function analyze_entities_from_file($bucketName, $objectName, $projectId = null)
40+
function analyze_entities_from_file($gcsUri, $projectId = null)
4241
{
43-
// Create the Cloud Storage object
44-
$storage = new StorageClient();
45-
$bucket = $storage->bucket($bucketName);
46-
$storageObject = $bucket->object($objectName);
47-
4842
// Create the Natural Language client
49-
$language = new LanguageClient([
50-
'projectId' => $projectId,
51-
]);
52-
53-
// Call the analyzeEntities function
54-
$annotation = $language->analyzeEntities($storageObject);
55-
56-
// Print out information about each entity
57-
$entities = $annotation->entities();
58-
foreach ($entities as $entity) {
59-
printf('Name: %s' . PHP_EOL, $entity['name']);
60-
printf('Type: %s' . PHP_EOL, $entity['type']);
61-
printf('Salience: %s' . PHP_EOL, $entity['salience']);
62-
if (array_key_exists('wikipedia_url', $entity['metadata'])) {
63-
printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']);
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+
$document = new Document();
56+
// Pass GCS URI and set document type to PLAIN_TEXT
57+
$document->setGcsContentUri($gcsUri)->setType(1);
58+
// Call the analyzeEntities function
59+
$response = $languageServiceClient->analyzeEntities($document, []);
60+
$entities = $response->getEntities();
61+
// Print out information about each entity
62+
foreach ($entities as $entity) {
63+
printf('Name: %s' . PHP_EOL, $entity->getName());
64+
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
65+
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
66+
if($entity->getMetadata()->offsetExists('wikipedia_url')) {
67+
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
68+
}
69+
if($entity->getMetadata()->offsetExists('mid')) {
70+
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid'));
71+
}
72+
printf(PHP_EOL);
6473
}
65-
if (array_key_exists('mid', $entity['metadata'])) {
66-
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']);
67-
}
68-
printf(PHP_EOL);
74+
75+
} finally {
76+
$languageServiceClient->close();
6977
}
7078
}
71-
# [END language_entities_gcs]
79+
# [END language_entities_gcs]

0 commit comments

Comments
 (0)