Skip to content

Commit edaca52

Browse files
authored
Merge pull request GoogleCloudPlatform#788 from gogasca/sentiment
[sentiment] Migrate to new client LanguageServiceClient in 0.16.2
2 parents 32ae358 + 573fde4 commit edaca52

File tree

3 files changed

+61
-50
lines changed

3 files changed

+61
-50
lines changed

language/language.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
// Regex to match a Cloud Storage path as the first argument
169169
// e.g "gs://my-bucket/file_with_text.txt"
170170
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
171-
analyze_sentiment_from_file($matches[1], $matches[2], $projectId);
171+
analyze_sentiment_from_file($content, $projectId);
172172
} else {
173173
analyze_sentiment($content, $projectId);
174174
}

language/src/analyze_sentiment.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
# [START language_sentiment_text]
2525
namespace Google\Cloud\Samples\Language;
2626

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;
2830

2931
/**
3032
* Find the sentiment in text.
@@ -38,26 +40,33 @@
3840
*/
3941
function analyze_sentiment($text, $projectId = null)
4042
{
41-
// Create the Natural Language client
42-
$language = new LanguageClient([
43-
'projectId' => $projectId,
44-
]);
45-
46-
// Call the analyzeSentiment function
47-
$annotation = $language->analyzeSentiment($text);
48-
49-
// Print document and sentence sentiment information
50-
$sentiment = $annotation->sentiment();
51-
printf('Document Sentiment:' . PHP_EOL);
52-
printf(' Magnitude: %s' . PHP_EOL, $sentiment['magnitude']);
53-
printf(' Score: %s' . PHP_EOL, $sentiment['score']);
54-
printf(PHP_EOL);
55-
foreach ($annotation->sentences() as $sentence) {
56-
printf('Sentence: %s' . PHP_EOL, $sentence['text']['content']);
57-
printf('Sentence Sentiment:' . PHP_EOL);
58-
printf(' Magnitude: %s' . PHP_EOL, $sentence['sentiment']['magnitude']);
59-
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->setContent($text)->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());
6056
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();
6170
}
6271
}
6372
# [END language_sentiment_text]

language/src/analyze_sentiment_from_file.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,49 @@
2424
# [START language_sentiment_gcs]
2525
namespace 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\Document\Type;
29+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2930

3031
/**
3132
* Find the sentiment in text stored in a Cloud Storage bucket.
3233
* ```
3334
* analyze_sentiment_from_file('my-bucket', 'file_with_text.txt');
3435
* ```
3536
*
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
3838
* @param string $projectId (optional) Your Google Cloud Project ID
3939
*
4040
*/
41-
function analyze_sentiment_from_file($bucketName, $objectName, $projectId = null)
41+
function analyze_sentiment_from_file($gcsUri, $projectId = null)
4242
{
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());
6756
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();
6870
}
6971
}
7072
# [END language_sentiment_gcs]

0 commit comments

Comments
 (0)