|
24 | 24 | # [START analyze_all_from_file]
|
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\AnnotateTextRequest\Features; |
| 28 | +use Google\Cloud\Language\V1beta2\Document; |
| 29 | +use Google\Cloud\Language\V1beta2\LanguageServiceClient; |
29 | 30 |
|
30 | 31 | /**
|
31 | 32 | * Find the everything in text stored in a Cloud Storage bucket.
|
32 | 33 | * ```
|
33 | 34 | * analyze_all_from_file('my-bucket', 'file_with_text.txt');;
|
34 | 35 | * ```
|
35 | 36 | *
|
36 |
| - * @param string $bucketName The Cloud Storage bucket. |
37 |
| - * @param string $objectName The Cloud Storage object with text. |
| 37 | + * @param string $cloud_storage_uri Your Cloud Storage bucket URI |
38 | 38 | * @param string $projectId (optional) Your Google Cloud Project ID
|
39 | 39 | *
|
40 | 40 | */
|
41 |
| -function analyze_all_from_file($bucketName, $objectName, $projectId = null) |
| 41 | +function analyze_all_from_file($gcsUri, $projectId = null) |
42 | 42 | {
|
43 |
| - // Create the Cloud Storage object |
44 |
| - $storage = new StorageClient(); |
45 |
| - $bucket = $storage->bucket($bucketName); |
46 |
| - $storageObject = $bucket->object($objectName); |
47 |
| - |
48 | 43 | // Create the Natural Language client
|
49 |
| - $language = new LanguageClient([ |
50 |
| - 'projectId' => $projectId, |
51 |
| - ]); |
| 44 | + $languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]); |
| 45 | + |
| 46 | + // Entities, Mention and Tags mappings |
| 47 | + $entity_types = [ |
| 48 | + 0 => 'UNKNOWN', |
| 49 | + 1 => 'PERSON', |
| 50 | + 2 => 'LOCATION', |
| 51 | + 3 => 'ORGANIZATION', |
| 52 | + 4 => 'EVENT', |
| 53 | + 5 => 'WORK_OF_ART', |
| 54 | + 6 => 'CONSUMER_GOOD', |
| 55 | + 7 => 'OTHER', |
| 56 | + ]; |
| 57 | + |
| 58 | + $mention_type = [ |
| 59 | + 0 => 'TYPE_UNKNOWN', |
| 60 | + 1 => 'PROPER', |
| 61 | + 2 => 'COMMON', |
| 62 | + ]; |
52 | 63 |
|
53 |
| - // Call the annotateText function |
54 |
| - $annotation = $language->annotateText($storageObject, [ |
55 |
| - 'features' => ['entities', 'syntax', 'sentiment'] |
56 |
| - ]); |
| 64 | + $tag_types = [ |
| 65 | + 0 => 'UNKNOWN', |
| 66 | + 1 => 'ADJ', |
| 67 | + 2 => 'ADP', |
| 68 | + 3 => 'ADV', |
| 69 | + 4 => 'CONJ', |
| 70 | + 5 => 'DET', |
| 71 | + 6 => 'NOUN', |
| 72 | + 7 => 'NUM', |
| 73 | + 8 => 'PRON', |
| 74 | + 9 => 'PRT', |
| 75 | + 10 => 'PUNCT', |
| 76 | + 11 => 'VERB', |
| 77 | + 12 => 'X', |
| 78 | + 13 => 'AFFIX', |
| 79 | + ]; |
57 | 80 |
|
58 |
| - // Print out information about each entity |
59 |
| - $entities = $annotation->entities(); |
60 |
| - foreach ($entities as $entity) { |
61 |
| - printf('Name: %s' . PHP_EOL, $entity['name']); |
62 |
| - printf('Type: %s' . PHP_EOL, $entity['type']); |
63 |
| - printf('Salience: %s' . PHP_EOL, $entity['salience']); |
64 |
| - if (array_key_exists('wikipedia_url', $entity['metadata'])) { |
65 |
| - printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']); |
| 81 | + try { |
| 82 | + // Create a new Document |
| 83 | + $document = new Document(); |
| 84 | + // Pass GCS URI and set document type to PLAIN_TEXT |
| 85 | + $document->setGcsContentUri($gcsUri)->setType(1); |
| 86 | + // Define features we need to extract. |
| 87 | + $features = new Features(); |
| 88 | + // Set Features to extract ['entities', 'syntax', 'sentiment'] |
| 89 | + $features->setExtractEntities(true); |
| 90 | + $features->setExtractSyntax(true); |
| 91 | + $features->setExtractDocumentSentiment(true); |
| 92 | + // Collect annotations |
| 93 | + $response = $languageServiceClient->annotateText($document, $features); |
| 94 | + // Process Entities |
| 95 | + $entities = $response->getEntities(); |
| 96 | + foreach ($entities as $entity) { |
| 97 | + printf('Name: %s' . PHP_EOL, $entity->getName()); |
| 98 | + printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]); |
| 99 | + printf('Salience: %s' . PHP_EOL, $entity->getSalience()); |
| 100 | + if($entity->getMetadata()->offsetExists('wikipedia_url')) { |
| 101 | + printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url')); |
| 102 | + } |
| 103 | + if($entity->getMetadata()->offsetExists('mid')) { |
| 104 | + printf('Knowledge Graph MID: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('mid')); |
| 105 | + } |
| 106 | + printf('Mentions:' . PHP_EOL); |
| 107 | + foreach ($entity->getMentions() as $mention) { |
| 108 | + printf(' Begin Offset: %s' . PHP_EOL, $mention->getText()->getBeginOffset()); |
| 109 | + printf(' Content: %s' . PHP_EOL, $mention->getText()->getContent()); |
| 110 | + printf(' Mention Type: %s' . PHP_EOL, $mention_type[$mention->getType()]); |
| 111 | + printf(PHP_EOL); |
| 112 | + } |
| 113 | + printf(PHP_EOL); |
66 | 114 | }
|
67 |
| - if (array_key_exists('mid', $entity['metadata'])) { |
68 |
| - printf('Knowledge Graph MID: %s' . PHP_EOL, $entity['metadata']['mid']); |
| 115 | + // Process Sentiment |
| 116 | + $document_sentiment = $response->getDocumentSentiment(); |
| 117 | + // Print document information |
| 118 | + printf('Document Sentiment:' . PHP_EOL); |
| 119 | + printf(' Magnitude: %s' . PHP_EOL, $document_sentiment->getMagnitude()); |
| 120 | + printf(' Score: %s' . PHP_EOL, $document_sentiment->getScore()); |
| 121 | + printf(PHP_EOL); |
| 122 | + $sentences = $response->getSentences(); |
| 123 | + foreach ($sentences as $sentence) { |
| 124 | + printf('Sentence: %s' . PHP_EOL, $sentence->getText()->getContent()); |
| 125 | + printf('Sentence Sentiment:' . PHP_EOL); |
| 126 | + $sentiment = $sentence->getSentiment(); |
| 127 | + if ($sentiment) { |
| 128 | + printf('Entity Magnitude: %s' . PHP_EOL, $sentiment->getMagnitude()); |
| 129 | + printf('Entity Score: %s' . PHP_EOL, $sentiment->getScore()); |
| 130 | + } |
| 131 | + printf(PHP_EOL); |
69 | 132 | }
|
70 |
| - printf('Mentions:' . PHP_EOL); |
71 |
| - foreach ($entity['mentions'] as $mention) { |
72 |
| - printf(' Begin Offset: %s' . PHP_EOL, $mention['text']['beginOffset']); |
73 |
| - printf(' Content: %s' . PHP_EOL, $mention['text']['content']); |
74 |
| - printf(' Mention Type: %s' . PHP_EOL, $mention['type']); |
| 133 | + // Process Syntax |
| 134 | + $tokens = $response->getTokens(); |
| 135 | + // Print out information about each entity |
| 136 | + foreach ($tokens as $token) { |
| 137 | + printf('Token text: %s' . PHP_EOL, $token->getText()->getContent()); |
| 138 | + printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]); |
75 | 139 | printf(PHP_EOL);
|
76 | 140 | }
|
77 |
| - printf(PHP_EOL); |
78 |
| - } |
79 |
| - |
80 |
| - // Print document and sentence sentiment information |
81 |
| - $sentiment = $annotation->sentiment(); |
82 |
| - printf('Document Sentiment:' . PHP_EOL); |
83 |
| - printf(' Magnitude: %s' . PHP_EOL, $sentiment['magnitude']); |
84 |
| - printf(' Score: %s' . PHP_EOL, $sentiment['score']); |
85 |
| - printf(PHP_EOL); |
86 |
| - foreach ($annotation->sentences() as $sentence) { |
87 |
| - printf('Sentence: %s' . PHP_EOL, $sentence['text']['content']); |
88 |
| - printf('Sentence Sentiment:' . PHP_EOL); |
89 |
| - printf(' Magnitude: %s' . PHP_EOL, $sentence['sentiment']['magnitude']); |
90 |
| - printf(' Score: %s' . PHP_EOL, $sentence['sentiment']['score']); |
91 |
| - printf(PHP_EOL); |
92 |
| - } |
93 | 141 |
|
94 |
| - // Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token |
95 |
| - // to learn about more information you can extract from Token objects. |
96 |
| - $tokens = $annotation->tokens(); |
97 |
| - foreach ($tokens as $token) { |
98 |
| - printf('Token text: %s' . PHP_EOL, $token['text']['content']); |
99 |
| - printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']); |
100 |
| - printf(PHP_EOL); |
| 142 | + } finally { |
| 143 | + $languageServiceClient->close(); |
101 | 144 | }
|
102 | 145 | }
|
103 | 146 | # [END analyze_all_from_file]
|
0 commit comments