Skip to content

Commit cf18d2d

Browse files
committed
[sentiment] Migrate to new client LanguageServiceClient in 0.16.2
Migrate client and tests to new Client version
1 parent 00f6623 commit cf18d2d

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

language/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-language": "~0.12",
3+
"google/cloud-language": "~0.16.2",
44
"google/cloud-storage": "^1.3",
55
"symfony/console": "^3.0"
66
},

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($matches[0], $projectId);
172172
} else {
173173
analyze_sentiment($content, $projectId);
174174
}

language/src/analyze_sentiment.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
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\LanguageServiceClient;
2829

2930
/**
3031
* Find the sentiment in text.
@@ -38,26 +39,33 @@
3839
*/
3940
function analyze_sentiment($text, $projectId = null)
4041
{
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']);
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->setContent($text)->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());
6055
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();
6169
}
6270
}
6371
# [END language_sentiment_text]

language/src/analyze_sentiment_from_file.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,48 @@
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\LanguageServiceClient;
2929

3030
/**
3131
* Find the sentiment in text stored in a Cloud Storage bucket.
3232
* ```
3333
* analyze_sentiment_from_file('my-bucket', 'file_with_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 Your Cloud Storage bucket URI
3837
* @param string $projectId (optional) Your Google Cloud Project ID
3938
*
4039
*/
41-
function analyze_sentiment_from_file($bucketName, $objectName, $projectId = null)
40+
function analyze_sentiment_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-
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());
6755
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();
6869
}
6970
}
7071
# [END language_sentiment_gcs]

0 commit comments

Comments
 (0)