|
24 | 24 | # [START language_entity_sentiment_gcs] |
25 | 25 | namespace Google\Cloud\Samples\Language; |
26 | 26 |
|
27 | | -use Google\Cloud\Language\LanguageClient; |
| 27 | +use Google\Cloud\Language\V1beta2\Document; |
| 28 | +use Google\Cloud\Language\V1beta2\Document\Type; |
| 29 | +use Google\Cloud\Language\V1beta2\LanguageServiceClient; |
28 | 30 |
|
29 | 31 | /** |
30 | 32 | * Find the entities in text. |
31 | 33 | * ``` |
32 | 34 | * analyze_entity_sentiment_from_file('gs://storage-bucket/file-name'); |
33 | 35 | * ``` |
34 | 36 | * |
35 | | - * @param string $cloud_storage_uri Your Cloud Storage bucket URI |
| 37 | + * @param string $gcsUri Your Cloud Storage bucket URI |
36 | 38 | * @param string $projectId (optional) Your Google Cloud Project ID |
37 | 39 | * |
38 | 40 | */ |
39 | 41 |
|
40 | | -function analyze_entity_sentiment_from_file($cloud_storage_uri, $projectId = null) |
| 42 | +function analyze_entity_sentiment_from_file($gcsUri, $projectId = null) |
41 | 43 | { |
42 | 44 | // Create the Natural Language client |
43 | | - $language = new LanguageClient([ |
44 | | - 'projectId' => $projectId, |
45 | | - ]); |
46 | | - |
47 | | - // Call the analyzeEntitySentiment function |
48 | | - $response = $language->analyzeEntitySentiment($cloud_storage_uri); |
49 | | - $info = $response->info(); |
50 | | - $entities = $info['entities']; |
51 | | - |
52 | | - $entity_types = array('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', 'EVENT', |
53 | | - 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER'); |
54 | | - |
55 | | - // Print out information about each entity |
56 | | - foreach ($entities as $entity) { |
57 | | - printf('Entity Name: %s' . PHP_EOL, $entity['name']); |
58 | | - printf('Entity Type: %s' . PHP_EOL, $entity['type']); |
59 | | - printf('Entity Salience: %s' . PHP_EOL, $entity['salience']); |
60 | | - printf('Entity Magnitude: %s' . PHP_EOL, $entity['sentiment']['magnitude']); |
61 | | - printf('Entity Score: %s' . PHP_EOL, $entity['sentiment']['score']); |
62 | | - printf(PHP_EOL); |
| 45 | + $languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]); |
| 46 | + try { |
| 47 | + $entity_types = [ |
| 48 | + 0 => 'UNKNOWN', |
| 49 | + 1 => 'PERSON', |
| 50 | + 2 => 'LOCATION', |
| 51 | + 3 => 'ORGANIZATION', |
| 52 | + 4 => 'EVENT', |
| 53 | + 5 => 'WORK_OF_ART', |
| 54 | + 6 => 'CONSUMER_GOOD', |
| 55 | + 7 => 'OTHER', |
| 56 | + ]; |
| 57 | + // Create a new Document |
| 58 | + $document = new Document(); |
| 59 | + // Pass GCS URI and set document type to PLAIN_TEXT |
| 60 | + $document->setGcsContentUri($gcsUri)->setType(Type::PLAIN_TEXT); |
| 61 | + // Call the analyzeEntitySentiment function |
| 62 | + $response = $languageServiceClient->analyzeEntitySentiment($document); |
| 63 | + $entities = $response->getEntities(); |
| 64 | + // Print out information about each entity |
| 65 | + foreach ($entities as $entity) { |
| 66 | + printf('Entity Name: %s' . PHP_EOL, $entity->getName()); |
| 67 | + printf('Entity Type: %s' . PHP_EOL, $entity_types[$entity->getType()]); |
| 68 | + printf('Entity Salience: %s' . PHP_EOL, $entity->getSalience()); |
| 69 | + $sentiment = $entity->getSentiment(); |
| 70 | + if ($sentiment) { |
| 71 | + printf('Entity Magnitude: %s' . PHP_EOL, $sentiment->getMagnitude()); |
| 72 | + printf('Entity Score: %s' . PHP_EOL, $sentiment->getScore()); |
| 73 | + } |
| 74 | + print(PHP_EOL); |
| 75 | + } |
| 76 | + } finally { |
| 77 | + $languageServiceClient->close(); |
63 | 78 | } |
64 | 79 | } |
65 | 80 | # [END language_entity_sentiment_gcs] |
0 commit comments