diff --git a/.gitignore b/.gitignore index 46eb8451d..373f4bbf6 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,5 @@ test/__init__.py *~ .sfdx/tools/apex.db +.pytest_cache/ +python-sdk.iml diff --git a/test/unit/test_natural_language_classifier_v1.py b/test/unit/test_natural_language_classifier_v1.py index edb1a501a..f7ebea29c 100644 --- a/test/unit/test_natural_language_classifier_v1.py +++ b/test/unit/test_natural_language_classifier_v1.py @@ -82,3 +82,54 @@ def test_success(): assert responses.calls[4].response.text == remove_response assert len(responses.calls) == 5 + + +@responses.activate +def test_validTextInputs_classifyMultipleTexts_callsUrlWithCorrectJSONBody(): + natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1(username="username", + password="password") + classify_collection_url = 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/497EF2-nlc-00/classify_collection' + classify_collection_response = '{ \ + "classifier_id": "497EF2-nlc-00", \ + "url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/10D41B-nlc-1", \ + "collection": [ \ + { \ + "text": "How hot will it be today?", \ + "top_class": "temperature", \ + "classes": [ \ + { \ + "class_name": "temperature", \ + "confidence": 0.9930558798985937 \ + }, \ + { \ + "class_name": "conditions", \ + "confidence": 0.006944120101406304 \ + } \ + ] \ + }, \ + { \ + "text": "Is it hot outside?", \ + "top_class": "temperature", \ + "classes": [ \ + { \ + "class_name": "temperature", \ + "confidence": 1 \ + }, \ + { \ + "class_name": "conditions", \ + "confidence": 0 \ + } \ + ] \ + } \ + ] \ + }' + responses.add(responses.POST, classify_collection_url, + body=classify_collection_response, status=200, + content_type='application/json') + + classifier_id = '497EF2-nlc-00' + collection = ["How hot will it be today?", "Is it hot outside?"] + natural_language_classifier.classifyCollection(classifier_id, collection) + + assert responses.calls[0].request.url == classify_collection_url + assert responses.calls[0].response.text == classify_collection_response diff --git a/watson_developer_cloud/natural_language_classifier_v1.py b/watson_developer_cloud/natural_language_classifier_v1.py index 6831c5de4..7ce65503d 100644 --- a/watson_developer_cloud/natural_language_classifier_v1.py +++ b/watson_developer_cloud/natural_language_classifier_v1.py @@ -91,6 +91,30 @@ def classify(self, classifier_id, text): method='POST', url=url, json=data, accept_json=True) return response + def classifyCollection(self, classifier_id, collection): + """ + Returns label information for the input. The status must be `Available` before you + can use the classifier to classify text. + + :param str classifier_id: Classifier ID to use. + :param str collection: The submitted collection of phrases. + :return: A `dict` containing the `Classification` response. + :rtype: dict + """ + if classifier_id is None: + raise ValueError('classifier_id must be provided') + if collection is None: + raise ValueError('collection must be provided') + if len(collection) == 0 is None: + raise ValueError('collection must be provided') + data = {'collection': list(map(lambda x: {"text" :x}, collection))} + url = '/v1/classifiers/{0}/classify_collection'.format( + *self._encode_path_vars(classifier_id)) + response = self.request( + method='POST', url=url, json=data, accept_json=True) + return response + + ######################### # Manage classifiers #########################