Skip to content

Commit 3057a7b

Browse files
committed
[classify] Migrate to new client LanguageServiceClient in 0.16.2
1 parent 00f6623 commit 3057a7b

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
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
@@ -290,7 +290,7 @@
290290
// Regex to match a Cloud Storage path as the first argument
291291
// e.g "gs://my-bucket/file_with_text.txt"
292292
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
293-
classify_text_from_file($content, $projectId);
293+
classify_text_from_file($matches[0], $projectId);
294294
} else {
295295
classify_text($content, $projectId);
296296
}

language/src/classify_text.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
# [START language_classify_text]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\LanguageClient;
28-
27+
use Google\Cloud\Language\V1beta2\Document;
28+
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
2929
/**
3030
* Classify text (20+ words) into categories.
3131
* ```
@@ -47,21 +47,23 @@ function classify_text($text, $projectId = null)
4747
printf('20+ words are required to classify text.' . PHP_EOL);
4848
return;
4949
}
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);
50+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
51+
try {
52+
// Create a new Document
53+
$document = new Document();
54+
// Pass GCS URI and set document type to PLAIN_TEXT
55+
$document->setContent($text)->setType(1);
56+
// Call the analyzeSentiment function
57+
$response = $languageServiceClient->classifyText($document);
58+
$categories = $response->getCategories();
59+
// Print document information
60+
foreach ($categories as $category) {
61+
printf('Category Name: %s' . PHP_EOL, $category->getName());
62+
printf('Confidence: %s' . PHP_EOL, $category->getConfidence());
63+
printf(PHP_EOL);
64+
}
65+
} finally {
66+
$languageServiceClient->close();
6567
}
6668
}
6769
# [END language_classify_text]

language/src/classify_text_from_file.php

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

2930
/**
3031
* Classify text (20+ words) into categories.
@@ -36,22 +37,25 @@
3637
* @param string $projectId (optional) Your Google Cloud Project ID
3738
*/
3839

39-
function classify_text_from_file($cloud_storage_uri, $projectId = null)
40+
function classify_text_from_file($gcsUri, $projectId = null)
4041
{
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);
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->classifyText($document);
50+
$categories = $response->getCategories();
51+
// Print document information
52+
foreach ($categories as $category) {
53+
printf('Category Name: %s' . PHP_EOL, $category->getName());
54+
printf('Confidence: %s' . PHP_EOL, $category->getConfidence());
55+
printf(PHP_EOL);
56+
}
57+
} finally {
58+
$languageServiceClient->close();
5559
}
5660
}
5761
# [END language_classify_gcs]

0 commit comments

Comments
 (0)