Skip to content

Commit f5128a4

Browse files
authored
chore: upgrade language to new sample format (GoogleCloudPlatform#1650)
1 parent 4c0e644 commit f5128a4

13 files changed

+156
-187
lines changed

language/src/analyze_all.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s TEXT\n", __FILE__);
29-
}
30-
list($_, $text) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START analyze_all]
3327
use Google\Cloud\Language\V1\AnnotateTextRequest\Features;
@@ -38,13 +32,14 @@
3832
use Google\Cloud\Language\V1\EntityMention\Type as MentionType;
3933
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
4034

41-
/** Uncomment and populate these variables in your code */
42-
// $text = 'The text to analyze.';
43-
44-
// Create the Natural Language client
45-
$languageServiceClient = new LanguageServiceClient();
35+
/**
36+
* @param string $text The text to analyze
37+
*/
38+
function analyze_all(string $text): void
39+
{
40+
// Create the Natural Language client
41+
$languageServiceClient = new LanguageServiceClient();
4642

47-
try {
4843
// Create a new Document, pass text and set type to PLAIN_TEXT
4944
$document = (new Document())
5045
->setContent($text)
@@ -105,7 +100,9 @@
105100
printf('Token part of speech: %s' . PHP_EOL, Tag::name($token->getPartOfSpeech()->getTag()));
106101
printf(PHP_EOL);
107102
}
108-
} finally {
109-
$languageServiceClient->close();
110103
}
111104
# [END analyze_all]
105+
106+
// The following 2 lines are only needed to run the samples
107+
require_once __DIR__ . '/../../testing/sample_helpers.php';
108+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

language/src/analyze_all_from_file.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s FILE\n", __FILE__);
29-
}
30-
list($_, $uri) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START analyze_all_from_file]
3327
use Google\Cloud\Language\V1\AnnotateTextRequest\Features;
@@ -38,12 +32,14 @@
3832
use Google\Cloud\Language\V1\EntityMention\Type as MentionType;
3933
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
4034

41-
/** Uncomment and populate these variables in your code */
42-
// $uri = 'The cloud storage object to analyze (gs://your-bucket-name/your-object-name)';
35+
/**
36+
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
37+
*/
38+
function analyze_all_from_file(string $uri): void
39+
{
40+
// Create the Natural Language client
41+
$languageServiceClient = new LanguageServiceClient();
4342

44-
// Create the Natural Language client
45-
$languageServiceClient = new LanguageServiceClient();
46-
try {
4743
// Create a new Document, pass GCS URI and set type to PLAIN_TEXT
4844
$document = (new Document())
4945
->setGcsContentUri($uri)
@@ -109,7 +105,9 @@
109105
printf('Token part of speech: %s' . PHP_EOL, Tag::name($token->getPartOfSpeech()->getTag()));
110106
printf(PHP_EOL);
111107
}
112-
} finally {
113-
$languageServiceClient->close();
114108
}
115109
# [END analyze_all_from_file]
110+
111+
// The following 2 lines are only needed to run the samples
112+
require_once __DIR__ . '/../../testing/sample_helpers.php';
113+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

language/src/analyze_entities.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,22 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s TEXT\n", __FILE__);
29-
}
30-
list($_, $text) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START language_entities_text]
3327
use Google\Cloud\Language\V1\Document;
3428
use Google\Cloud\Language\V1\Document\Type;
3529
use Google\Cloud\Language\V1\LanguageServiceClient;
3630
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3731

38-
/** Uncomment and populate these variables in your code */
39-
// $text = 'The text to analyze.';
32+
/**
33+
* @param string $text The text to analyze
34+
*/
35+
function analyze_entities(string $text): void
36+
{
37+
// Create the Natural Language client
38+
$languageServiceClient = new LanguageServiceClient();
4039

41-
// Create the Natural Language client
42-
$languageServiceClient = new LanguageServiceClient();
43-
try {
4440
// Create a new Document, add text as content and set type to PLAIN_TEXT
4541
$document = (new Document())
4642
->setContent($text)
@@ -62,7 +58,9 @@
6258
}
6359
printf(PHP_EOL);
6460
}
65-
} finally {
66-
$languageServiceClient->close();
6761
}
6862
# [END language_entities_text]
63+
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

language/src/analyze_entities_from_file.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,22 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s FILE\n", __FILE__);
29-
}
30-
list($_, $uri) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START language_entities_gcs]
3327
use Google\Cloud\Language\V1\Document;
3428
use Google\Cloud\Language\V1\Document\Type;
3529
use Google\Cloud\Language\V1\LanguageServiceClient;
3630
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3731

38-
/** Uncomment and populate these variables in your code */
39-
// $uri = 'The cloud storage object to analyze (gs://your-bucket-name/your-object-name)';
32+
/**
33+
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
34+
*/
35+
function analyze_entities_from_file(string $uri): void
36+
{
37+
// Create the Natural Language client
38+
$languageServiceClient = new LanguageServiceClient();
4039

41-
// Create the Natural Language client
42-
$languageServiceClient = new LanguageServiceClient();
43-
try {
4440
// Create a new Document, pass GCS URI and set type to PLAIN_TEXT
4541
$document = (new Document())
4642
->setGcsContentUri($uri)
@@ -62,7 +58,9 @@
6258
}
6359
printf(PHP_EOL);
6460
}
65-
} finally {
66-
$languageServiceClient->close();
6761
}
6862
# [END language_entities_gcs]
63+
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

