Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ test/__init__.py
*~

.sfdx/tools/apex.db
.pytest_cache/
python-sdk.iml
51 changes: 51 additions & 0 deletions test/unit/test_natural_language_classifier_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions watson_developer_cloud/natural_language_classifier_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#########################
Expand Down