-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send back the complete API responses. (#3059)
* Return full API responses for Natural Language. * Return full API responses from NL. * Do the hyphenated copyright thing for edited files. * Update usage doc. * Updates based on @dhermes feedback. * Update system tests. * Added system tests and Entity Response tests. Still need explicit SyntaxResponse and SentimentResponse tests. * Remove explcit dict.get('foo', None) * Fix some of the pylint errors. * Finish fixing pylint errors. * It is 2017. * Add SentimentResponse tests. * Unit tests for SyntaxResponse. * Missed a dict.get('foo', None) case. * Use assertIsNone * Remove wikipedia_url as an attribute. * PEP 257 compliance. * Add Sentiment isInstance check. * Add API responses documentation. * Adding sentences to docs. * Fix typo.
- Loading branch information
1 parent
50aa873
commit 77a5d94
Showing
11 changed files
with
486 additions
and
151 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
packages/google-cloud-language/google/cloud/language/api_responses.py
This file contains 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 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 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
68 changes: 68 additions & 0 deletions
68
packages/google-cloud-language/google/cloud/language/sentence.py
This file contains 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,68 @@ | ||
# 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. | ||
|
||
"""Representation of Sentence objects.""" | ||
|
||
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'] | ||
|
||
# The sentence may or may not have a sentiment; only attempt the | ||
# typecast if one is present. | ||
sentiment = None | ||
if payload.get('sentiment') is not None: | ||
sentiment = Sentiment.from_api_repr(payload['sentiment']) | ||
|
||
# Return a Sentence object. | ||
return cls(text_span['content'], text_span['beginOffset'], | ||
sentiment=sentiment) |
This file contains 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 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.