How to train natural language classifier using Fluent

517 Views Asked by At

I'm using Fluent library to fire a request to Natural Language Classifier service so as to 'train' the data.

Documentation says following parameters to be passed:

name=training_data; type=file; description=training data
name=training_meta_data; type=file; description=meta data to identify language etc

Below is my code sample:

File trainingCSVFile = new File("path to training file");
Request request=Request.Post(<bluemix service url>).
  bodyFile(trainingCSVFile, ContentType.TEXT_PLAIN).
  bodyString("{\"language\":\"en\",\"name\":\"PaymentDataClassifier\"}", ContentType.APPLICATION_JSON);

How ever am getting internal server error which plausibly due to my request format. Can any one help me how to pass the above mentioned parameters using Fluent library on priority?

1

There are 1 best solutions below

0
On

I'm going to assume that you are using Java and suggest you to use the Java SDK. You can find examples to use not only Natural language Classifier but all the Watson services + Alchemy services.

Installation

  1. Download the jar

  2. or use Maven

    <dependency>
        <groupId>com.ibm.watson.developer_cloud</groupId>
        <artifactId>java-sdk</artifactId>
        <version>2.10.0</version>
    </dependency>
    
  3. or use Gradle

    'com.ibm.watson.developer_cloud:java-sdk:2.10.0'
    

The code snippet to create a classifier is:

NaturalLanguageClassifier service = new NaturalLanguageClassifier();
service.setUsernameAndPassword("<username>", "<password>");

File trainingData = new File("/path/to/csv/file.csv");
Classifier classifier = service.createClassifier("PaymentDataClassifier", "en", trainingData);

System.out.println(classifier);

The training duration will depend on your data but once it's trained you can do:

Classification classification = service.classify(classifier.getId(), "Is it sunny?");
System.out.println(classification);

Feel free to open an issue in the GitHub repo if you have problems