-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing analyze sentiment method and helpers in language #2189
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Definition for Google Cloud Natural Language API sentiment. | ||
|
||
Sentiment is the response to an ``analyzeSentiment`` request. | ||
""" | ||
|
||
|
||
class Sentiment(object): | ||
"""A Google Cloud Natural Language API sentiment object. | ||
|
||
.. _Sentiment message: https://cloud.google.com/natural-language/\ | ||
reference/rest/v1beta1/Sentiment | ||
.. _Sentiment basics: https://cloud.google.com/natural-language/\ | ||
docs/basics#sentiment-analysis-values | ||
|
||
See `Sentiment message`_ and `Sentiment basics`_. | ||
|
||
|
||
:type polarity: float | ||
:param polarity: Polarity of the sentiment in the ``[-1.0, 1.0]`` range. | ||
Larger numbers represent more positive sentiments. | ||
|
||
:type magnitude: float | ||
:param magnitude: A non-negative number in the ``[0, +inf)`` range, which | ||
represents the absolute magnitude of sentiment | ||
regardless of polarity (positive or negative). | ||
""" | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
|
||
class TestSentiment(unittest.TestCase): | ||
|
||
def _getTargetClass(self): | ||
from gcloud.language.sentiment import Sentiment | ||
return Sentiment | ||
|
||
def _makeOne(self, *args, **kw): | ||
return self._getTargetClass()(*args, **kw) | ||
|
||
def test_constructor(self): | ||
polarity = 1 | ||
magnitude = 2.3 | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.