Skip to content

Commit 1b85789

Browse files
authored
Merge pull request GoogleCloudPlatform#790 from gogasca/classify
[classify] Migrate to new client LanguageServiceClient in 0.16.2
2 parents 4311cff + 8e9890e commit 1b85789

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

language/src/classify_text.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
# [START language_classify_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
* Classify text (20+ words) into categories.
@@ -47,21 +49,23 @@ function classify_text($text, $projectId = null)
4749
printf('20+ words are required to classify text.' . PHP_EOL);
4850
return;
4951
}
50-
51-
// Create the Natural Language client
52-
$language = new LanguageClient([
53-
'projectId' => $projectId,
54-
]);
55-
56-
// Call the classifyText function
57-
$response = $language->classifyText($text);
58-
$categories = $response->categories();
59-
60-
// Print out information about each category
61-
foreach ($categories as $category) {
62-
printf('Category Name: %s' . PHP_EOL, $category['name']);
63-
printf('Confidence: %s' . PHP_EOL, $category['confidence']);
64-
printf(PHP_EOL);
52+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
53+
try {
54+
// Create a new Document
55+
$document = new Document();
56+
// Pass GCS URI and set document type to PLAIN_TEXT (1)
57+
$document->setContent($text)->setType(Type::PLAIN_TEXT);
58+
// Call the analyzeSentiment function
59+
$response = $languageServiceClient->classifyText($document);
60+
$categories = $response->getCategories();
61+
// Print document information
62+
foreach ($categories as $category) {
63+
printf('Category Name: %s' . PHP_EOL, $category->getName());
64+
printf('Confidence: %s' . PHP_EOL, $category->getConfidence());
65+
print(PHP_EOL);
66+
}
67+
} finally {
68+
$languageServiceClient->close();
6569
}
6670
}
6771
# [END language_classify_text]

language/src/classify_text_from_file.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
# [START language_classify_gcs]
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
* Classify text (20+ words) into categories.
@@ -36,22 +38,25 @@
3638
* @param string $projectId (optional) Your Google Cloud Project ID
3739
*/
3840

39-
function classify_text_from_file($cloud_storage_uri, $projectId = null)
41+
function classify_text_from_file($gcsUri, $projectId = null)
4042
{
41-
// Create the Natural Language client
42-
$language = new LanguageClient([
43-
'projectId' => $projectId,
44-
]);
45-
46-
// Call the classifyText function
47-
$response = $language->classifyText($cloud_storage_uri);
48-
$categories = $response->categories();
49-
50-
// Print out information about each category
51-
foreach ($categories as $category) {
52-
printf('Category Name: %s' . PHP_EOL, $category['name']);
53-
printf('Confidence: %s' . PHP_EOL, $category['confidence']);
54-
printf(PHP_EOL);
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->classifyText($document);
51+
$categories = $response->getCategories();
52+
// Print document information
53+
foreach ($categories as $category) {
54+
printf('Category Name: %s' . PHP_EOL, $category->getName());
55+
printf('Confidence: %s' . PHP_EOL, $category->getConfidence());
56+
print(PHP_EOL);
57+
}
58+
} finally {
59+
$languageServiceClient->close();
5560
}
5661
}
5762
# [END language_classify_gcs]

0 commit comments

Comments
 (0)