1818breaks a document down into tokens and sentences.
1919"""
2020
21+ from google .cloud .language .sentiment import Sentiment
22+
2123
2224class PartOfSpeech (object ):
2325 """Part of speech of a :class:`Token`."""
@@ -183,11 +185,19 @@ class Sentence(object):
183185 :param begin: The beginning offset of the sentence in the original
184186 document according to the encoding type specified
185187 in the API request.
188+
189+ :type sentiment: :class:`~google.cloud.language.sentiment.Sentiment`
190+ :param sentiment:
191+ (Optional) For calls to
192+ :meth:`~google.cloud.language.document.Document.annotate_text` where
193+ ``include_sentiment`` is set to true, this field will contain the
194+ sentiment for the sentence.
186195 """
187196
188- def __init__ (self , content , begin ):
197+ def __init__ (self , content , begin , sentiment = None ):
189198 self .content = content
190199 self .begin = begin
200+ self .sentiment = sentiment
191201
192202 @classmethod
193203 def from_api_repr (cls , payload ):
@@ -200,4 +210,11 @@ def from_api_repr(cls, payload):
200210 :returns: The sentence parsed from the API representation.
201211 """
202212 text_span = payload ['text' ]
203- return cls (text_span ['content' ], text_span ['beginOffset' ])
213+
214+ try :
215+ sentiment = Sentiment .from_api_repr (payload ['sentiment' ])
216+ except KeyError :
217+ sentiment = None
218+
219+ return cls (text_span ['content' ], text_span ['beginOffset' ],
220+ sentiment = sentiment )
0 commit comments