1717
1818ANNOTATE_NAME = 'Moon'
1919ANNOTATE_CONTENT = 'A cow jumped over the %s.' % (ANNOTATE_NAME ,)
20- ANNOTATE_POLARITY = 1
20+ ANNOTATE_SCORE = 1
2121ANNOTATE_MAGNITUDE = 0.2
2222ANNOTATE_SALIENCE = 0.11793101
2323ANNOTATE_WIKI_URL = 'http://en.wikipedia.org/wiki/Natural_satellite'
@@ -286,20 +286,20 @@ def test_analyze_entities(self):
286286 client ._connection .api_request .assert_called_once_with (
287287 path = 'analyzeEntities' , method = 'POST' , data = expected )
288288
289- def _verify_sentiment (self , sentiment , polarity , magnitude ):
289+ def _verify_sentiment (self , sentiment , score , magnitude ):
290290 from google .cloud .language .sentiment import Sentiment
291291
292292 self .assertIsInstance (sentiment , Sentiment )
293- self .assertEqual (sentiment .polarity , polarity )
293+ self .assertEqual (sentiment .score , score )
294294 self .assertEqual (sentiment .magnitude , magnitude )
295295
296296 def test_analyze_sentiment (self ):
297297 content = 'All the pretty horses.'
298- polarity = 1
298+ score = 1
299299 magnitude = 0.6
300300 response = {
301301 'documentSentiment' : {
302- 'polarity ' : polarity ,
302+ 'score ' : score ,
303303 'magnitude' : magnitude ,
304304 },
305305 'language' : 'en-US' ,
@@ -308,13 +308,164 @@ def test_analyze_sentiment(self):
308308 document = self ._make_one (client , content )
309309
310310 sentiment = document .analyze_sentiment ()
311- self ._verify_sentiment (sentiment , polarity , magnitude )
311+ self ._verify_sentiment (sentiment , score , magnitude )
312312
313313 # Verify the request.
314314 expected = self ._expected_data (content )
315315 client ._connection .api_request .assert_called_once_with (
316316 path = 'analyzeSentiment' , method = 'POST' , data = expected )
317317
318+ def _verify_token (self , token , text_content , part_of_speech , lemma ):
319+ from google .cloud .language .syntax import Token
320+ from google .cloud .language .syntax import PartOfSpeech
321+
322+ self .assertIsInstance (token , Token )
323+ self .assertEqual (token .text_content , text_content )
324+ self .assertEqual (token .part_of_speech , part_of_speech )
325+ self .assertEqual (token .lemma , lemma )
326+
327+ def test_analyze_syntax (self ):
328+ from google .cloud .language .document import Encoding
329+ from google .cloud .language .syntax import Token
330+ from google .cloud .language .syntax import PartOfSpeech
331+
332+ name1 = 'R-O-C-K'
333+ name2 = 'USA'
334+ content = name1 + ' in the ' + name2
335+ response = {
336+ 'sentences' : [
337+ {
338+ 'text' : {
339+ 'content' : 'R-O-C-K in the USA' ,
340+ 'beginOffset' : - 1 ,
341+ },
342+ 'sentiment' : None ,
343+ }
344+ ],
345+ 'tokens' : [
346+ {
347+ 'text' : {
348+ 'content' : 'R-O-C-K' ,
349+ 'beginOffset' : - 1 ,
350+ },
351+ 'partOfSpeech' : {
352+ 'tag' : 'NOUN' ,
353+ 'aspect' : 'ASPECT_UNKNOWN' ,
354+ 'case' : 'CASE_UNKNOWN' ,
355+ 'form' : 'FORM_UNKNOWN' ,
356+ 'gender' : 'GENDER_UNKNOWN' ,
357+ 'mood' : 'MOOD_UNKNOWN' ,
358+ 'number' : 'SINGULAR' ,
359+ 'person' : 'PERSON_UNKNOWN' ,
360+ 'proper' : 'PROPER' ,
361+ 'reciprocity' : 'RECIPROCITY_UNKNOWN' ,
362+ 'tense' : 'TENSE_UNKNOWN' ,
363+ 'voice' : 'VOICE_UNKNOWN' ,
364+ },
365+ 'dependencyEdge' : {
366+ 'headTokenIndex' : 0 ,
367+ 'label' : 'ROOT' ,
368+ },
369+ 'lemma' : 'R-O-C-K' ,
370+ },
371+ {
372+ 'text' : {
373+ 'content' : 'in' ,
374+ 'beginOffset' : - 1 ,
375+ },
376+ 'partOfSpeech' : {
377+ 'tag' : 'ADP' ,
378+ 'aspect' : 'ASPECT_UNKNOWN' ,
379+ 'case' : 'CASE_UNKNOWN' ,
380+ 'form' : 'FORM_UNKNOWN' ,
381+ 'gender' : 'GENDER_UNKNOWN' ,
382+ 'mood' : 'MOOD_UNKNOWN' ,
383+ 'number' : 'NUMBER_UNKNOWN' ,
384+ 'person' : 'PERSON_UNKNOWN' ,
385+ 'proper' : 'PROPER_UNKNOWN' ,
386+ 'reciprocity' : 'RECIPROCITY_UNKNOWN' ,
387+ 'tense' : 'TENSE_UNKNOWN' ,
388+ 'voice' : 'VOICE_UNKNOWN' ,
389+ },
390+ 'dependencyEdge' : {
391+ 'headTokenIndex' : 0 ,
392+ 'label' : 'PREP' ,
393+ },
394+ 'lemma' : 'in' ,
395+ },
396+ {
397+ 'text' : {
398+ 'content' : 'the' ,
399+ 'beginOffset' : - 1 ,
400+ },
401+ 'partOfSpeech' : {
402+ 'tag' : 'DET' ,
403+ 'aspect' : 'ASPECT_UNKNOWN' ,
404+ 'case' : 'CASE_UNKNOWN' ,
405+ 'form' : 'FORM_UNKNOWN' ,
406+ 'gender' : 'GENDER_UNKNOWN' ,
407+ 'mood' : 'MOOD_UNKNOWN' ,
408+ 'number' : 'NUMBER_UNKNOWN' ,
409+ 'person' : 'PERSON_UNKNOWN' ,
410+ 'proper' : 'PROPER_UNKNOWN' ,
411+ 'reciprocity' : 'RECIPROCITY_UNKNOWN' ,
412+ 'tense' : 'TENSE_UNKNOWN' ,
413+ 'voice' : 'VOICE_UNKNOWN' ,
414+ },
415+ 'dependencyEdge' : {
416+ 'headTokenIndex' : 3 ,
417+ 'label' : 'DET' ,
418+ },
419+ 'lemma' : 'the' ,
420+ },
421+ {
422+ 'text' : {
423+ 'content' : 'USA' ,
424+ 'beginOffset' : - 1 ,
425+ },
426+ 'partOfSpeech' : {
427+ 'tag' : 'NOUN' ,
428+ 'aspect' : 'ASPECT_UNKNOWN' ,
429+ 'case' : 'CASE_UNKNOWN' ,
430+ 'form' : 'FORM_UNKNOWN' ,
431+ 'gender' : 'GENDER_UNKNOWN' ,
432+ 'mood' : 'MOOD_UNKNOWN' ,
433+ 'number' : 'SINGULAR' ,
434+ 'person' : 'PERSON_UNKNOWN' ,
435+ 'proper' : 'PROPER' ,
436+ 'reciprocity' : 'RECIPROCITY_UNKNOWN' ,
437+ 'tense' : 'TENSE_UNKNOWN' ,
438+ 'voice' : 'VOICE_UNKNOWN' ,
439+ },
440+ 'dependencyEdge' : {
441+ 'headTokenIndex' : 1 ,
442+ 'label' : 'POBJ' ,
443+ },
444+ 'lemma' : 'USA' ,
445+ },
446+ ],
447+ 'language' : 'en-US' ,
448+ }
449+ client = make_mock_client (response )
450+ document = self ._make_one (client , content )
451+
452+ tokens = document .analyze_syntax ()
453+ self .assertEqual (len (tokens ), 4 )
454+ token1 = tokens [0 ]
455+ self ._verify_token (token1 , name1 , PartOfSpeech .NOUN , name1 )
456+ token2 = tokens [1 ]
457+ self ._verify_token (token2 , 'in' , PartOfSpeech .ADPOSITION , 'in' )
458+ token3 = tokens [2 ]
459+ self ._verify_token (token3 , 'the' , PartOfSpeech .DETERMINER , 'the' )
460+ token4 = tokens [3 ]
461+ self ._verify_token (token4 , name2 , PartOfSpeech .NOUN , name2 )
462+
463+ # Verify the request.
464+ expected = self ._expected_data (
465+ content , encoding_type = Encoding .UTF8 )
466+ client ._connection .api_request .assert_called_once_with (
467+ path = 'analyzeSyntax' , method = 'POST' , data = expected )
468+
318469 def _verify_sentences (self , include_syntax , annotations ):
319470 from google .cloud .language .syntax import Sentence
320471
@@ -357,7 +508,7 @@ def _annotate_text_helper(self, include_sentiment,
357508 }
358509 if include_sentiment :
359510 response ['documentSentiment' ] = {
360- 'polarity ' : ANNOTATE_POLARITY ,
511+ 'score ' : ANNOTATE_SCORE ,
361512 'magnitude' : ANNOTATE_MAGNITUDE ,
362513 }
363514
@@ -375,7 +526,7 @@ def _annotate_text_helper(self, include_sentiment,
375526 # Sentiment
376527 if include_sentiment :
377528 self ._verify_sentiment (annotations .sentiment ,
378- ANNOTATE_POLARITY , ANNOTATE_MAGNITUDE )
529+ ANNOTATE_SCORE , ANNOTATE_MAGNITUDE )
379530 else :
380531 self .assertIsNone (annotations .sentiment )
381532 # Entity
0 commit comments