# Copyright 2016 Google Inc.
#
# 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/v1/Sentiment
    .. _Sentiment basics: https://cloud.google.com/natural-language/\
                          docs/basics#sentiment-analysis-values

    See `Sentiment message`_ and `Sentiment basics`_.

    :type score: float
    :param score: Score 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 score (positive or negative).
    """

    def __init__(self, score, magnitude):
        self.score = score
        self.magnitude = magnitude

    @classmethod
    def from_api_repr(cls, payload):
        """Convert a 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.
        """
        score = payload['score']
        magnitude = payload['magnitude']
        return cls(score, magnitude)

language/google/cloud/language/sentiment.py
# Copyright 2016 Google Inc.
#
# 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/v1/Sentiment
    .. _Sentiment basics: https://cloud.google.com/natural-language/\
                          docs/basics#sentiment-analysis-values

    See `Sentiment message`_ and `Sentiment basics`_.

    :type score: float
    :param score: Score 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 score (positive or negative).
    """

    def __init__(self, score, magnitude):
        self.score = score
        self.magnitude = magnitude

    @classmethod
    def from_api_repr(cls, payload):
        """Convert a 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.
        """
        score = payload.get('score', None)
        magnitude = payload['magnitude']
        return cls(score, magnitude)

language/google/cloud/language/sentiment.py
diff --git a/language/google/cloud/language/sentiment.py b/language/google/cloud/language/sentiment.py
index e1e5da7edc6f..2c56f9d1ee87 100644
--- a/language/google/cloud/language/sentiment.py
+++ b/language/google/cloud/language/sentiment.py
@@ -52,6 +52,6 @@ def from_api_repr(cls, payload):
         :rtype: :class:`Sentiment`
         :returns: The sentiment parsed from the API representation.
         """
-        score = payload['score']
+        score = payload.get('score', None)
         magnitude = payload['magnitude']
         return cls(score, magnitude)

Error in NL API, 'Sentiment' object has no attribute 'score'
Upon executing the following code on GCE & locally I'm getting 'score' not found error locally while it works on GCE.

```
def analyze_text(text):
    language_client = language.Client()
    document = language_client.document_from_text(text)
    annotations = document.annotate_text(include_sentiment=True)
    print(annotations.sentiment.score)
    print (annotations.sentiment.magnitude)
```

Only difference I can find is on GCE, the gcloud version is different than the local
**GCE gcloud**
gcloud --version
Google Cloud SDK 145.0.0
alpha 2017.02.21
app-engine-python 1.9.50
beta 2017.02.21
bq 2.0.24
bq-nix 2.0.24
core 2017.02.21
core-nix 2017.02.21
gcloud 
gcloud-deps 2017.02.21
gcloud-deps-linux-x86_64 2017.02.21
gsutil 4.22
gsutil-nix 4.22

**Local gcloud**
gcloud --version
Google Cloud SDK 148.0.1
bq 2.0.24
bq-nix 2.0.24
core 2017.03.24
core-nix 2016.11.07
gcloud 
gcloud-deps 2017.03.17
gcloud-deps-darwin-x86_64 2017.02.21
gsutil 4.23
gsutil-nix 4.19

Debugging locally, the response from NL API consist of polarity property and not score.

<img width="761" alt="image" src="https://cloud.githubusercontent.com/assets/1888535/24331449/62cbdbee-1252-11e7-9d29-8699df078fcf.png">


