Пример #1
0
def test_entities_tostring(analysis):
    assert analysis.entities_tostring([
        Entity(name="General Motors",
               entity_type="ORGANIZATION",
               metadata={
                   "mid": "/m/035nm",
                   "wikipedia_url":
                   "http://en.wikipedia.org/wiki/General_Motors"
               },
               salience=0.33838183,
               mentions=["General Motors"]),
        Entity(name="jobs",
               entity_type="OTHER",
               metadata={"wikipedia_url": None},
               salience=0.31634554,
               mentions=["jobs"])
    ]) == ('[{name: "General Motors",'
           ' entity_type: "ORGANIZATION",'
           ' wikipedia_url: "http://en.wikipedia.org/wiki/General_Motors",'
           ' metadata: {"mid": "/m/035nm"},'
           ' salience: 0.33838183,'
           ' mentions: ["General Motors"]}, '
           '{name: "jobs",'
           ' entity_type: "OTHER",'
           ' wikipedia_url: None,'
           ' metadata: {},'
           ' salience: 0.31634554,'
           ' mentions: ["jobs"]}]')
    assert analysis.entities_tostring([]) == "[]"
Пример #2
0
    def analyze_entities(self):
        """Analyze the entities in the current document.

        Finds named entities (currently finds proper names as of August 2016)
        in the text, entity types, salience, mentions for each entity, and
        other properties.

        .. _analyzeEntities: https://cloud.google.com/natural-language/\
                             reference/rest/v1/documents/analyzeEntities

        See `analyzeEntities`_.

        :rtype: list
        :returns: A list of :class:`~.language.entity.Entity` returned from
                  the API.
        """
        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']
        ]
Пример #3
0
    def test_constructor(self):
        from google.cloud.language.api_responses import EntityResponse
        from google.cloud.language.entity import Entity

        entity_response = EntityResponse(
            entities=[Entity.from_api_repr(self.ENTITY_DICT)],
            language='en',
        )
        self._verify_entity_response(entity_response)
Пример #4
0
    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'],
        )
Пример #5
0
    def analyze_entities(self):
        """Analyze the entities in the current document.

        Finds named entities (currently finds proper names as of August 2016)
        in the text, entity types, salience, mentions for each entity, and
        other properties.

        .. _analyzeEntities: https://cloud.google.com/natural-language/\
                             reference/rest/v1/documents/analyzeEntities

        See `analyzeEntities`_.

        :rtype: list
        :returns: A list of :class:`~.language.entity.Entity` returned from
                  the API.
        """
        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']]
Пример #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
Пример #7
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