use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierIT method aCreate.
/**
* Creates the classifier.
*
* @throws Exception the exception
*/
@Test
public void aCreate() throws Exception {
final File trainingData = new File("src/test/resources/natural_language_classifier/weather_data_train.csv");
final File metadata = new File("src/test/resources/natural_language_classifier/metadata.json");
CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().metadata(metadata).trainingData(trainingData).trainingDataFilename("weather_data_train.csv").build();
Classifier classifier = service.createClassifier(createOptions).execute();
try {
assertNotNull(classifier);
assertEquals(Status.TRAINING, classifier.getStatus());
assertEquals("test-classifier", classifier.getName());
assertEquals("en", classifier.getLanguage());
} finally {
classifierId = classifier.getClassifierId();
}
}
use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierTest method testCreateClassifier.
/**
* Test create classifier.
*
* @throws InterruptedException the interrupted exception
*/
@Test
public void testCreateClassifier() throws InterruptedException, FileNotFoundException {
server.enqueue(jsonResponse(classifier));
File metadata = new File(RESOURCE + "metadata.json");
File trainingData = new File(RESOURCE + "weather_data_train.csv");
CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().metadata(metadata).trainingData(trainingData).trainingDataFilename("weather_data_train.csv").build();
final Classifier response = service.createClassifier(createOptions).execute();
final RecordedRequest request = server.takeRequest();
assertEquals(CLASSIFIERS_PATH, request.getPath());
assertEquals(classifier, response);
}
use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifier method createClassifier.
/**
* Create classifier.
*
* This method is here for backwards-compatibility with the old version of createClassifier.
*
* @param name the classifier name
* @param language IETF primary language for the classifier. for example: 'en'
* @param trainingData the set of questions and their "keys" used to adapt a system to a domain (the ground truth)
* @return the classifier
* @throws FileNotFoundException if the file could not be found
*/
public ServiceCall<Classifier> createClassifier(String name, String language, File trainingData) throws FileNotFoundException {
Map<String, String> metadataMap = new HashMap<>();
metadataMap.put("name", name);
metadataMap.put("language", language);
String metadataString = GsonSingleton.getGson().toJson(metadataMap);
CreateClassifierOptions createClassifierOptions = new CreateClassifierOptions.Builder().metadata(new ByteArrayInputStream(metadataString.getBytes())).trainingData(trainingData).build();
return createClassifier(createClassifierOptions);
}
use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifier method createClassifier.
/**
* Create classifier.
*
* Sends data to create and train a classifier and returns information about the new classifier.
*
* @param createClassifierOptions the {@link CreateClassifierOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Classifier}
*/
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
String[] pathSegments = { "v1/classifiers" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
RequestBody trainingMetadataBody = RequestUtils.inputStreamBody(createClassifierOptions.metadata(), "application/json");
multipartBuilder.addFormDataPart("training_metadata", createClassifierOptions.metadataFilename(), trainingMetadataBody);
RequestBody trainingDataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingData(), "text/csv");
multipartBuilder.addFormDataPart("training_data", createClassifierOptions.trainingDataFilename(), trainingDataBody);
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionExample method main.
public static void main(String[] args) {
VisualRecognition service = new VisualRecognition("2016-05-20");
service.setApiKey("<api-key>");
System.out.println("Classify an image");
ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").build();
ClassifiedImages result = service.classify(options).execute();
System.out.println(result);
System.out.println("Create a classifier with positive and negative images");
CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().name("foo").addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).addClass("baseball", new File("src/test/resources/visual_recognition/baseball_positive.zip")).negativeExamples(new File("src/test/resources/visual_recognition/negative.zip")).build();
Classifier foo = service.createClassifier(createOptions).execute();
System.out.println(foo);
System.out.println("Classify using the 'Car' classifier");
options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").addClassifierId(foo.getClassifierId()).build();
result = service.classify(options).execute();
System.out.println(result);
System.out.println("Update a classifier with more positive images");
UpdateClassifierOptions updateOptions = new UpdateClassifierOptions.Builder().classifierId(foo.getClassifierId()).addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).build();
Classifier updatedFoo = service.updateClassifier(updateOptions).execute();
System.out.println(updatedFoo);
}
Aggregations