Skip to content

Commit 9b3b2e3

Browse files
authored
chore: upgrade language samples to new surface (#1868)
1 parent cab377b commit 9b3b2e3

13 files changed

+63
-25
lines changed

language/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-language": "^0.28.0",
3+
"google/cloud-language": "^0.30.2",
44
"google/cloud-storage": "^1.20.1"
55
}
66
}

language/src/analyze_all.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START analyze_all]
27+
use Google\Cloud\Language\V1\AnnotateTextRequest;
2728
use Google\Cloud\Language\V1\AnnotateTextRequest\Features;
29+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2830
use Google\Cloud\Language\V1\Document;
2931
use Google\Cloud\Language\V1\Document\Type;
30-
use Google\Cloud\Language\V1\LanguageServiceClient;
3132
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3233
use Google\Cloud\Language\V1\EntityMention\Type as MentionType;
3334
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
@@ -52,7 +53,10 @@ function analyze_all(string $text): void
5253
->setExtractDocumentSentiment(true);
5354

5455
// Collect annotations
55-
$response = $languageServiceClient->annotateText($document, $features);
56+
$request = (new AnnotateTextRequest())
57+
->setDocument($document)
58+
->setFeatures($features);
59+
$response = $languageServiceClient->annotateText($request);
5660
// Process Entities
5761
$entities = $response->getEntities();
5862
foreach ($entities as $entity) {

language/src/analyze_all_from_file.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START analyze_all_from_file]
27+
use Google\Cloud\Language\V1\AnnotateTextRequest;
2728
use Google\Cloud\Language\V1\AnnotateTextRequest\Features;
29+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2830
use Google\Cloud\Language\V1\Document;
2931
use Google\Cloud\Language\V1\Document\Type;
30-
use Google\Cloud\Language\V1\LanguageServiceClient;
3132
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3233
use Google\Cloud\Language\V1\EntityMention\Type as MentionType;
3334
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
@@ -52,7 +53,10 @@ function analyze_all_from_file(string $uri): void
5253
->setExtractDocumentSentiment(true);
5354

5455
// Collect annotations
55-
$response = $languageServiceClient->annotateText($document, $features);
56+
$request = (new AnnotateTextRequest())
57+
->setDocument($document)
58+
->setFeatures($features);
59+
$response = $languageServiceClient->annotateText($request);
5660

5761
// Process Entities
5862
$entities = $response->getEntities();

language/src/analyze_entities.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_entities_text]
27+
use Google\Cloud\Language\V1\AnalyzeEntitiesRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3132

