Skip to content

Commit b28c94d

Browse files
SurferJeffAtGooglebshaffer
authored andcommitted
Update language samples to dump all data returned from server. (GoogleCloudPlatform#368)
1 parent 50e38a0 commit b28c94d

File tree

10 files changed

+175
-122
lines changed

10 files changed

+175
-122
lines changed

language/api/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"require": {
33
"google/cloud-language": "^0.2",
44
"symfony/console": "^3.0",
5-
"google/cloud-storage": "^1.0"
5+
"google/cloud-storage": "^1.0",
6+
"symfony/yaml": "^3.2"
67
},
78
"require-dev": {
89
"phpunit/phpunit": "~4.0"

language/api/composer.lock

Lines changed: 56 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

language/api/src/AllCommand.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,48 @@ protected function configure()
4141
4242
php %command.full_name% gs://my_bucket/file_with_text.txt
4343
44+
Example:
45+
php %command.full_name% "John took a walk."
46+
sentences:
47+
-
48+
text:
49+
content: 'John took a walk.'
50+
beginOffset: 0
51+
sentiment:
52+
magnitude: 0
53+
score: 0
54+
tokens:
55+
-
56+
text:
57+
content: John
58+
beginOffset: 0
59+
partOfSpeech:
60+
tag: NOUN
61+
aspect: ASPECT_UNKNOWN
62+
case: CASE_UNKNOWN
63+
...
64+
dependencyEdge:
65+
headTokenIndex: 1
66+
label: NSUBJ
67+
lemma: John
68+
...
69+
entities:
70+
-
71+
name: John
72+
type: PERSON
73+
metadata: { }
74+
salience: 0.67526394
75+
mentions:
76+
-
77+
text:
78+
content: John
79+
beginOffset: 0
80+
type: PROPER
81+
...
82+
documentSentiment:
83+
magnitude: 0
84+
score: 0
85+
language: en
4486
EOF
4587
)
4688
->addArgument(

language/api/src/EntitiesCommand.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ protected function configure()
4141
4242
php %command.full_name% gs://my_bucket/file_with_text.txt
4343
44+
Example:
45+
php %command.full_name% "John took a walk."
46+
entities:
47+
-
48+
name: John
49+
type: PERSON
50+
metadata: { }
51+
salience: 0.67526394
52+
mentions:
53+
-
54+
text:
55+
content: John
56+
beginOffset: 0
57+
type: PROPER
58+
-
59+
name: walk
60+
type: EVENT
61+
metadata: { }
62+
salience: 0.3247361
63+
mentions:
64+
-
65+
text:
66+
content: walk
67+
beginOffset: 12
68+
type: COMMON
69+
language: en
4470
EOF
4571
)
4672
->addArgument(

language/api/src/SentimentCommand.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ protected function configure()
4141
4242
php %command.full_name% gs://my_bucket/file_with_text.txt
4343
44+
Example:
45+
php %command.full_name% "John took a walk."
46+
documentSentiment:
47+
magnitude: 0
48+
score: 0
49+
language: en
50+
sentences:
51+
-
52+
text:
53+
content: 'John took a walk.'
54+
beginOffset: 0
55+
sentiment:
56+
magnitude: 0
57+
score: 0
4458
EOF
4559
)
4660
->addArgument(

language/api/src/SyntaxCommand.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,33 @@ protected function configure()
4040
php %command.full_name% Text to analyze.
4141
4242
php %command.full_name% gs://my_bucket/file_with_text.txt
43-
43+
44+
Example:
45+
php %command.full_name% "John took a walk."
46+
sentences:
47+
-
48+
text:
49+
content: 'John took a walk.'
50+
beginOffset: 0
51+
tokens:
52+
-
53+
text:
54+
content: John
55+
beginOffset: 0
56+
partOfSpeech:
57+
tag: NOUN
58+
aspect: ASPECT_UNKNOWN
59+
case: CASE_UNKNOWN
60+
form: FORM_UNKNOWN
61+
...
62+
dependencyEdge:
63+
headTokenIndex: 1
64+
label: NSUBJ
65+
lemma: John
66+
-
67+
...
68+
language: en
69+
entities: { }
4470
EOF
4571
)
4672
->addArgument(

language/api/src/functions/annotation_to_string.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Google\Cloud\Samples\Language;
1919

2020
use Google\Cloud\NaturalLanguage\Annotation;
21+
use Symfony\Component\Yaml\Yaml;
2122

2223
/**
2324
* Convert an Annotation to a string.
@@ -27,46 +28,5 @@
2728
*/
2829
function annotation_to_string(Annotation $annotation)
2930
{
30-
$ret = '';
31-
$info = $annotation->info();
32-
if (isset($info['language'])) {
33-
$ret .= sprintf('language: %s' . PHP_EOL, $info['language']);
34-
}
35-
36-
if (isset($info['documentSentiment'])) {
37-
$magnitude = $info['documentSentiment']['magnitude'];
38-
$score = $info['documentSentiment']['score'];
39-
$ret .= sprintf('sentiment magnitude: %s score: %s' . PHP_EOL,
40-
$magnitude, $score);
41-
}
42-
43-
if (isset($info['sentences'])) {
44-
$ret .= 'sentences:' . PHP_EOL;
45-
foreach ($info['sentences'] as $sentence) {
46-
$ret .= sprintf(' %s: %s' . PHP_EOL,
47-
$sentence['text']['beginOffset'],
48-
$sentence['text']['content']);
49-
}
50-
}
51-
52-
if (isset($info['tokens'])) {
53-
$ret .= 'tokens:' . PHP_EOL;
54-
foreach ($info['tokens'] as $token) {
55-
$ret .= sprintf(' %s: %s' . PHP_EOL,
56-
$token['lemma'],
57-
$token['partOfSpeech']['tag']);
58-
}
59-
}
60-
61-
if (isset($info['entities'])) {
62-
$ret .= 'entities:' . PHP_EOL;
63-
foreach ($info['entities'] as $entity) {
64-
$ret .= sprintf(' %s', $entity['name']);
65-
if (isset($entity['metadata']['wikipedia_url'])) {
66-
$ret .= sprintf(': %s', $entity['metadata']['wikipedia_url']);
67-
}
68-
$ret .= PHP_EOL;
69-
}
70-
}
71-
return $ret;
31+
return Yaml::dump($annotation->info(), 20, 2);
7232
}

language/api/test/AllCommandTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,8 @@ public function setUp()
4747
$application->add(new AllCommand());
4848
$this->commandTester = new CommandTester($application->get('all'));
4949
$this->expectedPatterns = array(
50-
'/language: en/',
51-
'/sentiment/',
52-
'/sentences:/',
53-
'/0: Do you know the way to San Jose\\?/',
54-
'/tokens:/',
55-
'/Do: VERB/',
56-
'/you: PRON/',
57-
'/know: VERB/',
58-
'/the: DET/',
59-
'/way: NOUN/',
60-
'/to: ADP/',
61-
'/San: NOUN/',
62-
'/Jose: NOUN/',
63-
'/\\?: PUNCT/',
64-
'/entities:/',
65-
'/San Jose: http:\\/\\/en.wikipedia.org\\/wiki\\/San_Jose,'
66-
. '_California/',
50+
'/content: know/',
51+
'/http:\/\/en.wikipedia.org\/wiki\/San_Jose,_California/'
6752
);
6853
}
6954

language/api/test/EntitiesCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testEntities()
5757
);
5858

5959
$output = $this->commandTester->getDisplay();
60-
$this->assertRegExp('/San Jose: http:\/\/en.wikipedia.org/', $output);
60+
$this->assertRegExp('/http:\/\/en.wikipedia.org/', $output);
6161
}
6262

6363
public function testEntitiesFromStorageObject()
@@ -75,6 +75,6 @@ public function testEntitiesFromStorageObject()
7575
);
7676

7777
$output = $this->commandTester->getDisplay();
78-
$this->assertRegExp('/San Jose: http:\/\/en.wikipedia.org/', $output);
78+
$this->assertRegExp('/http:\/\/en.wikipedia.org/', $output);
7979
}
8080
}

0 commit comments

Comments
 (0)