|
| 1 | +# Copyright 2017 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Response types from the Natural Language API.""" |
| 16 | + |
| 17 | +from google.cloud.language.entity import Entity |
| 18 | +from google.cloud.language.sentence import Sentence |
| 19 | +from google.cloud.language.sentiment import Sentiment |
| 20 | +from google.cloud.language.syntax import Token |
| 21 | + |
| 22 | + |
| 23 | +class EntityResponse(object): |
| 24 | + """Object representation of entity responses. |
| 25 | +
|
| 26 | + A representation of a response sent back from the |
| 27 | + ``analyzeEntites`` request to the Google Natural language API. |
| 28 | +
|
| 29 | + :type entities: list |
| 30 | + :param entities: A list of :class:`~.language.entity.Entity` objects. |
| 31 | +
|
| 32 | + :type language: str |
| 33 | + :param language: The language used for analysis. |
| 34 | + """ |
| 35 | + def __init__(self, entities, language): |
| 36 | + self.entities = entities |
| 37 | + self.language = language |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def from_api_repr(cls, payload): |
| 41 | + """Return an entity response from a JSON representation. |
| 42 | +
|
| 43 | + :type payload: dict |
| 44 | + :param payload: A dictionary representing the response. |
| 45 | +
|
| 46 | + :rtype: :class:`~.language.entity.Entity` |
| 47 | + :returns: An ``Entity`` object. |
| 48 | + """ |
| 49 | + return cls( |
| 50 | + entities=[Entity.from_api_repr(i) for i in payload['entities']], |
| 51 | + language=payload['language'], |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +class SentimentResponse(object): |
| 56 | + """Object representation of sentiment responses. |
| 57 | +
|
| 58 | + A representation of a response to an ``analyzeSentiment`` request |
| 59 | + to the Google Natural Language API. |
| 60 | +
|
| 61 | + :type sentiment: :class:`~.language.sentiment.Sentiment` |
| 62 | + :param sentiment: A Sentiment object. |
| 63 | +
|
| 64 | + :type language: str |
| 65 | + :param language: The language used for analyzing sentiment. |
| 66 | +
|
| 67 | + :type sentences: list |
| 68 | + :param sentences: A list of :class:`~.language.syntax.Sentence` objects. |
| 69 | + """ |
| 70 | + def __init__(self, sentiment, language, sentences): |
| 71 | + self.sentiment = sentiment |
| 72 | + self.language = language |
| 73 | + self.sentences = sentences |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def from_api_repr(cls, payload): |
| 77 | + """Return an sentiment response from a JSON representation. |
| 78 | +
|
| 79 | + :type payload: dict |
| 80 | + :param payload: A dictionary representing the response. |
| 81 | +
|
| 82 | + :rtype: `~.language.sentiment.Sentiment` |
| 83 | + :returns: A ``Sentiment`` object. |
| 84 | + """ |
| 85 | + return cls( |
| 86 | + language=payload.get('language'), |
| 87 | + sentences=[Sentence.from_api_repr(sentence) for sentence |
| 88 | + in payload.get('sentences', ())], |
| 89 | + sentiment=Sentiment.from_api_repr(payload['documentSentiment']), |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +class SyntaxResponse(object): |
| 94 | + """Object representation of syntax responses. |
| 95 | +
|
| 96 | + A representation of a response to an ``analyzeSyntax`` request |
| 97 | + to the Google Natural Language API. |
| 98 | +
|
| 99 | + :type tokens: list |
| 100 | + :param tokens: A list of :class:`~.language.syntax.Token` objects. |
| 101 | +
|
| 102 | + :type language: str |
| 103 | + :param language: The language used for analyzing sentiment. |
| 104 | +
|
| 105 | + :type sentences: list |
| 106 | + :param sentences: A list of :class:`~.language.syntax.Sentence` objects. |
| 107 | + """ |
| 108 | + def __init__(self, tokens, language, sentences): |
| 109 | + self.tokens = tokens |
| 110 | + self.language = language |
| 111 | + self.sentences = sentences |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def from_api_repr(cls, payload): |
| 115 | + """Return an syntax response from a JSON representation. |
| 116 | +
|
| 117 | + :type payload: dict |
| 118 | + :param payload: A dictionary representing the response. |
| 119 | +
|
| 120 | + :rtype: `~.language.syntax.Syntax` |
| 121 | + :returns: A ``Syntax`` object. |
| 122 | + """ |
| 123 | + return cls( |
| 124 | + language=payload.get('language'), |
| 125 | + sentences=[Sentence.from_api_repr(sentence) for sentence in |
| 126 | + payload.get('sentences', ())], |
| 127 | + tokens=[Token.from_api_repr(token) for token in |
| 128 | + payload.get('tokens', ())] |
| 129 | + ) |
0 commit comments