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
+ $ document = new Document ();
46
+ // Pass GCS URI and set document type to PLAIN_TEXT, otherwise you get a Code: 3 INVALID_ARGUMENT error
47
+ $ document ->setGcsContentUri ($ gcsUri )->setType (1 );
48
+ // Call the analyzeEntities function
49
+ $ response = $ languageServiceClient ->analyzeEntities ($ document , []);
50
+ $ entities = $ response ->getEntities ();
51
+ // Print out information about each entity
52
+ foreach ($ entities as $ entity ) {
53
+ printf ('Name: %s ' . PHP_EOL , $ entity ->getName ());
54
+ printf ('Type: %s ' . PHP_EOL , $ entity ->getType ());
55
+ printf ('Salience: %s ' . PHP_EOL , $ entity ->getSalience ());
56
+ printf ('Wikipedia URL: %s ' . PHP_EOL , $ entity ->getMetadata ()->offsetGet ('wikipedia_url ' ));
57
+ printf ('Knowledge Graph MID: %s ' . PHP_EOL , $ entity ->getMetadata ()->offsetGet ('mid ' ));
58
+ printf (PHP_EOL );
64
59
}
65
- if (array_key_exists ('mid ' , $ entity ['metadata ' ])) {
66
- printf ('Knowledge Graph MID: %s ' . PHP_EOL , $ entity ['metadata ' ]['mid ' ]);
67
- }
68
- printf (PHP_EOL );
60
+
61
+ } finally {
62
+ $ languageServiceClient ->close ();
69
63
}
70
64
}
71
- # [END language_entities_gcs]
65
+ # [END language_entities_gcs]
0 commit comments