2424# [START language_entities_gcs]
2525namespace 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+ $ 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 );
6459 }
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 ();
6963 }
7064}
71- # [END language_entities_gcs]
65+ # [END language_entities_gcs]
0 commit comments