Skip to content

Commit 07da022

Browse files
authored
Fixes and updates language samples (GoogleCloudPlatform#872)
1 parent fdd9acb commit 07da022

13 files changed

+59
-190
lines changed

language/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-language": "~0.16.2",
3+
"google/cloud-language": "~0.18",
44
"google/cloud-storage": "^1.3",
55
"symfony/console": "^3.0"
66
},

language/src/analyze_all.php

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

27-
use Google\Cloud\Language\V1beta2\AnnotateTextRequest\Features;
28-
use Google\Cloud\Language\V1beta2\Document;
29-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\AnnotateTextRequest\Features;
28+
use Google\Cloud\Language\V1\Document;
29+
use Google\Cloud\Language\V1\LanguageServiceClient;
30+
use Google\Cloud\Language\V1\Entity\Type as EntityType;
31+
use Google\Cloud\Language\V1\EntityMention\Type as MentionType;
32+
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
3033

3134
/**
3235
* Find the everything in text.
@@ -43,41 +46,6 @@ function analyze_all($text, $projectId = null)
4346
// Create the Natural Language client
4447
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4548

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-
];
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-
8149
try {
8250
// Create a new Document
8351
$document = new Document();
@@ -95,7 +63,7 @@ function analyze_all($text, $projectId = null)
9563
$entities = $response->getEntities();
9664
foreach ($entities as $entity) {
9765
printf('Name: %s' . PHP_EOL, $entity->getName());
98-
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
66+
printf('Type: %s' . PHP_EOL, EntityType::name($entity->getType()));
9967
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
10068
if ($entity->getMetadata()->offsetExists('wikipedia_url')) {
10169
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
@@ -107,7 +75,7 @@ function analyze_all($text, $projectId = null)
10775
foreach ($entity->getMentions() as $mention) {
10876
printf(' Begin Offset: %s' . PHP_EOL, $mention->getText()->getBeginOffset());
10977
printf(' Content: %s' . PHP_EOL, $mention->getText()->getContent());
110-
printf(' Mention Type: %s' . PHP_EOL, $mention_type[$mention->getType()]);
78+
printf(' Mention Type: %s' . PHP_EOL, MentionType::name($mention->getType()));
11179
printf(PHP_EOL);
11280
}
11381
printf(PHP_EOL);
@@ -135,7 +103,7 @@ function analyze_all($text, $projectId = null)
135103
// Print out information about each entity
136104
foreach ($tokens as $token) {
137105
printf('Token text: %s' . PHP_EOL, $token->getText()->getContent());
138-
printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]);
106+
printf('Token part of speech: %s' . PHP_EOL, Tag::name($token->getPartOfSpeech()->getTag()));
139107
printf(PHP_EOL);
140108
}
141109
} finally {

language/src/analyze_all_from_file.php

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

27-
use Google\Cloud\Language\V1beta2\AnnotateTextRequest\Features;
28-
use Google\Cloud\Language\V1beta2\Document;
29-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\AnnotateTextRequest\Features;
28+
use Google\Cloud\Language\V1\Document;
29+
use Google\Cloud\Language\V1\LanguageServiceClient;
30+
use Google\Cloud\Language\V1\Entity\Type as EntityType;
31+
use Google\Cloud\Language\V1\EntityMention\Type as MentionType;
32+
use Google\Cloud\Language\V1\PartOfSpeech\Tag;
3033

3134
/**
3235
* Find the everything in text stored in a Cloud Storage bucket.
@@ -42,42 +45,6 @@ function analyze_all_from_file($gcsUri, $projectId = null)
4245
{
4346
// Create the Natural Language client
4447
$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-
];
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-
8148
try {
8249
// Create a new Document
8350
$document = new Document();
@@ -95,7 +62,7 @@ function analyze_all_from_file($gcsUri, $projectId = null)
9562
$entities = $response->getEntities();
9663
foreach ($entities as $entity) {
9764
printf('Name: %s' . PHP_EOL, $entity->getName());
98-
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
65+
printf('Type: %s' . PHP_EOL, EntityType::name($entity->getType()));
9966
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
10067
if ($entity->getMetadata()->offsetExists('wikipedia_url')) {
10168
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));
@@ -107,7 +74,7 @@ function analyze_all_from_file($gcsUri, $projectId = null)
10774
foreach ($entity->getMentions() as $mention) {
10875
printf(' Begin Offset: %s' . PHP_EOL, $mention->getText()->getBeginOffset());
10976
printf(' Content: %s' . PHP_EOL, $mention->getText()->getContent());
110-
printf(' Mention Type: %s' . PHP_EOL, $mention_type[$mention->getType()]);
77+
printf(' Mention Type: %s' . PHP_EOL, MentionType::name($mention->getType()));
11178
printf(PHP_EOL);
11279
}
11380
printf(PHP_EOL);
@@ -135,7 +102,7 @@ function analyze_all_from_file($gcsUri, $projectId = null)
135102
// Print out information about each entity
136103
foreach ($tokens as $token) {
137104
printf('Token text: %s' . PHP_EOL, $token->getText()->getContent());
138-
printf('Token part of speech: %s' . PHP_EOL, $tag_types[$token->getPartOfSpeech()->getTag()]);
105+
printf('Token part of speech: %s' . PHP_EOL, Tag::name($token->getPartOfSpeech()->getTag()));
139106
printf(PHP_EOL);
140107
}
141108
} finally {

language/src/analyze_entities.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
# [START language_entities_text]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\V1beta2\Document;
28-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\Document;
28+
use Google\Cloud\Language\V1\LanguageServiceClient;
29+
use Google\Cloud\Language\V1\Entity\Type as EntityType;
2930

3031
/**
3132
* Find the entities in text.
@@ -42,16 +43,6 @@ function analyze_entities($text, $projectId = null)
4243
// Create the Natural Language client
4344
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4445
try {
45-
$entity_types = [
46-
0 => 'UNKNOWN',
47-
1 => 'PERSON',
48-
2 => 'LOCATION',
49-
3 => 'ORGANIZATION',
50-
4 => 'EVENT',
51-
5 => 'WORK_OF_ART',
52-
6 => 'CONSUMER_GOOD',
53-
7 => 'OTHER',
54-
];
5546
$document = new Document();
5647
// Add text as content and set document type to PLAIN_TEXT
5748
$document->setContent($text)->setType(1);
@@ -61,7 +52,7 @@ function analyze_entities($text, $projectId = null)
6152
// Print out information about each entity
6253
foreach ($entities as $entity) {
6354
printf('Name: %s' . PHP_EOL, $entity->getName());
64-
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
55+
printf('Type: %s' . PHP_EOL, EntityType::name($entity->getType()));
6556
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
6657
if ($entity->getMetadata()->offsetExists('wikipedia_url')) {
6758
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));

language/src/analyze_entities_from_file.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
# [START language_entities_gcs]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\V1beta2\Document;
28-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\Document;
28+
use Google\Cloud\Language\V1\LanguageServiceClient;
29+
use Google\Cloud\Language\V1\Entity\Type;
2930

3031
/**
3132
* Find the entities in text stored in a Cloud Storage bucket.
@@ -42,16 +43,6 @@ function analyze_entities_from_file($gcsUri, $projectId = null)
4243
// Create the Natural Language client
4344
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4445
try {
45-
$entity_types = [
46-
0 => 'UNKNOWN',
47-
1 => 'PERSON',
48-
2 => 'LOCATION',
49-
3 => 'ORGANIZATION',
50-
4 => 'EVENT',
51-
5 => 'WORK_OF_ART',
52-
6 => 'CONSUMER_GOOD',
53-
7 => 'OTHER',
54-
];
5546
$document = new Document();
5647
// Pass GCS URI and set document type to PLAIN_TEXT
5748
$document->setGcsContentUri($gcsUri)->setType(1);
@@ -61,7 +52,7 @@ function analyze_entities_from_file($gcsUri, $projectId = null)
6152
// Print out information about each entity
6253
foreach ($entities as $entity) {
6354
printf('Name: %s' . PHP_EOL, $entity->getName());
64-
printf('Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
55+
printf('Type: %s' . PHP_EOL, Type::name($entity->getType()));
6556
printf('Salience: %s' . PHP_EOL, $entity->getSalience());
6657
if ($entity->getMetadata()->offsetExists('wikipedia_url')) {
6758
printf('Wikipedia URL: %s' . PHP_EOL, $entity->getMetadata()->offsetGet('wikipedia_url'));

language/src/analyze_entity_sentiment.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
# [START language_entity_sentiment_text]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\V1beta2\Document;
28-
use Google\Cloud\Language\V1beta2\Document\Type;
29-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\Document;
28+
use Google\Cloud\Language\V1\Document\Type;
29+
use Google\Cloud\Language\V1\LanguageServiceClient;
30+
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3031

3132
/**
3233
* Find the entities in text.
@@ -43,16 +44,6 @@ function analyze_entity_sentiment($text, $projectId = null)
4344
{
4445
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4546
try {
46-
$entity_types = [
47-
0 => 'UNKNOWN',
48-
1 => 'PERSON',
49-
2 => 'LOCATION',
50-
3 => 'ORGANIZATION',
51-
4 => 'EVENT',
52-
5 => 'WORK_OF_ART',
53-
6 => 'CONSUMER_GOOD',
54-
7 => 'OTHER',
55-
];
5647
// Create a new Document
5748
$document = new Document();
5849
// Add text as content and set document type to PLAIN_TEXT
@@ -63,7 +54,7 @@ function analyze_entity_sentiment($text, $projectId = null)
6354
// Print out information about each entity
6455
foreach ($entities as $entity) {
6556
printf('Entity Name: %s' . PHP_EOL, $entity->getName());
66-
printf('Entity Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
57+
printf('Entity Type: %s' . PHP_EOL, EntityType::name($entity->getType()));
6758
printf('Entity Salience: %s' . PHP_EOL, $entity->getSalience());
6859
$sentiment = $entity->getSentiment();
6960
if ($sentiment) {

language/src/analyze_entity_sentiment_from_file.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
# [START language_entity_sentiment_gcs]
2525
namespace Google\Cloud\Samples\Language;
2626

27-
use Google\Cloud\Language\V1beta2\Document;
28-
use Google\Cloud\Language\V1beta2\Document\Type;
29-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\Document;
28+
use Google\Cloud\Language\V1\Document\Type;
29+
use Google\Cloud\Language\V1\LanguageServiceClient;
30+
use Google\Cloud\Language\V1\Entity\Type as EntityType;
3031

3132
/**
3233
* Find the entities in text.
@@ -44,16 +45,6 @@ function analyze_entity_sentiment_from_file($gcsUri, $projectId = null)
4445
// Create the Natural Language client
4546
$languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
4647
try {
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-
];
5748
// Create a new Document
5849
$document = new Document();
5950
// Pass GCS URI and set document type to PLAIN_TEXT
@@ -64,7 +55,7 @@ function analyze_entity_sentiment_from_file($gcsUri, $projectId = null)
6455
// Print out information about each entity
6556
foreach ($entities as $entity) {
6657
printf('Entity Name: %s' . PHP_EOL, $entity->getName());
67-
printf('Entity Type: %s' . PHP_EOL, $entity_types[$entity->getType()]);
58+
printf('Entity Type: %s' . PHP_EOL, EntityType::name($entity->getType()));
6859
printf('Entity Salience: %s' . PHP_EOL, $entity->getSalience());
6960
$sentiment = $entity->getSentiment();
7061
if ($sentiment) {

language/src/analyze_sentiment.php

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

27-
use Google\Cloud\Language\V1beta2\Document;
28-
use Google\Cloud\Language\V1beta2\Document\Type;
29-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\Document;
28+
use Google\Cloud\Language\V1\Document\Type;
29+
use Google\Cloud\Language\V1\LanguageServiceClient;
3030

3131
/**
3232
* Find the sentiment in text.

language/src/analyze_sentiment_from_file.php

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

27-
use Google\Cloud\Language\V1beta2\Document;
28-
use Google\Cloud\Language\V1beta2\Document\Type;
29-
use Google\Cloud\Language\V1beta2\LanguageServiceClient;
27+
use Google\Cloud\Language\V1\Document;
28+
use Google\Cloud\Language\V1\Document\Type;
29+
use Google\Cloud\Language\V1\LanguageServiceClient;
3030

3131
/**
3232
* Find the sentiment in text stored in a Cloud Storage bucket.

0 commit comments

Comments
 (0)