language/src/analyze_entity_sentiment.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,21 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s TEXT\n", __FILE__);
29-
}
30-
list($_, $text) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START language_entity_sentiment_text]
3327
use Google\Cloud\Language\V1\Document;
3428
use Google\Cloud\Language\V1\Document\Type;
3529
use Google\Cloud\Language\V1\LanguageServiceClient;
3630
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3731

38-
/** Uncomment and populate these variables in your code */
39-
// $text = 'The text to analyze.';
32+
/**
33+
* @param string $text The text to analyze
34+
*/
35+
function analyze_entity_sentiment(string $text): void
36+
{
37+
$languageServiceClient = new LanguageServiceClient();
4038

41-
$languageServiceClient = new LanguageServiceClient();
42-
try {
4339
// Create a new Document, add text as content and set type to PLAIN_TEXT
4440
$document = (new Document())
4541
->setContent($text)
@@ -60,7 +56,9 @@
6056
}
6157
print(PHP_EOL);
6258
}
63-
} finally {
64-
$languageServiceClient->close();
6559
}
6660
# [END language_entity_sentiment_text]
61+
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

language/src/analyze_entity_sentiment_from_file.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,22 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s FILE\n", __FILE__);
29-
}
30-
list($_, $uri) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START language_entity_sentiment_gcs]
3327
use Google\Cloud\Language\V1\Document;
3428
use Google\Cloud\Language\V1\Document\Type;
3529
use Google\Cloud\Language\V1\LanguageServiceClient;
3630
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3731

38-
/** Uncomment and populate these variables in your code */
39-
// $uri = 'The cloud storage object to analyze (gs://your-bucket-name/your-object-name)';
32+
/**
33+
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
34+
*/
35+
function analyze_entity_sentiment_from_file(string $uri): void
36+
{
37+
// Create the Natural Language client
38+
$languageServiceClient = new LanguageServiceClient();
4039

41-
// Create the Natural Language client
42-
$languageServiceClient = new LanguageServiceClient();
43-
try {
4440
// Create a new Document, pass GCS URI and set type to PLAIN_TEXT
4541
$document = (new Document())
4642
->setGcsContentUri($uri)
@@ -61,7 +57,9 @@
6157
}
6258
print(PHP_EOL);
6359
}
64-
} finally {
65-
$languageServiceClient->close();
6660
}
6761
# [END language_entity_sentiment_gcs]
62+
63+
// The following 2 lines are only needed to run the samples
64+
require_once __DIR__ . '/../../testing/sample_helpers.php';
65+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

language/src/analyze_sentiment.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,20 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s TEXT\n", __FILE__);
29-
}
30-
list($_, $text) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START language_sentiment_text]
3327
use Google\Cloud\Language\V1\Document;
3428
use Google\Cloud\Language\V1\Document\Type;
3529
use Google\Cloud\Language\V1\LanguageServiceClient;
3630

37-
/** Uncomment and populate these variables in your code */
38-
// $text = 'The text to analyze.';
31+
/**
32+
* @param string $text The text to analyze
33+
*/
34+
function analyze_sentiment(string $text): void
35+
{
36+
$languageServiceClient = new LanguageServiceClient();
3937

40-
$languageServiceClient = new LanguageServiceClient();
41-
try {
4238
// Create a new Document, add text as content and set type to PLAIN_TEXT
4339
$document = (new Document())
4440
->setContent($text)
@@ -63,7 +59,9 @@
6359
}
6460
print(PHP_EOL);
6561
}
66-
} finally {
67-
$languageServiceClient->close();
6862
}
6963
# [END language_sentiment_text]
64+
65+
// The following 2 lines are only needed to run the samples
66+
require_once __DIR__ . '/../../testing/sample_helpers.php';
67+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

language/src/analyze_sentiment_from_file.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,20 @@
2121
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/language/README.md
2222
*/
2323

24-
// Include Google Cloud dependendencies using Composer
25-
require_once __DIR__ . '/../vendor/autoload.php';
26-
27-
if (count($argv) != 2) {
28-
return printf("Usage: php %s FILE\n", __FILE__);
29-
}
30-
list($_, $uri) = $argv;
24+
namespace Google\Cloud\Samples\Language;
3125

3226
# [START language_sentiment_gcs]
3327
use Google\Cloud\Language\V1\Document;
3428
use Google\Cloud\Language\V1\Document\Type;
3529
use Google\Cloud\Language\V1\LanguageServiceClient;
3630

37-
/** Uncomment and populate these variables in your code */
38-
// $uri = 'The cloud storage object to analyze (gs://your-bucket-name/your-object-name)';
31+
/**
32+
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
33+
*/
34+
function analyze_sentiment_from_file(string $uri): void
35+
{
36+
$languageServiceClient = new LanguageServiceClient();
3937

40-
$languageServiceClient = new LanguageServiceClient();
41-
try {
4238
// Create a new Document, pass GCS URI and set type to PLAIN_TEXT
4339
$document = (new Document())
4440
->setGcsContentUri($uri)
@@ -63,7 +59,9 @@
6359
}
6460
print(PHP_EOL);
6561
}
66-
} finally {
67-
$languageServiceClient->close();
6862
}
6963
# [END language_sentiment_gcs]
64+
65+
// The following 2 lines are only needed to run the samples
66+
require_once __DIR__ . '/../../testing/sample_helpers.php';
67+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)