|
18 | 18 | namespace Google\Cloud\Samples\Language; |
19 | 19 |
|
20 | 20 | use Google\Cloud\NaturalLanguage\Annotation; |
| 21 | +use Symfony\Component\Yaml\Yaml; |
21 | 22 |
|
22 | 23 | /** |
23 | 24 | * Convert an Annotation to a string. |
|
27 | 28 | */ |
28 | 29 | function annotation_to_string(Annotation $annotation) |
29 | 30 | { |
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); |
72 | 32 | } |
0 commit comments