def analyze_sentiment(self, document, encoding_type=None, options=None): """ Analyzes the sentiment of the provided text. Example: >>> from google.cloud.gapic.language.v1beta2 import language_service_client >>> from google.cloud.proto.language.v1beta2 import language_service_pb2 >>> client = language_service_client.LanguageServiceClient() >>> document = language_service_pb2.Document() >>> response = client.analyze_sentiment(document) Args: document (:class:`google.cloud.proto.language.v1beta2.language_service_pb2.Document`): Input document. Currently, ``analyzeSentiment`` only supports English text (``Document.language``=\"EN\"). encoding_type (enum :class:`google.cloud.gapic.language.v1beta2.enums.EncodingType`): The encoding type used by the API to calculate sentence offsets for the sentence sentiment. 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.AnalyzeSentimentResponse` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ if encoding_type is None: encoding_type = enums.EncodingType.NONE # Create the request object. request = language_service_pb2.AnalyzeSentimentRequest( document=document, encoding_type=encoding_type) return self._analyze_sentiment(request, options)
def test_analyze_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() # Mock response language = 'language-1613589672' expected_response = language_service_pb2.AnalyzeSentimentResponse( language=language) grpc_stub.AnalyzeSentiment.return_value = expected_response response = client.analyze_sentiment(document) self.assertEqual(expected_response, response) grpc_stub.AnalyzeSentiment.assert_called_once() args, kwargs = grpc_stub.AnalyzeSentiment.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.AnalyzeSentimentRequest( document=document) self.assertEqual(expected_request, actual_request)