def clicked(): global classifier_id global input_string global service_username global service_password eText = st.get('1.0', END) input_string = eText.replace("\n", " ") if input_string and input_string.strip(): if os.getenv("natural_language_classifier_classifier_id") is not None: classifier_id = os.getenv( "natural_language_classifier_classifier_id") natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1( username=service_username, password=service_password) classifiers = natural_language_classifier.list_classifiers() status = natural_language_classifier.get_classifier(classifier_id) if status['status'] == 'Available': classes = natural_language_classifier.classify( classifier_id, input_string) W_answer = "We recommend contacting the Support Team >>> " + json.dumps( classes["top_class"], indent=2) Label(mainwin, text=W_answer).grid(row=2, column=1)
def test_classify_collection(): natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1( username="******", 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 = [ '{"text":"How hot will it be today?"}', '{"text":"Is it hot outside?"}' ] natural_language_classifier.classify_collection(classifier_id, collection) assert responses.calls[0].request.url == classify_collection_url assert responses.calls[0].response.text == classify_collection_response
def setUp(self): self.natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1( username='******', password='******') self.natural_language_classifier.set_default_headers({ 'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1' }) # Create a classifier with open( os.path.join(os.path.dirname(__file__), '../../resources/weather_data_train.csv'), 'rb') as training_data: metadata = json.dumps({'name': 'my-classifier', 'language': 'en'}) classifier = self.natural_language_classifier.create_classifier( metadata=metadata, training_data=training_data) self.classifier_id = classifier['classifier_id']
def test_success(): natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1( username="******", password="******") list_url = 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers' list_response = '{"classifiers": [{"url": "https://gateway.watsonplatform.net/natural-language-classifier-' \ 'experimental/api/v1/classifiers/497EF2-nlc-00", "classifier_id": "497EF2-nlc-00"}]}' responses.add(responses.GET, list_url, body=list_response, status=200, content_type='application/json') natural_language_classifier.list_classifiers() assert responses.calls[0].request.url == list_url assert responses.calls[0].response.text == list_response status_url = ( 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/' '497EF2-nlc-00') status_response = '{"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/' \ 'classifiers/497EF2-nlc-00", "status": "Available", "status_description": "The classifier ' \ 'instance is now available and is ready to take classifier requests.", "classifier_id": ' \ '"497EF2-nlc-00"}' responses.add(responses.GET, status_url, body=status_response, status=200, content_type='application/json') natural_language_classifier.get_classifier('497EF2-nlc-00') assert responses.calls[1].request.url == status_url assert responses.calls[1].response.text == status_response classify_url = 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/' \ '497EF2-nlc-00/classify' classify_response = '{"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/' \ 'v1", "text": "test", "classes": [{"class_name": "conditions", "confidence": ' \ '0.6575315710901418}, {"class_name": "temperature", "confidence": 0.3424684289098582}], ' \ '"classifier_id": "497EF2-nlc-00", "top_class": "conditions"}' responses.add(responses.POST, classify_url, body=classify_response, status=200, content_type='application/json') natural_language_classifier.classify('497EF2-nlc-00', 'test') assert responses.calls[2].request.url == classify_url assert responses.calls[2].response.text == classify_response create_url = 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers' create_response = '{"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/' \ 'classifiers/497EF2-nlc-00", "status": "Available", "status_description": "The classifier ' \ 'instance is now available and is ready to take classifier requests.", "classifier_id": ' \ '"497EF2-nlc-00"}' responses.add(responses.POST, create_url, body=create_response, status=200, content_type='application/json') with open( os.path.join(os.path.dirname(__file__), '../../resources/weather_data_train.csv'), 'rb') as training_data: natural_language_classifier.create_classifier( training_data=training_data, metadata='{"language": "en"}') assert responses.calls[3].request.url == create_url assert responses.calls[3].response.text == create_response remove_url = status_url remove_response = '{}' responses.add(responses.DELETE, remove_url, body=remove_response, status=200, content_type='application/json') natural_language_classifier.delete_classifier('497EF2-nlc-00') assert responses.calls[4].request.url == remove_url assert responses.calls[4].response.text == remove_response assert len(responses.calls) == 5