|
24 | 24 | # [START language_syntax_gcs]
|
25 | 25 | namespace Google\Cloud\Samples\Language;
|
26 | 26 |
|
27 |
| -use Google\Cloud\Language\LanguageClient; |
28 |
| -use Google\Cloud\Storage\StorageClient; |
| 27 | +use Google\Cloud\Language\V1beta2\Document; |
| 28 | +use Google\Cloud\Language\V1beta2\LanguageServiceClient; |
29 | 29 |
|
30 | 30 | /**
|
31 | 31 | * Find the syntax in text stored in a Cloud Storage bucket.
|
32 | 32 | * ```
|
33 | 33 | * analyze_syntax_from_file('my-bucket', 'file_with_text.txt');
|
34 | 34 | * ```
|
35 | 35 | *
|
36 |
| - * @param string $bucketName The Cloud Storage bucket. |
37 |
| - * @param string $objectName The Cloud Storage object with text. |
| 36 | + * @param string $gcsUri The Cloud Storage path with text. |
38 | 37 | * @param string $projectId (optional) Your Google Cloud Project ID
|
39 | 38 | *
|
40 | 39 | */
|
41 |
| -function analyze_syntax_from_file($bucketName, $objectName, $projectId = null) |
| 40 | +function analyze_syntax_from_file($gcsUri, $projectId = null) |
42 | 41 | {
|
43 |
| - // Create the Cloud Storage object |
44 |
| - $storage = new StorageClient(); |
45 |
| - $bucket = $storage->bucket($bucketName); |
46 |
| - $storageObject = $bucket->object($objectName); |
47 |
| - |
48 | 42 | // Create the Natural Language client
|
49 |
| - $language = new LanguageClient([ |
50 |
| - 'projectId' => $projectId, |
51 |
| - ]); |
52 |
| - |
53 |
| - // Call the analyzeSyntax function |
54 |
| - $annotation = $language->analyzeSyntax($storageObject); |
| 43 | + $languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]); |
55 | 44 |
|
56 |
| - // Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token |
57 |
| - // to learn about more information you can extract from Token objects. |
58 |
| - $tokens = $annotation->tokens(); |
59 |
| - foreach ($tokens as $token) { |
60 |
| - printf('Token text: %s' . PHP_EOL, $token['text']['content']); |
61 |
| - printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']); |
62 |
| - printf(PHP_EOL); |
| 45 | + try { |
| 46 | + $tag_types = [ |
| 47 | + 0 => 'UNKNOWN', |
| 48 | + 1 => 'ADJ', |
| 49 | + 2 => 'ADP', |
| 50 | + 3 => 'ADV', |
| 51 | + 4 => 'CONJ', |
| 52 | + 5 => 'DET', |
| 53 | + 6 => 'NOUN', |
| 54 | + 7 => 'NUM', |
| 55 | + 8 => 'PRON', |
| 56 | + 9 => 'PRT', |
| 57 | + 10 => 'PUNCT', |
| 58 | + 11 => 'VERB', |
| 59 | + 12 => 'X', |
| 60 | + 13 => 'AFFIX', |
| 61 | + ]; |
| 62 | + // Create a new Document |
| 63 | + $document = new Document(); |
| 64 | + // Pass GCS URI and set document type to PLAIN_TEXT |
| 65 | + $document->setGcsContentUri($gcsUri)->setType(1); |
| 66 | + // Call the analyzeEntities function |
| 67 | + $response = $languageServiceClient->analyzeSyntax($document, []); |
| 68 | + $tokens = $response->getTokens(); |
| 69 | + // Print out information about each entity |
| 70 | + foreach ($tokens as $token) { |
| 71 | + printf('Token text: %s' . PHP_EOL, $token->getText()->getContent()); |
| 72 | + printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]); |
| 73 | + printf(PHP_EOL); |
| 74 | + } |
| 75 | + } finally { |
| 76 | + $languageServiceClient->close(); |
63 | 77 | }
|
64 | 78 | }
|
65 | 79 | # [END language_syntax_gcs]
|
0 commit comments