File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1818"""
1919
2020from gcloud .language .entity import Entity
21+ from gcloud .language .sentiment import Sentiment
2122
2223
2324DEFAULT_LANGUAGE = 'en-US'
@@ -146,3 +147,19 @@ def analyze_entities(self):
146147 method = 'POST' , path = 'analyzeEntities' , data = data )
147148 return [Entity .from_api_repr (entity )
148149 for entity in api_response ['entities' ]]
150+
151+ def analyze_sentiment (self ):
152+ """Analyze the sentiment in the current document.
153+
154+ .. _analyzeSentiment: https://cloud.google.com/natural-language/\
155+ reference/rest/v1beta1/documents/analyzeSentiment
156+
157+ See `analyzeSentiment`_.
158+
159+ :rtype: :class:`.Sentiment`
160+ :returns: The sentiment of the current document.
161+ """
162+ data = {'document' : self ._to_dict ()}
163+ api_response = self .client .connection .api_request (
164+ method = 'POST' , path = 'analyzeSentiment' , data = data )
165+ return Sentiment .from_api_repr (api_response ['documentSentiment' ])
Original file line number Diff line number Diff line change @@ -39,3 +39,17 @@ class Sentiment(object):
3939 def __init__ (self , polarity , magnitude ):
4040 self .polarity = polarity
4141 self .magnitude = magnitude
42+
43+ @classmethod
44+ def from_api_repr (cls , payload ):
45+ """Convert an Sentiment from the JSON API into a :class:`Sentiment`.
46+
47+ :param payload: dict
48+ :type payload: The value from the backend.
49+
50+ :rtype: :class:`Sentiment`
51+ :returns: The sentiment parsed from the API representation.
52+ """
53+ polarity = payload ['polarity' ]
54+ magnitude = payload ['magnitude' ]
55+ return cls (polarity , magnitude )
Original file line number Diff line number Diff line change @@ -167,6 +167,34 @@ def test_analyze_entities(self):
167167 self .assertEqual (req ['path' ], 'analyzeEntities' )
168168 self .assertEqual (req ['method' ], 'POST' )
169169
170+ def test_analyze_sentiment (self ):
171+ from gcloud .language .sentiment import Sentiment
172+
173+ content = 'All the pretty horses.'
174+ polarity = 1
175+ magnitude = 0.6
176+ response = {
177+ 'documentSentiment' : {
178+ 'polarity' : polarity ,
179+ 'magnitude' : magnitude ,
180+ },
181+ 'language' : 'en' ,
182+ }
183+ connection = _Connection (response )
184+ client = _Client (connection = connection )
185+ document = self ._makeOne (client , content )
186+
187+ sentiment = document .analyze_sentiment ()
188+ self .assertIsInstance (sentiment , Sentiment )
189+ self .assertEqual (sentiment .polarity , polarity )
190+ self .assertEqual (sentiment .magnitude , magnitude )
191+
192+ # Verify the request.
193+ self .assertEqual (len (connection ._requested ), 1 )
194+ req = connection ._requested [0 ]
195+ self .assertEqual (req ['path' ], 'analyzeSentiment' )
196+ self .assertEqual (req ['method' ], 'POST' )
197+
170198
171199class _Connection (object ):
172200
Original file line number Diff line number Diff line change @@ -30,3 +30,15 @@ def test_constructor(self):
3030 sentiment = self ._makeOne (polarity , magnitude )
3131 self .assertEqual (sentiment .polarity , polarity )
3232 self .assertEqual (sentiment .magnitude , magnitude )
33+
34+ def test_from_api_repr (self ):
35+ klass = self ._getTargetClass ()
36+ polarity = - 1
37+ magnitude = 5.55
38+ payload = {
39+ 'polarity' : polarity ,
40+ 'magnitude' : magnitude ,
41+ }
42+ sentiment = klass .from_api_repr (payload )
43+ self .assertEqual (sentiment .polarity , polarity )
44+ self .assertEqual (sentiment .magnitude , magnitude )
You can’t perform that action at this time.
0 commit comments