2424# [START analyze_all_from_file]
2525namespace 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 {
0 commit comments