use of com.ibm.watson.visual_recognition.v3.model.Classifier 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().trainingMetadata(metadata).trainingData(trainingData).build();
Classifier classifier = service.createClassifier(createOptions).execute().getResult();
try {
assertNotNull(classifier);
Assert.assertEquals(Classifier.Status.TRAINING, classifier.getStatus());
assertEquals("test-classifier", classifier.getName());
assertEquals("en", classifier.getLanguage());
} finally {
classifierId = classifier.getClassifierId();
}
}
use of com.ibm.watson.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifier method createClassifier.
/**
* Create classifier.
*
* <p>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 result of type {@link Classifier}
*/
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/classifiers"));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("natural_language_classifier", "v1", "createClassifier");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
okhttp3.RequestBody trainingMetadataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingMetadata(), "application/json");
multipartBuilder.addFormDataPart("training_metadata", "filename", trainingMetadataBody);
okhttp3.RequestBody trainingDataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingData(), "text/csv");
multipartBuilder.addFormDataPart("training_data", "filename", trainingDataBody);
builder.body(multipartBuilder.build());
ResponseConverter<Classifier> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Classifier>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierTest method testCreateClassifierWOptions.
@Test
public void testCreateClassifierWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"name\": \"name\", \"url\": \"url\", \"status\": \"Non Existent\", \"classifier_id\": \"classifierId\", \"created\": \"2019-01-01T12:00:00.000Z\", \"status_description\": \"statusDescription\", \"language\": \"language\"}";
String createClassifierPath = "/v1/classifiers";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the CreateClassifierOptions model
CreateClassifierOptions createClassifierOptionsModel = new CreateClassifierOptions.Builder().trainingMetadata(TestUtilities.createMockStream("This is a mock file.")).trainingData(TestUtilities.createMockStream("This is a mock file.")).build();
// Invoke operation with valid options model (positive test)
Response<Classifier> response = naturalLanguageClassifierService.createClassifier(createClassifierOptionsModel).execute();
assertNotNull(response);
Classifier responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNull(query);
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, createClassifierPath);
}
use of com.ibm.watson.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testListClassifiers.
/**
* Test list all the classifiers.
*/
@Ignore
@Test
public void testListClassifiers() {
ListClassifiersOptions options = new ListClassifiersOptions.Builder().verbose(true).build();
List<Classifier> classifiers = service.listClassifiers(options).execute().getResult().getClassifiers();
assertNotNull(classifiers);
assertTrue(!classifiers.isEmpty());
Classifier classifier = classifiers.get(0);
assertNotNull(classifier.getClassifierId());
assertNotNull(classifier.getName());
assertNotNull(classifier.getOwner());
assertNotNull(classifier.getStatus());
assertNotNull(classifier.getClasses());
assertNotNull(classifier.getCreated());
}
use of com.ibm.watson.visual_recognition.v3.model.Classifier in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testCreateClassifier.
/**
* Test create a classifier.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Test
public void testCreateClassifier() throws FileNotFoundException, InterruptedException {
String classifierName = "integration-test-java-sdk";
String carClassifier = "car";
File carImages = new File("src/test/resources/visual_recognition/v3/car_positive.zip");
InputStream negativeImages = new FileInputStream("src/test/resources/visual_recognition/v3/negative.zip");
CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
builder.addPositiveExamples(carClassifier, carImages);
builder.negativeExamples(negativeImages);
builder.negativeExamplesFilename("negative.zip");
Classifier newClass = service.createClassifier(builder.build()).execute().getResult();
try {
assertEquals(classifierName, newClass.getName());
boolean ready = false;
for (int x = 0; (x < 40) && !ready; x++) {
Thread.sleep(2000);
GetClassifierOptions getOptions = new GetClassifierOptions.Builder(newClass.getClassifierId()).build();
newClass = service.getClassifier(getOptions).execute().getResult();
ready = newClass.getStatus().equals(Status.READY);
}
// if it at least hasn't failed, we're probably fine
assertNotEquals(Status.FAILED, newClass.getStatus());
} finally {
DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClass.getClassifierId()).build();
service.deleteClassifier(deleteOptions).execute();
}
}
Aggregations