Beispiel #1
0
    def analyze_entity_sentiment(self, document, encoding_type, options=None):
        """
        Finds entities, similar to ``AnalyzeEntities`` in the text and analyzes
        sentiment associated with each entity and its mentions.

        Example:
          >>> from google.cloud.gapic.language.v1beta2 import language_service_client
          >>> from google.cloud.gapic.language.v1beta2 import enums
          >>> from google.cloud.proto.language.v1beta2 import language_service_pb2
          >>> client = language_service_client.LanguageServiceClient()
          >>> document = language_service_pb2.Document()
          >>> encoding_type = enums.EncodingType.NONE
          >>> response = client.analyze_entity_sentiment(document, encoding_type)

        Args:
          document (:class:`google.cloud.proto.language.v1beta2.language_service_pb2.Document`): Input document.
          encoding_type (enum :class:`google.cloud.gapic.language.v1beta2.enums.EncodingType`): The encoding type used by the API to calculate offsets.
          options (:class:`google.gax.CallOptions`): Overrides the default
            settings for this call, e.g, timeout, retries etc.

        Returns:
          A :class:`google.cloud.proto.language.v1beta2.language_service_pb2.AnalyzeEntitySentimentResponse` instance.

        Raises:
          :exc:`google.gax.errors.GaxError` if the RPC is aborted.
          :exc:`ValueError` if the parameters are invalid.
        """
        # Create the request object.
        request = language_service_pb2.AnalyzeEntitySentimentRequest(
            document=document, encoding_type=encoding_type)
        return self._analyze_entity_sentiment(request, options)
    def test_analyze_entity_sentiment(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = language_service_client.LanguageServiceClient()

        # Mock request
        document = language_service_pb2.Document()
        encoding_type = enums.EncodingType.NONE

        # Mock response
        language = 'language-1613589672'
        expected_response = language_service_pb2.AnalyzeEntitySentimentResponse(
            language=language)
        grpc_stub.AnalyzeEntitySentiment.return_value = expected_response

        response = client.analyze_entity_sentiment(document, encoding_type)
        self.assertEqual(expected_response, response)

        grpc_stub.AnalyzeEntitySentiment.assert_called_once()
        args, kwargs = grpc_stub.AnalyzeEntitySentiment.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = language_service_pb2.AnalyzeEntitySentimentRequest(
            document=document, encoding_type=encoding_type)
        self.assertEqual(expected_request, actual_request)