-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
36411d6
Return full API responses for Natural Language.
fbd13d0
Return full API responses from NL.
84e59a8
Do the hyphenated copyright thing for edited files.
4865674
Update usage doc.
0b3ce25
Updates based on @dhermes feedback.
c0448da
Update system tests.
1c6db0d
Added system tests and Entity Response tests.
8581893
Remove explcit dict.get('foo', None)
e670708
Fix some of the pylint errors.
lukesneeringer 31f36c4
Finish fixing pylint errors.
lukesneeringer 26a73e1
It is 2017.
lukesneeringer 5a5d934
Add SentimentResponse tests.
7226f12
Unit tests for SyntaxResponse.
f1ebca1
Missed a dict.get('foo', None) case.
27db2d4
Use assertIsNone
75bc55e
Remove wikipedia_url as an attribute.
31a7e88
PEP 257 compliance.
66c4f85
Add Sentiment isInstance check.
c301400
Add API responses documentation.
006bad4
Adding sentences to docs.
dhermes 65e4b85
Fix typo.
dhermes 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 hidden or 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 hidden or 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 hidden or 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,129 @@ | ||
# 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. | ||
|
||
"""Response types 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): | ||
"""Object representation of entity responses. | ||
|
||
A representation of a response sent back from the | ||
``analyzeEntites`` request to the Google Natural language API. | ||
|
||
: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. | ||
|
||
:rtype: :class:`~.language.entity.Entity` | ||
:returns: An ``Entity`` object. | ||
""" | ||
return cls( | ||
entities=[Entity.from_api_repr(i) for i in payload['entities']], | ||
language=payload['language'], | ||
) | ||
|
||
|
||
class SentimentResponse(object): | ||
"""Object representation of sentiment responses. | ||
|
||
A representation of a response to an ``analyzeSentiment`` request | ||
to the Google Natural Language API. | ||
|
||
: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. | ||
|
||
:rtype: `~.language.sentiment.Sentiment` | ||
:returns: A ``Sentiment`` object. | ||
""" | ||
return cls( | ||
language=payload.get('language'), | ||
sentences=[Sentence.from_api_repr(sentence) for sentence | ||
in payload.get('sentences', ())], | ||
sentiment=Sentiment.from_api_repr(payload['documentSentiment']), | ||
) | ||
|
||
|
||
class SyntaxResponse(object): | ||
"""Object representation of syntax responses. | ||
|
||
A representation of a response to an ``analyzeSyntax`` request | ||
to the Google Natural Language API. | ||
|
||
: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. | ||
|
||
:rtype: `~.language.syntax.Syntax` | ||
:returns: A ``Syntax`` object. | ||
""" | ||
return cls( | ||
language=payload.get('language'), | ||
sentences=[Sentence.from_api_repr(sentence) for sentence in | ||
payload.get('sentences', ())], | ||
tokens=[Token.from_api_repr(token) for token in | ||
payload.get('tokens', ())] | ||
) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.