3233
/**
@@ -43,7 +44,9 @@ function analyze_entities(string $text): void
4344
->setType(Type::PLAIN_TEXT);
4445

4546
// Call the analyzeEntities function
46-
$response = $languageServiceClient->analyzeEntities($document, []);
47+
$request = (new AnalyzeEntitiesRequest())
48+
->setDocument($document);
49+
$response = $languageServiceClient->analyzeEntities($request);
4750
$entities = $response->getEntities();
4851
// Print out information about each entity
4952
foreach ($entities as $entity) {

language/src/analyze_entities_from_file.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_entities_gcs]
27+
use Google\Cloud\Language\V1\AnalyzeEntitiesRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3132

3233
/**
@@ -43,7 +44,9 @@ function analyze_entities_from_file(string $uri): void
4344
->setType(Type::PLAIN_TEXT);
4445

4546
// Call the analyzeEntities function
46-
$response = $languageServiceClient->analyzeEntities($document, []);
47+
$request = (new AnalyzeEntitiesRequest())
48+
->setDocument($document);
49+
$response = $languageServiceClient->analyzeEntities($request);
4750
$entities = $response->getEntities();
4851
// Print out information about each entity
4952
foreach ($entities as $entity) {

language/src/analyze_entity_sentiment.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_entity_sentiment_text]
27+
use Google\Cloud\Language\V1\AnalyzeEntitySentimentRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3132

3233
/**
@@ -42,7 +43,9 @@ function analyze_entity_sentiment(string $text): void
4243
->setType(Type::PLAIN_TEXT);
4344

4445
// Call the analyzeEntitySentiment function
45-
$response = $languageServiceClient->analyzeEntitySentiment($document);
46+
$request = (new AnalyzeEntitySentimentRequest())
47+
->setDocument($document);
48+
$response = $languageServiceClient->analyzeEntitySentiment($request);
4649
$entities = $response->getEntities();
4750
// Print out information about each entity
4851
foreach ($entities as $entity) {

language/src/analyze_entity_sentiment_from_file.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_entity_sentiment_gcs]
27+
use Google\Cloud\Language\V1\AnalyzeEntitySentimentRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3132

3233
/**
@@ -43,7 +44,9 @@ function analyze_entity_sentiment_from_file(string $uri): void
4344
->setType(Type::PLAIN_TEXT);
4445

4546
// Call the analyzeEntitySentiment function
46-
$response = $languageServiceClient->analyzeEntitySentiment($document);
47+
$request = (new AnalyzeEntitySentimentRequest())
48+
->setDocument($document);
49+
$response = $languageServiceClient->analyzeEntitySentiment($request);
4750
$entities = $response->getEntities();
4851
// Print out information about each entity
4952
foreach ($entities as $entity) {

language/src/analyze_sentiment.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_sentiment_text]
27+
use Google\Cloud\Language\V1\AnalyzeSentimentRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031

3132
/**
3233
* @param string $text The text to analyze
@@ -41,7 +42,9 @@ function analyze_sentiment(string $text): void
4142
->setType(Type::PLAIN_TEXT);
4243

4344
// Call the analyzeSentiment function
44-
$response = $languageServiceClient->analyzeSentiment($document);
45+
$request = (new AnalyzeSentimentRequest())
46+
->setDocument($document);
47+
$response = $languageServiceClient->analyzeSentiment($request);
4548
$document_sentiment = $response->getDocumentSentiment();
4649
// Print document information
4750
printf('Document Sentiment:' . PHP_EOL);

language/src/analyze_sentiment_from_file.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_sentiment_gcs]
27+
use Google\Cloud\Language\V1\AnalyzeSentimentRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031

3132
/**
3233
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
@@ -41,7 +42,9 @@ function analyze_sentiment_from_file(string $uri): void
4142
->setType(Type::PLAIN_TEXT);
4243

4344
// Call the analyzeSentiment function
44-
$response = $languageServiceClient->analyzeSentiment($document);
45+
$request = (new AnalyzeSentimentRequest())
46+
->setDocument($document);
47+
$response = $languageServiceClient->analyzeSentiment($request);
4548
$document_sentiment = $response->getDocumentSentiment();
4649
// Print document information
4750
printf('Document Sentiment:' . PHP_EOL);

language/src/analyze_syntax.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_syntax_text]
27+
use Google\Cloud\Language\V1\AnalyzeSyntaxRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
3132

3233
/**
@@ -43,7 +44,9 @@ function analyze_syntax(string $text): void
4344
->setType(Type::PLAIN_TEXT);
4445

4546
// Call the analyzeEntities function
46-
$response = $languageServiceClient->analyzeSyntax($document, []);
47+
$request = (new AnalyzeSyntaxRequest())
48+
->setDocument($document);
49+
$response = $languageServiceClient->analyzeSyntax($request);
4750
$tokens = $response->getTokens();
4851
// Print out information about each entity
4952
foreach ($tokens as $token) {

language/src/analyze_syntax_from_file.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_syntax_gcs]
27+
use Google\Cloud\Language\V1\AnalyzeSyntaxRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
3132

3233
/**
@@ -43,7 +44,9 @@ function analyze_syntax_from_file(string $uri): void
4344
->setType(Type::PLAIN_TEXT);
4445

4546
// Call the analyzeEntities function
46-
$response = $languageServiceClient->analyzeSyntax($document, []);
47+
$request = (new AnalyzeSyntaxRequest())
48+
->setDocument($document);
49+
$response = $languageServiceClient->analyzeSyntax($request);
4750
$tokens = $response->getTokens();
4851
// Print out information about each entity
4952
foreach ($tokens as $token) {

language/src/classify_text.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_classify_text]
27+
use Google\Cloud\Language\V1\ClassifyTextRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031

3132
/**
3233
* @param string $text The text to analyze
@@ -46,7 +47,9 @@ function classify_text(string $text): void
4647
->setType(Type::PLAIN_TEXT);
4748

4849
// Call the analyzeSentiment function
49-
$response = $languageServiceClient->classifyText($document);
50+
$request = (new ClassifyTextRequest())
51+
->setDocument($document);
52+
$response = $languageServiceClient->classifyText($request);
5053
$categories = $response->getCategories();
5154
// Print document information
5255
foreach ($categories as $category) {

language/src/classify_text_from_file.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
namespace Google\Cloud\Samples\Language;
2525

2626
# [START language_classify_gcs]
27+
use Google\Cloud\Language\V1\ClassifyTextRequest;
28+
use Google\Cloud\Language\V1\Client\LanguageServiceClient;
2729
use Google\Cloud\Language\V1\Document;
2830
use Google\Cloud\Language\V1\Document\Type;
29-
use Google\Cloud\Language\V1\LanguageServiceClient;
3031

3132
/**
3233
* @param string $uri The cloud storage object to analyze (gs://your-bucket-name/your-object-name)
@@ -41,7 +42,9 @@ function classify_text_from_file(string $uri): void
4142
->setType(Type::PLAIN_TEXT);
4243

4344
// Call the analyzeSentiment function
44-
$response = $languageServiceClient->classifyText($document);
45+
$request = (new ClassifyTextRequest())
46+
->setDocument($document);
47+
$response = $languageServiceClient->classifyText($request);
4548
$categories = $response->getCategories();
4649
// Print document information
4750
foreach ($categories as $category) {

0 commit comments

Comments
 (0)