Skip to content

Train the Natural Language Classifier service

biosopher edited this page Nov 24, 2015 · 42 revisions

####1. Train Your Natural Language Classifier service

To classify a user's text input, we need to train an NLC classifier. We do this using ipa-web-nlc_training.csv which contains the user text examples mapped to predefined classes. In this step then, We'll issue the following cURL command to call the POST /classifiers/ method to upload the training data and create the classifier.

a) cd to the root directory of your project.

b) Obtain your NLC service credentials by following these steps to display Bluemix service credentials from the command line or by going through the Bluemix admin tool (if you already know how to do that).

c) Replace <nlc_service_username> and <nlc_service_password> with the NLC service credentials from the previous step.

$ curl -i -u "<nlc_service_username>:<nlc_service_password>" \
-F training_data=@nlc/ipa-web-nlc_training.csv \
-F training_metadata="{\"language\":\"en\",\"name\":\"demo_ipa\"}" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers"

The response includes a new classifier ID and status. For example:

{
"name": "demo_ipa",
"language": "en",
"status": "Training",
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/10D41B-nlc-1",
"classifier_id": "10D41B-nlc-1",
"created": "2015-05-28T18:01:57.393Z",
"status_description": "The classifier instance is in its training phase, not yet ready to accept classify requests"
}

####2. Wait for Training to Complete Training begins immediately but must finish before you can query the classifier. Check the training status periodically until you see a status of Available. With this sample data, training takes about 6 minutes:

Issue a GET /classifiers/{classifier_id} call to retrieve the status of the classifier. In the following example, replace <nlc_service_username>, <nlc_service_password>, and <classifier_id> with your information:

$ curl -i -u "<nlc_service_username>:<nlc_service_password>" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/<classifier_id>"

Once the classifier is trained, you will see something like this:

{
  "classifier_id" : "98A37Cx3-nlc-195",
  "name" : "demo_ipa",
  "language" : "en",
  "created" : "2015-10-27T17:50:04.122Z",
  "url" : "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/98A37Cx3-nlc-195",
  "status" : "Available",
  "status_description" : "The classifier instance is now available and is ready to take classifier requests."
}

####3. Testing Your Classifier from the Command Line Now that the classifier is trained, you can query it.

Classify some assistant-related questions to review how your newly trained classifier responds. Here is an example call. Replace <nlc_service_username>, <nlc_service_password>, and <classifier_id> with your information:

$ curl -G -u "<nlc_service_username>:<nlc_service_password>" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/<classifier_id>/classify" \
--data-urlencode "text=Please schedule a meeting with Sue at 5pm tomorrow"

The API returns a response that includes the name of class for which the classifier has the highest confidence. Other class-confidence pairs are listed in descending order of confidence:

{
      "classifier_id": "98A37Cx3-nlc-195",
      "url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1",
      "text": "Please schedule a meeting with Sue at 5pm tomorrow",
      "top_class": "action-meeting-create",
      "classes": [
          {
              "class_name": "action-meeting-create",
              "confidence": 0.9998201258549781
          },
          {
              "class_name": "action-email-create",
              "confidence": 0.00017987414502176904
          }
      ]
}

The confidence value represents a percentage, and higher values represent higher confidences. The response includes up to 10 classes. So for example, if you have 10 or fewer classes in your training data, the sum of all confidence values is 100%. Review the top class for the questions to see how the classifier is predicting them.

Here are some other sample questions to classify:

  • Schedule a meeting
  • Arrange a meeting at 5p
  • Make a meeting with Sue
  • I want to meet at 5pm with John

####4. Next Step? Configuring the Dialog Service! Your NLC service is now ready to accept user input. So we're over 1/2 way completed. We just need to setup the Watson Dialog service so we can have an interactive conversation with the user.


ADDITIONAL INFORMATION


For information on training classifiers, see the NLC training tutorial.

Viewing All Classifiers

Run this command to view all classifiers. Replacing <username> and <password> with your own.

curl -X GET https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers -u "<username>:<password>"

Deleting a Classifier

To delete a classifier, run this command after replacing <classifier_id> for the appropriate classifier along replacing these <nlc_service_username> and <nlc_service_password> with your.

curl -X DELETE https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/<classifier_id> -u "<nlc_service_username>:<nlc_service_password>"

#####Explore the NLC's REST APIs

Review the Natural Language Classifier's REST APIs. We also have a Swagger version of the the NLC API. There are many more options to explore.