Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
36411d6fbd13d084e59a848656740b3ce25c0448da1c6db0d8581893e67070831f36c426a73e15a5d9347226f12f1ebca127db2d475bc55e31a7e8866c4f85c301400006bad465e4b85File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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', ())] | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Copyright 2016 Google Inc. | ||
| # Copyright 2016-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. | ||
| @@ -19,15 +19,16 @@ | ||
| import collections | ||
| from google.cloud.language import api_responses | ||
| from google.cloud.language.entity import Entity | ||
| from google.cloud.language.sentiment import Sentiment | ||
| from google.cloud.language.syntax import Sentence | ||
| from google.cloud.language.sentence import Sentence | ||
| from google.cloud.language.syntax import Token | ||
| Annotations = collections.namedtuple( | ||
| 'Annotations', | ||
| 'sentencestokenssentimententities') | ||
| ['sentences', 'tokens', 'sentiment', 'entities', 'language']) | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| """Annotations for a document. | ||
| :type sentences: list | ||
| @@ -42,6 +43,9 @@ | ||
| :type entities: list | ||
| :param entities: List of :class:`~.language.entity.Entity` | ||
| found in a document. | ||
| :type language: str | ||
| :param language: The language used for the annotation. | ||
| """ | ||
| @@ -156,18 +160,16 @@ def analyze_entities(self): | ||
| See `analyzeEntities`_. | ||
| :rtype: list | ||
| :returns: A list of :class:`~.language.entity.Entity` returned from | ||
| the API. | ||
| :rtype: :class:`~.language.entity.EntityResponse` | ||
| :returns: A representation of the entity response. | ||
| """ | ||
| data = { | ||
| 'document': self._to_dict(), | ||
| 'encodingType': self.encoding, | ||
| } | ||
| api_response = self.client._connection.api_request( | ||
| method='POST', path='analyzeEntities', data=data) | ||
| return [Entity.from_api_repr(entity) | ||
| for entity in api_response['entities']] | ||
| return api_responses.EntityResponse.from_api_repr(api_response) | ||
| def analyze_sentiment(self): | ||
| """Analyze the sentiment in the current document. | ||
| @@ -177,13 +179,13 @@ def analyze_sentiment(self): | ||
| See `analyzeSentiment`_. | ||
| :rtype: :class:`.Sentiment` | ||
| :returns: The sentiment of the current document. | ||
| :rtype: :class:`.SentimentResponse` | ||
| :returns: A representation of the sentiment response. | ||
| """ | ||
| data = {'document': self._to_dict()} | ||
| api_response = self.client._connection.api_request( | ||
| method='POST', path='analyzeSentiment', data=data) | ||
| return Sentiment.from_api_repr(api_response['documentSentiment']) | ||
| return api_responses.SentimentResponse.from_api_repr(api_response) | ||
| def analyze_syntax(self): | ||
| """Analyze the syntax in the current document. | ||
| @@ -203,8 +205,7 @@ def analyze_syntax(self): | ||
| } | ||
| api_response = self.client._connection.api_request( | ||
| method='POST', path='analyzeSyntax', data=data) | ||
| return [Token.from_api_repr(token) | ||
| for token in api_response.get('tokens', ())] | ||
| return api_responses.SyntaxResponse.from_api_repr(api_response) | ||
| def annotate_text(self, include_syntax=True, include_entities=True, | ||
| include_sentiment=True): | ||
| @@ -271,9 +272,10 @@ def annotate_text(self, include_syntax=True, include_entities=True, | ||
| entities = [Entity.from_api_repr(entity) | ||
| for entity in api_response['entities']] | ||
| annotations = Annotations( | ||
| entities=entities, | ||
| language=api_response.get('language'), | ||
| sentences=sentences, | ||
| tokens=tokens, | ||
| sentiment=sentiment, | ||
| entities=entities, | ||
| tokens=tokens, | ||
| ) | ||
| return annotations | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Copyright 2016 Google Inc. | ||
| # Copyright 2016-2017 Google Inc. | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| @@ -53,10 +53,6 @@ class Entity(object): | ||
| an organization, or location. The API associates information, such as | ||
| salience and mentions, with entities. | ||
| The only supported metadata (as of August 2016) is ``wikipedia_url``, | ||
| so this value will be removed from the passed in ``metadata`` | ||
| and put in its own property. | ||
| .. _Entity message: https://cloud.google.com/natural-language/\ | ||
| reference/rest/v1/Entity | ||
| .. _EntityType enum: https://cloud.google.com/natural-language/\ | ||
| @@ -84,7 +80,6 @@ class Entity(object): | ||
| def __init__(self, name, entity_type, metadata, salience, mentions): | ||
| self.name = name | ||
| self.entity_type = entity_type | ||
| self.wikipedia_url = metadata.pop('wikipedia_url', None) | ||
| self.metadata = metadata | ||
| self.salience = salience | ||
| self.mentions = mentions | ||
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.