Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Implementing analyze sentiment method and helpers in language#2189
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| 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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -115,3 +115,10 @@ def test_analyze_entities_from_blob(self): | ||
| document = Config.CLIENT.document_from_url(gcs_url) | ||
| entities = document.analyze_entities() | ||
| self._check_analyze_entities_result(entities) | ||
| def test_analyze_sentiment(self): | ||
| positive_msg = 'Jogging is fun' | ||
| document = Config.CLIENT.document_from_text(positive_msg) | ||
| sentiment = document.analyze_sentiment() | ||
| self.assertEqual(sentiment.polarity, 1) | ||
| self.assertTrue(0.5 < sentiment.magnitude < 1.5) | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.