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