2525namespace Google \Cloud \Samples \Language ;
2626
2727use Google \Cloud \Language \V1beta2 \Document ;
28+ use Google \Cloud \Language \V1beta2 \Entity ;
2829use 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 ' ));
0 commit comments