24
24
# [START language_entities_gcs]
25
25
namespace Google \Cloud \Samples \Language ;
26
26
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 ;
29
29
30
30
/**
31
31
* Find the entities in text stored in a Cloud Storage bucket.
32
32
* ```
33
- * analyze_entities_from_file('my-bucket', 'file_with_text .txt');
33
+ * analyze_entities_from_file('gs:// my-bucket/text .txt');
34
34
* ```
35
35
*
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.
38
37
* @param string $projectId (optional) Your Google Cloud Project ID
39
38
*
40
39
*/
41
- function analyze_entities_from_file ($ bucketName , $ objectName , $ projectId = null )
40
+ function analyze_entities_from_file ($ gcsUri , $ projectId = null )
42
41
{
43
- // Create the Cloud Storage object
44
- $ storage = new StorageClient ();
45
- $ bucket = $ storage ->bucket ($ bucketName );
46
- $ storageObject = $ bucket ->object ($ objectName );
47
-
48
42
// 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 );
64
73
}
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 ();
69
77
}
70
78
}
71
- # [END language_entities_gcs]
79
+ # [END language_entities_gcs]
0 commit comments