Skip to content

Commit 5088680

Browse files
committed
Add array with Entity types
In previous API we used to get the name, now we get the constant value defined in
1 parent a0ba8c0 commit 5088680

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

language/src/analyze_entities.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace Google\Cloud\Samples\Language;
2626

2727
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\Entity;
2829
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2930
/**
3031
* Find the entities in text.
@@ -41,6 +42,16 @@ function analyze_entities($text, $projectId = null)
4142
// Create the Natural Language client
4243
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4344
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+
];
4455
$document = new Document();
4556
// Add text as content and set document type to PLAIN_TEXT, otherwise you get a Code: 3 INVALID_ARGUMENT error
4657
$document->setContent($text)->setType(1);
@@ -50,7 +61,7 @@ function analyze_entities($text, $projectId = null)
5061
// Print out information about each entity
5162
foreach ($entities as $entity) {
5263
printf('Name: %s' . PHP_EOL, $entity->getName());
53-
printf('Type: %s' . PHP_EOL, $entity->getType());
64+
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
5465
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
5566
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
5667
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid'));

language/src/analyze_entities_from_file.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ function analyze_entities_from_file($gcsUri, $projectId = null)
4242
// Create the Natural Language client
4343
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4444
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+
];
4555
$document = new Document();
4656
// Pass GCS URI and set document type to PLAIN_TEXT, otherwise you get a Code: 3 INVALID_ARGUMENT error
4757
$document->setGcsContentUri($gcsUri)->setType(1);
@@ -51,7 +61,7 @@ function analyze_entities_from_file($gcsUri, $projectId = null)
5161
// Print out information about each entity
5262
foreach ($entities as $entity) {
5363
printf('Name: %s' . PHP_EOL, $entity->getName());
54-
printf('Type: %s' . PHP_EOL, $entity->getType());
64+
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
5565
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
5666
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
5767
printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid'));

0 commit comments

Comments
 (0)