Ejemplo n.º 1
0
    def test_constructor(self):
        from google.cloud.language.api_responses import SyntaxResponse
        from google.cloud.language.sentence import Sentence
        from google.cloud.language.syntax import Token

        syntax_response = SyntaxResponse(
            language='en',
            sentences=[Sentence.from_api_repr(self.SENTENCE_DICT)],
            tokens=[Token.from_api_repr(self.TOKEN_DICT)],
        )

        self._verify_syntax_response(syntax_response)
Ejemplo n.º 2
0
    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', ())]
        )
Ejemplo n.º 3
0
    def analyze_syntax(self):
        """Analyze the syntax in the current document.

        .. _analyzeSyntax: https://cloud.google.com/natural-language/\
                              reference/rest/v1/documents/analyzeSyntax

        See `analyzeSyntax`_.

        :rtype: list
        :returns: A list of :class:`~.language.syntax.Token` returned from
                  the API.
        """
        data = {
            'document': self._to_dict(),
            'encodingType': self.encoding,
        }
        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', ())]
Ejemplo n.º 4
0
    def analyze_syntax(self):
        """Analyze the syntax in the current document.

        .. _analyzeSyntax: https://cloud.google.com/natural-language/\
                              reference/rest/v1/documents/analyzeSyntax

        See `analyzeSyntax`_.

        :rtype: list
        :returns: A list of :class:`~.language.syntax.Token` returned from
                  the API.
        """
        data = {
            'document': self._to_dict(),
            'encodingType': self.encoding,
        }
        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', ())]
Ejemplo n.º 5
0
    def annotate_text(self,
                      include_syntax=True,
                      include_entities=True,
                      include_sentiment=True):
        """Advanced natural language API: document syntax and other features.

        Includes the full functionality of :meth:`analyze_entities` and
        :meth:`analyze_sentiment`, enabled by the flags
        ``include_entities`` and ``include_sentiment`` respectively.

        In addition ``include_syntax`` adds a new feature that analyzes
        the document for semantic and syntacticinformation.

        .. note::

            This API is intended for users who are familiar with machine
            learning and need in-depth text features to build upon.

        .. _annotateText: https://cloud.google.com/natural-language/\
                          reference/rest/v1/documents/annotateText

        See `annotateText`_.

        :type include_syntax: bool
        :param include_syntax: (Optional) Flag to enable syntax analysis
                               of the current document.

        :type include_entities: bool
        :param include_entities: (Optional) Flag to enable entity extraction
                                 from the current document.

        :type include_sentiment: bool
        :param include_sentiment: (Optional) Flag to enable sentiment
                                  analysis of the current document.

        :rtype: :class:`Annotations`
        :returns: A tuple of each of the four values returned from the API:
                  sentences, tokens, sentiment and entities.
        """
        features = {}
        if include_syntax:
            features['extractSyntax'] = True
        if include_entities:
            features['extractEntities'] = True
        if include_sentiment:
            features['extractDocumentSentiment'] = True

        data = {
            'document': self._to_dict(),
            'features': features,
            'encodingType': self.encoding,
        }
        api_response = self.client._connection.api_request(method='POST',
                                                           path='annotateText',
                                                           data=data)

        sentences = [
            Sentence.from_api_repr(sentence)
            for sentence in api_response['sentences']
        ]
        tokens = [
            Token.from_api_repr(token) for token in api_response['tokens']
        ]
        sentiment_info = api_response.get('documentSentiment')
        if sentiment_info is None:
            sentiment = None
        else:
            sentiment = Sentiment.from_api_repr(sentiment_info)
        entities = [
            Entity.from_api_repr(entity) for entity in api_response['entities']
        ]
        annotations = Annotations(
            sentences=sentences,
            tokens=tokens,
            sentiment=sentiment,
            entities=entities,
        )
        return annotations
Ejemplo n.º 6
0
    def annotate_text(self, include_syntax=True, include_entities=True,
                      include_sentiment=True):
        """Advanced natural language API: document syntax and other features.

        Includes the full functionality of :meth:`analyze_entities` and
        :meth:`analyze_sentiment`, enabled by the flags
        ``include_entities`` and ``include_sentiment`` respectively.

        In addition ``include_syntax`` adds a new feature that analyzes
        the document for semantic and syntacticinformation.

        .. note::

            This API is intended for users who are familiar with machine
            learning and need in-depth text features to build upon.

        .. _annotateText: https://cloud.google.com/natural-language/\
                          reference/rest/v1/documents/annotateText

        See `annotateText`_.

        :type include_syntax: bool
        :param include_syntax: (Optional) Flag to enable syntax analysis
                               of the current document.

        :type include_entities: bool
        :param include_entities: (Optional) Flag to enable entity extraction
                                 from the current document.

        :type include_sentiment: bool
        :param include_sentiment: (Optional) Flag to enable sentiment
                                  analysis of the current document.

        :rtype: :class:`Annotations`
        :returns: A tuple of each of the four values returned from the API:
                  sentences, tokens, sentiment and entities.
        """
        features = {}
        if include_syntax:
            features['extractSyntax'] = True
        if include_entities:
            features['extractEntities'] = True
        if include_sentiment:
            features['extractDocumentSentiment'] = True

        data = {
            'document': self._to_dict(),
            'features': features,
            'encodingType': self.encoding,
        }
        api_response = self.client._connection.api_request(
            method='POST', path='annotateText', data=data)

        sentences = [Sentence.from_api_repr(sentence)
                     for sentence in api_response['sentences']]
        tokens = [Token.from_api_repr(token)
                  for token in api_response['tokens']]
        sentiment_info = api_response.get('documentSentiment')
        if sentiment_info is None:
            sentiment = None
        else:
            sentiment = Sentiment.from_api_repr(sentiment_info)
        entities = [Entity.from_api_repr(entity)
                    for entity in api_response['entities']]
        annotations = Annotations(
            sentences=sentences,
            tokens=tokens,
            sentiment=sentiment,
            entities=entities,
        )
        return annotations