-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Send back the complete API responses. #3059
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
Changes from 3 commits
36411d6
fbd13d0
84e59a8
4865674
0b3ce25
c0448da
1c6db0d
8581893
e670708
31f36c4
26a73e1
5a5d934
7226f12
f1ebca1
27db2d4
75bc55e
31a7e88
66c4f85
c301400
006bad4
65e4b85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Copyright 2017 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. | ||
|
|
||
| """Classes representing each of the types of responses returned from | ||
| the Natural Language API. | ||
| """ | ||
|
|
||
| from google.cloud.language.entity import Entity | ||
| from google.cloud.language.sentence import Sentence | ||
| from google.cloud.language.sentiment import Sentiment | ||
| from google.cloud.language.syntax import Token | ||
|
|
||
|
|
||
| class EntityResponse(object): | ||
| """A representation of a response sent back from the | ||
| ``analyzeEntites`` request to the Google Natural language API. | ||
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. |
||
| :type entities: list | ||
| :param entities: A list of :class:`~.language.entity.Entity` objects. | ||
| :type language: str | ||
| :param language: The language used for analysis. | ||
| """ | ||
| def __init__(self, entities, language): | ||
| self.entities = entities | ||
| self.language = language | ||
|
|
||
| @classmethod | ||
| def from_api_repr(cls, payload): | ||
| """Return an entity response from a JSON representation. | ||
| :type payload: dict | ||
| :param payload: A dictionary representing the response. | ||
| """ | ||
| return cls( | ||
| entities=[Entity.from_api_repr(i) for i in payload['entities']], | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| language=payload['language'], | ||
| ) | ||
|
|
||
|
|
||
| class SentimentResponse(object): | ||
| """A representation of a response to an ``analyzeSentiment`` request | ||
| to the Google Natural Language API. | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| :type sentiment: :class:`~.language.sentiment.Sentiment` | ||
| :param sentiment: A Sentiment object. | ||
| :type language: str | ||
| :param: language: The language used for analyzing sentiment. | ||
| :type sentences: list | ||
| :param sentences: A list of :class:`~.language.syntax.Sentence` objects. | ||
| """ | ||
| def __init__(self, sentiment, language, sentences): | ||
| self.sentiment = sentiment | ||
| self.language = language | ||
| self.sentences = sentences | ||
|
|
||
| @classmethod | ||
| def from_api_repr(cls, payload): | ||
| """Return an sentiment response from a JSON representation. | ||
| :type payload: dict | ||
| :param payload: A dictionary representing the response. | ||
| """ | ||
| return cls( | ||
| language=payload.get('language', None), | ||
|
||
| sentences=[Sentence.from_api_repr(i) for i | ||
|
||
| in payload.get('sentences', ())], | ||
| sentiment=Sentiment.from_api_repr(payload['documentSentiment']), | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| ) | ||
|
|
||
|
|
||
| class SyntaxResponse(object): | ||
| """A representation of a response to an ``analyzeSyntax`` request | ||
| to the Google Natural Language API. | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| :type tokens: list | ||
| :param tokens: A list of :class:`~.language.syntax.Token` objects. | ||
| :type language: str | ||
| :param: language: The language used for analyzing sentiment. | ||
| :type sentences: list | ||
| :param sentences: A list of :class:`~.language.syntax.Sentence` objects. | ||
| """ | ||
| def __init__(self, tokens, language, sentences): | ||
| self.tokens = tokens | ||
| self.language = language | ||
| self.sentences = sentences | ||
|
|
||
| @classmethod | ||
| def from_api_repr(cls, payload): | ||
| """Return an syntax response from a JSON representation. | ||
| :type payload: dict | ||
| :param payload: A dictionary representing the response. | ||
| """ | ||
| return cls( | ||
| language=payload.get('language', None), | ||
|
||
| sentences=[Sentence.from_api_repr(i) for i in | ||
|
||
| payload.get('sentences', ())], | ||
| tokens=[Token.from_api_repr(i) for i in | ||
| payload.get('tokens', ())] | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright 2017 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. | ||
|
|
||
| from google.cloud.language.sentiment import Sentiment | ||
|
|
||
|
|
||
| class Sentence(object): | ||
| """A Google Cloud Natural Language API sentence object. | ||
| .. _Sentence message: https://cloud.google.com/natural-language/reference\ | ||
| /rest/v1/documents/annotateText#Sentence | ||
| See `Sentence message`_. | ||
| :type content: str | ||
| :param content: The text that the sentence is composed of. | ||
| :type begin: int | ||
| :param begin: The beginning offset of the sentence in the original | ||
| document according to the encoding type specified | ||
| in the API request. | ||
| :type sentiment: :class:`~google.cloud.language.sentiment.Sentiment` | ||
| :param sentiment: | ||
| (Optional) For calls to | ||
| :meth:`~google.cloud.language.document.Document.annotate_text` where | ||
| ``include_sentiment`` is set to true, this field will contain the | ||
| sentiment for the sentence. | ||
| """ | ||
|
|
||
| def __init__(self, content, begin, sentiment=None): | ||
| self.content = content | ||
| self.begin = begin | ||
| self.sentiment = sentiment | ||
|
|
||
| @classmethod | ||
| def from_api_repr(cls, payload): | ||
| """Convert a sentence from the JSON API into a :class:`Sentence`. | ||
| :param payload: dict | ||
| :type payload: The value from the backend. | ||
| :rtype: :class:`Sentence` | ||
| :returns: The sentence parsed from the API representation. | ||
| """ | ||
| text_span = payload['text'] | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
|
||
| # The sentence may or may not have a sentiment; only attempt the | ||
| # typecast if one is present. | ||
| sentiment = None | ||
| if payload.get('sentiment', None): | ||
|
||
| sentiment = Sentiment.from_api_repr(payload['sentiment']) | ||
|
|
||
| # Return a Sentence object. | ||
| return cls(text_span['content'], text_span['beginOffset'], | ||
| sentiment=sentiment) | ||
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.