Skip to content

Commit

Permalink
Adding Document.analyze_sentiment() in language.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Aug 25, 2016
1 parent ccb8dd1 commit 645c086
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gcloud/language/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""

from gcloud.language.entity import Entity
from gcloud.language.sentiment import Sentiment


DEFAULT_LANGUAGE = 'en-US'
Expand Down Expand Up @@ -146,3 +147,19 @@ def analyze_entities(self):
method='POST', path='analyzeEntities', data=data)
return [Entity.from_api_repr(entity)
for entity in api_response['entities']]

def analyze_sentiment(self):
"""Analyze the sentiment in the current document.
.. _analyzeSentiment: https://cloud.google.com/natural-language/\
reference/rest/v1beta1/documents/analyzeSentiment
See `analyzeSentiment`_.
:rtype: :class:`.Sentiment`
:returns: The sentiment of the current document.
"""
data = {'document': self._to_dict()}
api_response = self.client.connection.api_request(
method='POST', path='analyzeSentiment', data=data)
return Sentiment.from_api_repr(api_response['documentSentiment'])
14 changes: 14 additions & 0 deletions gcloud/language/sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ class Sentiment(object):
def __init__(self, polarity, magnitude):
self.polarity = polarity
self.magnitude = magnitude

@classmethod
def from_api_repr(cls, payload):
"""Convert an Sentiment from the JSON API into a :class:`Sentiment`.
:param payload: dict
:type payload: The value from the backend.
:rtype: :class:`Sentiment`
:returns: The sentiment parsed from the API representation.
"""
polarity = payload['polarity']
magnitude = payload['magnitude']
return cls(polarity, magnitude)
28 changes: 28 additions & 0 deletions gcloud/language/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ def test_analyze_entities(self):
self.assertEqual(req['path'], 'analyzeEntities')
self.assertEqual(req['method'], 'POST')

def test_analyze_sentiment(self):
from gcloud.language.sentiment import Sentiment

content = 'All the pretty horses.'
polarity = 1
magnitude = 0.6
response = {
'documentSentiment': {
'polarity': polarity,
'magnitude': magnitude,
},
'language': 'en',
}
connection = _Connection(response)
client = _Client(connection=connection)
document = self._makeOne(client, content)

sentiment = document.analyze_sentiment()
self.assertIsInstance(sentiment, Sentiment)
self.assertEqual(sentiment.polarity, polarity)
self.assertEqual(sentiment.magnitude, magnitude)

# Verify the request.
self.assertEqual(len(connection._requested), 1)
req = connection._requested[0]
self.assertEqual(req['path'], 'analyzeSentiment')
self.assertEqual(req['method'], 'POST')


class _Connection(object):

Expand Down
12 changes: 12 additions & 0 deletions gcloud/language/test_sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ def test_constructor(self):
sentiment = self._makeOne(polarity, magnitude)
self.assertEqual(sentiment.polarity, polarity)
self.assertEqual(sentiment.magnitude, magnitude)

def test_from_api_repr(self):
klass = self._getTargetClass()
polarity = -1
magnitude = 5.55
payload = {
'polarity': polarity,
'magnitude': magnitude,
}
sentiment = klass.from_api_repr(payload)
self.assertEqual(sentiment.polarity, polarity)
self.assertEqual(sentiment.magnitude, magnitude)

0 comments on commit 645c086

Please sign in to comment.