1717
1818namespace Google \Cloud \Language \Tests \System ;
1919
20+ use Google \Cloud \Language \V2 \AnalyzeEntitiesRequest ;
21+ use Google \Cloud \Language \V2 \AnalyzeSentimentRequest ;
22+ use Google \Cloud \Language \V2 \ClassifyTextRequest ;
23+ use Google \Cloud \Language \V2 \Document ;
24+ use Google \Cloud \Language \V2 \EncodingType ;
25+ use Google \Cloud \Language \V2 \Entity ;
26+
2027/**
2128 * @group language
2229 */
2330class AnalyzeTest extends LanguageTestCase
2431{
25- /**
26- * @dataProvider analyzeSyntaxProvider
27- */
28- public function testAnalyzeSyntax ($ text , $ expectedValues )
29- {
30- $ result = self ::$ client ->analyzeSyntax ($ text );
31- $ info = $ result ->info ();
32-
33- foreach ($ expectedValues as $ key => $ expected ) {
34- $ this ->assertEquals ($ expected , $ info [$ key ]);
35- }
36- }
37-
38- public function analyzeSyntaxProvider ()
39- {
40- return [
41- [
42- 'Do you know the way to San Jose? ' ,
43- [
44- 'sentences ' => [
45- [
46- 'text ' => [
47- 'content ' => 'Do you know the way to San Jose? ' ,
48- 'beginOffset ' => 0 ,
49- ]
50- ]
51- ],
52- 'entities ' => [],
53- 'language ' => 'en '
54- ]
55- ]
56- ];
57- }
58-
5932 /**
6033 * @dataProvider analyzeSentimentProvider
6134 */
6235 public function testAnalyzeSentiment ($ text , $ expectedValues )
6336 {
64- $ result = self ::$ client ->analyzeSentiment ($ text );
65- $ info = $ result ->info ();
66-
67- foreach ($ expectedValues as $ key => $ expected ) {
68- $ this ->assertEqualsWithDelta ($ expected , $ info [$ key ], 0.2 );
37+ $ request = (new AnalyzeSentimentRequest ())
38+ ->setDocument (new Document ([
39+ 'content ' => $ text ,
40+ 'type ' => Document \Type::PLAIN_TEXT
41+ ]))
42+ ->setEncodingType (EncodingType::UTF8 );
43+ $ result = self ::$ client ->analyzeSentiment ($ request );
44+
45+ $ this ->assertEqualsWithDelta ($ expectedValues ['language ' ], $ result ->getLanguageCode (), 0.2 );
46+
47+ $ sentences = $ result ->getSentences ();
48+ $ this ->assertCount (count ($ expectedValues ['sentences ' ]), $ sentences );
49+
50+ foreach ($ expectedValues ['sentences ' ] as $ i => $ expectedSentence ) {
51+ $ sentence = $ sentences [$ i ];
52+ $ this ->assertEquals ($ expectedSentence ['text ' ]['content ' ], $ sentence ->getText ()->getContent ());
53+ $ this ->assertEquals ($ expectedSentence ['text ' ]['beginOffset ' ], $ sentence ->getText ()->getBeginOffset ());
54+ $ this ->assertEqualsWithDelta (
55+ $ expectedSentence ['sentiment ' ]['magnitude ' ],
56+ $ sentence ->getSentiment ()->getMagnitude (),
57+ 0.2
58+ );
59+ $ this ->assertEqualsWithDelta (
60+ $ expectedSentence ['sentiment ' ]['score ' ],
61+ $ sentence ->getSentiment ()->getScore (),
62+ 0.2
63+ );
6964 }
7065 }
7166
@@ -98,75 +93,29 @@ public function analyzeSentimentProvider()
9893 */
9994 public function testAnalyzeEntities ($ text , $ expectedEntities )
10095 {
101- $ result = self ::$ client ->analyzeEntities ($ text );
102- $ info = $ result ->info ();
96+ $ request = (new AnalyzeEntitiesRequest ())
97+ ->setDocument (new Document ([
98+ 'content ' => $ text ,
99+ 'type ' => Document \Type::PLAIN_TEXT ,
100+ ]));
103101
104- foreach ($ expectedEntities as $ expectedEntity ) {
105- $ exists = false ;
106- foreach ($ info ['entities ' ] as $ entity ) {
107- if ($ entity ['name ' ] == $ expectedEntity ['name ' ]) {
108- $ exists = true ;
109- $ this ->assertEquals ($ entity ['type ' ], $ expectedEntity ['type ' ]);
110- break ;
111- }
112- }
113- $ this ->assertTrue ($ exists );
114- }
115- }
116-
117- public function analyzeEntitiesProvider ()
118- {
119- return [
120- [
121- 'Do you know the way to San Jose? ' ,
122- [
123- [
124- 'name ' => 'San Jose ' ,
125- 'type ' => 'LOCATION ' ,
126- ],
127- [
128- 'name ' => 'way ' ,
129- 'type ' => 'OTHER ' ,
130- ],
131- ]
132- ]
133- ];
134- }
135-
136- /**
137- * @dataProvider analyzeEntitySentimentProvider
138- */
139- public function testAnalyzeEntitySentiment ($ text , $ expectedEntities )
140- {
141- $ result = self ::$ client ->analyzeEntitySentiment ($ text );
142- $ info = $ result ->info ();
102+ $ result = self ::$ client ->analyzeEntities ($ request );
103+ $ entities = $ result ->getEntities ();
143104
144105 foreach ($ expectedEntities as $ expectedEntity ) {
145106 $ exists = false ;
146- foreach ($ info [ ' entities ' ] as $ entity ) {
147- if ($ entity[ ' name ' ] == $ expectedEntity ['name ' ]) {
107+ foreach ($ entities as $ entity ) {
108+ if ($ entity-> getName () == $ expectedEntity ['name ' ]) {
148109 $ exists = true ;
149- $ this ->assertEquals ($ entity ['type ' ], $ expectedEntity ['type ' ]);
150- $ this ->assertEqualsWithDelta (
151- $ entity ['sentiment ' ]['score ' ],
152- $ expectedEntity ['sentiment ' ]['score ' ],
153- 0.2
154- );
155-
156- $ this ->assertEqualsWithDelta (
157- $ entity ['sentiment ' ]['magnitude ' ],
158- $ expectedEntity ['sentiment ' ]['magnitude ' ],
159- 0.2
160- );
161-
110+ $ this ->assertEquals (Entity \Type::value ($ expectedEntity ['type ' ]), $ entity ->getType ());
162111 break ;
163112 }
164113 }
165114 $ this ->assertTrue ($ exists );
166115 }
167116 }
168117
169- public function analyzeEntitySentimentProvider ()
118+ public function analyzeEntitiesProvider ()
170119 {
171120 return [
172121 [
@@ -175,39 +124,10 @@ public function analyzeEntitySentimentProvider()
175124 [
176125 'name ' => 'San Jose ' ,
177126 'type ' => 'LOCATION ' ,
178- 'sentiment ' => [
179- 'magnitude ' => 0 ,
180- 'score ' => 0 ,
181- ],
182127 ],
183128 [
184129 'name ' => 'way ' ,
185130 'type ' => 'OTHER ' ,
186- 'sentiment ' => [
187- 'magnitude ' => 0 ,
188- 'score ' => 0 ,
189- ],
190- ],
191- ]
192- ],
193- [
194- "The road to San Jose is great! " ,
195- [
196- [
197- 'name ' => 'San Jose ' ,
198- 'type ' => 'LOCATION ' ,
199- 'sentiment ' => [
200- 'magnitude ' => 0.8 ,
201- 'score ' => 0.8 ,
202- ],
203- ],
204- [
205- 'name ' => 'road ' ,
206- 'type ' => 'LOCATION ' ,
207- 'sentiment ' => [
208- 'magnitude ' => 0.8 ,
209- 'score ' => 0.8 ,
210- ],
211131 ],
212132 ]
213133 ]
@@ -216,15 +136,20 @@ public function analyzeEntitySentimentProvider()
216136
217137 public function testClassifyText ()
218138 {
219- $ result = self ::$ client ->classifyText (
220- 'Rafael Montero Shines in Mets’ Victory Over the Reds.Montero, who ' .
139+ $ text = 'Rafael Montero Shines in Mets’ Victory Over the Reds.Montero, who ' .
221140 'was demoted at midseason, took a one-hitter into the ninth inning ' .
222141 'as the Mets continued to dominate Cincinnati with a win at Great ' .
223- 'American Ball Park. '
224- );
225- $ category = $ result ->categories ()[0 ];
226-
227- $ this ->assertEquals ('/Sports/Team Sports/Baseball ' , $ category ['name ' ]);
228- $ this ->assertGreaterThan (.9 , $ category ['confidence ' ]);
142+ 'American Ball Park. ' ;
143+ $ request = (new ClassifyTextRequest ())
144+ ->setDocument (new Document ([
145+ 'content ' => $ text ,
146+ 'type ' => Document \Type::PLAIN_TEXT ,
147+ ]));
148+ $ result = self ::$ client ->classifyText ($ request );
149+ $ categories = $ result ->getCategories ();
150+ $ category = $ categories [0 ];
151+
152+ $ this ->assertEquals ('/Sports/Team Sports/Baseball ' , $ category ->getName ());
153+ $ this ->assertGreaterThan (.9 , $ category ->getConfidence ());
229154 }
230155}
0 commit comments