|
24 | 24 | # [START language_classify_gcs]
|
25 | 25 | namespace Google\Cloud\Samples\Language;
|
26 | 26 |
|
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; |
28 | 30 |
|
29 | 31 | /**
|
30 | 32 | * Classify text (20+ words) into categories.
|
|
36 | 38 | * @param string $projectId (optional) Your Google Cloud Project ID
|
37 | 39 | */
|
38 | 40 |
|
39 |
| -function classify_text_from_file($cloud_storage_uri, $projectId = null) |
| 41 | +function classify_text_from_file($gcsUri, $projectId = null) |
40 | 42 | {
|
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(); |
55 | 60 | }
|
56 | 61 | }
|
57 | 62 | # [END language_classify_gcs]
|
0 commit comments