|
24 | 24 | # [START analyze_all]
|
25 | 25 | namespace Google\Cloud\Samples\Language;
|
26 | 26 |
|
27 |
| -use Google\Cloud\Language\LanguageClient; |
| 27 | +use Google\Cloud\Language\V1beta2\AnnotateTextRequest\Features; |
| 28 | +use Google\Cloud\Language\V1beta2\Document; |
| 29 | +use Google\Cloud\Language\V1beta2\LanguageServiceClient; |
28 | 30 |
|
29 | 31 | /**
|
30 | 32 | * Find the everything in text.
|
|
39 | 41 | function analyze_all($text, $projectId = null)
|
40 | 42 | {
|
41 | 43 | // Create the Natural Language client
|
42 |
| - $language = new LanguageClient([ |
43 |
| - 'projectId' => $projectId, |
44 |
| - ]); |
| 44 | + $languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]); |
45 | 45 |
|
46 |
| - // Call the annotateText function |
47 |
| - $annotation = $language->annotateText($text, [ |
48 |
| - 'features' => ['entities', 'syntax', 'sentiment'] |
49 |
| - ]); |
| 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 | + ]; |
50 | 57 |
|
51 |
| - // Print out information about each entity |
52 |
| - $entities = $annotation->entities(); |
53 |
| - foreach ($entities as $entity) { |
54 |
| - printf('Name: %s' . PHP_EOL, $entity['name']); |
55 |
| - printf('Type: %s' . PHP_EOL, $entity['type']); |
56 |
| - printf('Salience: %s' . PHP_EOL, $entity['salience']); |
57 |
| - if (array_key_exists('wikipedia_url', $entity['metadata'])) { |
58 |
| - printf('Wikipedia URL: %s' . PHP_EOL, $entity['metadata']['wikipedia_url']); |
| 58 | + $mention_type = [ |
| 59 | + 0 => 'TYPE_UNKNOWN', |
| 60 | + 1 => 'PROPER', |
| 61 | + 2 => 'COMMON', |
| 62 | + ]; |
| 63 | + |
| 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 | + ]; |
| 80 | + |
| 81 | + try { |
| 82 | + // Create a new Document |
| 83 | + $document = new Document(); |
| 84 | + // Pass GCS URI and set document type to PLAIN_TEXT |
| 85 | + $document->setContent($text)->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); |
59 | 114 | }
|
60 |
| - if (array_key_exists('mid', $entity['metadata'])) { |
61 |
| - 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); |
62 | 132 | }
|
63 |
| - printf('Mentions:' . PHP_EOL); |
64 |
| - foreach ($entity['mentions'] as $mention) { |
65 |
| - printf(' Begin Offset: %s' . PHP_EOL, $mention['text']['beginOffset']); |
66 |
| - printf(' Content: %s' . PHP_EOL, $mention['text']['content']); |
67 |
| - 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()]); |
68 | 139 | printf(PHP_EOL);
|
69 | 140 | }
|
70 |
| - printf(PHP_EOL); |
71 |
| - } |
72 | 141 |
|
73 |
| - // Print document and sentence sentiment information |
74 |
| - $sentiment = $annotation->sentiment(); |
75 |
| - printf('Document Sentiment:' . PHP_EOL); |
76 |
| - printf(' Magnitude: %s' . PHP_EOL, $sentiment['magnitude']); |
77 |
| - printf(' Score: %s' . PHP_EOL, $sentiment['score']); |
78 |
| - printf(PHP_EOL); |
79 |
| - foreach ($annotation->sentences() as $sentence) { |
80 |
| - printf('Sentence: %s' . PHP_EOL, $sentence['text']['content']); |
81 |
| - printf('Sentence Sentiment:' . PHP_EOL); |
82 |
| - printf(' Magnitude: %s' . PHP_EOL, $sentence['sentiment']['magnitude']); |
83 |
| - printf(' Score: %s' . PHP_EOL, $sentence['sentiment']['score']); |
84 |
| - printf(PHP_EOL); |
| 142 | + } finally { |
| 143 | + $languageServiceClient->close(); |
85 | 144 | }
|
86 | 145 |
|
87 |
| - // Print syntax information. See https://cloud.google.com/natural-language/docs/reference/rest/v1/Token |
88 |
| - // to learn about more information you can extract from Token objects. |
89 |
| - $tokens = $annotation->tokens(); |
90 |
| - foreach ($tokens as $token) { |
91 |
| - printf('Token text: %s' . PHP_EOL, $token['text']['content']); |
92 |
| - printf('Token part of speech: %s' . PHP_EOL, $token['partOfSpeech']['tag']); |
93 |
| - printf(PHP_EOL); |
94 |
| - } |
95 | 146 | }
|
96 | 147 | # [END analyze_all]
|
0 commit comments