Skip to content

Commit a8ea24a

Browse files
authored
Merge pull request GoogleCloudPlatform#793 from GoogleCloudPlatform/analyze_all
[analyze_all] Migrate to new client LanguageServiceClient in 0.16.2
2 parents b9eeb12 + 88c1bf3 commit a8ea24a

File tree

3 files changed

+195
-101
lines changed

3 files changed

+195
-101
lines changed

language/language.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
// Regex to match a Cloud Storage path as the first argument
8080
// e.g "gs://my-bucket/file_with_text.txt"
8181
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $content, $matches)) {
82-
analyze_all_from_file($matches[1], $matches[2], $projectId);
82+
analyze_all_from_file($matches[0], $projectId);
8383
} else {
8484
analyze_all($content, $projectId);
8585
}

language/src/analyze_all.php

Lines changed: 96 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
# [START analyze_all]
2525
namespace Google\Cloud\Samples\Language;
2626

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;
2830

2931
/**
3032
* Find the everything in text.
@@ -39,58 +41,107 @@
3941
function analyze_all($text, $projectId = null)
4042
{
4143
// Create the Natural Language client
42-
$language = new LanguageClient([
43-
'projectId' => $projectId,
44-
]);
44+
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4545

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+
];
5057

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);
59114
}
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);
62132
}
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()]);
68139
printf(PHP_EOL);
69140
}
70-
printf(PHP_EOL);
71-
}
72141

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();
85144
}
86145

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-
}
95146
}
96147
# [END analyze_all]

language/src/analyze_all_from_file.php

Lines changed: 98 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,80 +24,123 @@
2424
# [START analyze_all_from_file]
2525
namespace Google\Cloud\Samples\Language;
2626

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;
2930

3031
/**
3132
* Find the everything in text stored in a Cloud Storage bucket.
3233
* ```
3334
* analyze_all_from_file('my-bucket', 'file_with_text.txt');;
3435
* ```
3536
*
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
3838
* @param string $projectId (optional) Your Google Cloud Project ID
3939
*
4040
*/
41-
function analyze_all_from_file($bucketName, $objectName, $projectId = null)
41+
function analyze_all_from_file($gcsUri, $projectId = null)
4242
{
43-
// Create the Cloud Storage object
44-
$storage = new StorageClient();
45-
$bucket = $storage->bucket($bucketName);
46-
$storageObject = $bucket->object($objectName);
47-
4843
// 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+
];
5263

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+
];
5780

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);
66114
}
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);
69132
}
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()]);
75139
printf(PHP_EOL);
76140
}
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-
}
93141

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();
101144
}
102145
}
103146
# [END analyze_all_from_file]

0 commit comments

Comments
 (0)