copyright | lastupdated | ||
---|---|---|---|
|
2017-04-20 |
{:curl: #curl .ph data-hd-programlang='curl'} {:javascript: #javascript .ph data-hd-programlang='javascript'} {:java: #java .ph data-hd-programlang='java'} {:python: #python .ph data-hd-programlang='python'} {:swift: data-hd-programlang='swift'} {:new_wind{:new_window: target="_blank"} {:shortdesc: .shortdesc} {:screen: .screen} {:pre: .pre} {:codeblock: .codeblock} {:download: .download} {:tip: .tip}
{: #natural-language-classifier}
The {{site.data.keyword.nlclassifierfull}} service can help your application understand the language of short texts, and make predictions about how to handle them. A classifier learns from your example data and then can return information for texts that it is not trained on. {:shortdesc}
You can create and train a {{site.data.keyword.nlclassifiershort}} in less than 15 minutes.
If you already know your credentials for the {{site.data.keyword.nlclassifiershort}} service instance, skip this step. {: tip}
- Go to the {{site.data.keyword.nlclassifiershort}} service and either sign up for a free {{site.data.keyword.Bluemix_notm}} account or log in.
- After you log in, type
Classifier-tutorial
in the Service name field of the {{site.data.keyword.nlclassifiershort}} page. Click Create. - Copy your credentials:
- Click Service credentials.
- Click View credentials in the "Service Credentials" section.
- Copy the
username
andpassword
values.
The classifier learns from examples before it can return information for texts that it hasn't seen before. The example data is referred to as "training data." You upload the training data when you create a classifier.
-
Download the sample weather_data_train.csv. This is the same training data that is used in the demo {:new_window}.
The file is in a CSV format in two columns. The first column is the text input. The second column is the class for that text: temperature or condition. View the file to see the entries.
-
Issue the following command to call the
POST /classifiers/
method, which uploads the training data and creates the classifier:- Replace
{username}
and{password}
with the service credentials you copied in the previous step. - Modify the location of the training data to point to where you saved the
weather_data_train.csv
file.
curl -i --user "{username}":"{password}" -F training_data=@{path_to_file}/weather_data_train.csv -F training_metadata="{\"language\":\"en\",\"name\":\"TutorialClassifier\"}" "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers"
{: pre}
The response includes a new classifier ID and status. For example:
{ "name": "TutorialClassifier", "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" }
Training begins immediately and must finish before you can query the classifier.
- Replace
-
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{username}
,{password}
, and{classifier_id}
with your information:curl --user "{username}":"{password}" "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/{classifier_id}"
{: pre}
-
Now that the classifier is trained, you can query it.
-
Classify some weather-related questions to review how your newly trained classifier responds. Here is an example call. Replace
{username}
,{password}
, and{classifier_id}
with your information:curl -G --user "{username}":"{password}" "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/{classifier_id}/classify" --data-urlencode "text=How hot will it be today?"
{: pre}
The API returns a response that includes the name of the class for which the classifier has the highest confidence. Other class-confidence pairs are listed in descending order of confidence:
{ "classifier_id": "10D41B-nlc-1", "url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1", "text": "How hot will it be today?", "top_class": "temperature", "classes": [ { "class_name": "temperature", "confidence": 0.9998201258549781 }, { "class_name": "conditions", "confidence": 0.00017987414502176904 } ] }
The confidence value represents a percentage, and higher values represent higher confidences. The response includes up to 10 classes.
If you have fewer than 10 classes in your training data, the sum of all confidence values is 100%. In this sample training data, only two classes are defined, so only two can be returned.
-
Review the top class for the questions to see how the classifier is predicting them.
Here are some other sample questions to classify:
- Is it hot outside?
- Will it be windy?
- Will we see some sun?
- What is the expected high for today?
- Will it be foggy tomorrow morning?
One of the sample questions includes a term ("foggy") that the classifier isn't trained on. The classifier can score well with these "missing" terms without your having to do extra work to identify them. Try other questions that include words that are not in the training data, for example, "sleet" or "storm."
You're done! You created, trained, and queried a classifier in the {{site.data.keyword.nlclassifiershort}} service.
So that you can create classifiers for your own use and with your own training data, you might want to delete this classifier from the tutorial. To delete the classifier, call the DELETE /classifiers/{classifier_id}
method.
As previously, replace {username}
, {password}
, and {classifier_id}
with your information in the following command.
curl -X DELETE --user "{username}":"{password}" "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/{classifier_id}"
{: pre}
The response to the command is an empty JSON object.
You have a basic understanding of how to use {{site.data.keyword.nlclassifiershort}}. Now dive deeper:
- Learn how to use your own data to train a classifier
- Read about the API in the API reference {:new_window}
- Interact with the API in the API explorer {:new_window}
- Look at the Node.js starter application{:new_window} for an example web app