|
24 | 24 | # [START language_sentiment_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 sentiment in text stored in a Cloud Storage bucket.
|
32 | 32 | * ```
|
33 | 33 | * analyze_sentiment_from_file('my-bucket', 'file_with_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 Your Cloud Storage bucket URI |
38 | 37 | * @param string $projectId (optional) Your Google Cloud Project ID
|
39 | 38 | *
|
40 | 39 | */
|
41 |
| -function analyze_sentiment_from_file($bucketName, $objectName, $projectId = null) |
| 40 | +function analyze_sentiment_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 |
| - // Create the Natural Language client |
49 |
| - $language = new LanguageClient([ |
50 |
| - 'projectId' => $projectId, |
51 |
| - ]); |
52 |
| - |
53 |
| - // Call the analyzeSentiment function |
54 |
| - $annotation = $language->analyzeSentiment($storageObject); |
55 |
| - |
56 |
| - // Print document and sentence sentiment information |
57 |
| - $sentiment = $annotation->sentiment(); |
58 |
| - printf('Document Sentiment:' . PHP_EOL); |
59 |
| - printf(' Magnitude: %s' . PHP_EOL, $sentiment['magnitude']); |
60 |
| - printf(' Score: %s' . PHP_EOL, $sentiment['score']); |
61 |
| - printf(PHP_EOL); |
62 |
| - foreach ($annotation->sentences() as $sentence) { |
63 |
| - printf('Sentence: %s' . PHP_EOL, $sentence['text']['content']); |
64 |
| - printf('Sentence Sentiment:' . PHP_EOL); |
65 |
| - printf(' Magnitude: %s' . PHP_EOL, $sentence['sentiment']['magnitude']); |
66 |
| - printf(' Score: %s' . PHP_EOL, $sentence['sentiment']['score']); |
| 42 | + $languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]); |
| 43 | + try { |
| 44 | + // Create a new Document |
| 45 | + $document = new Document(); |
| 46 | + // Pass GCS URI and set document type to PLAIN_TEXT |
| 47 | + $document->setGcsContentUri($gcsUri)->setType(1); |
| 48 | + // Call the analyzeSentiment function |
| 49 | + $response = $languageServiceClient->analyzeSentiment($document); |
| 50 | + $document_sentiment = $response->getDocumentSentiment(); |
| 51 | + // Print document information |
| 52 | + printf('Document Sentiment:' . PHP_EOL); |
| 53 | + printf(' Magnitude: %s' . PHP_EOL, $document_sentiment->getMagnitude()); |
| 54 | + printf(' Score: %s' . PHP_EOL, $document_sentiment->getScore()); |
67 | 55 | printf(PHP_EOL);
|
| 56 | + $sentences = $response->getSentences(); |
| 57 | + foreach ($sentences as $sentence) { |
| 58 | + printf('Sentence: %s' . PHP_EOL, $sentence->getText()->getContent()); |
| 59 | + printf('Sentence Sentiment:' . PHP_EOL); |
| 60 | + $sentiment = $sentence->getSentiment(); |
| 61 | + if ($sentiment) { |
| 62 | + printf('Entity Magnitude: %s' . PHP_EOL, $sentiment->getMagnitude()); |
| 63 | + printf('Entity Score: %s' . PHP_EOL, $sentiment->getScore()); |
| 64 | + } |
| 65 | + printf(PHP_EOL); |
| 66 | + } |
| 67 | + } finally { |
| 68 | + $languageServiceClient->close(); |
68 | 69 | }
|
69 | 70 | }
|
70 | 71 | # [END language_sentiment_gcs]
|
0 commit comments