use of com.ibm.watson.visual_recognition.v3.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.visual_recognition.v3.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.visual_recognition.v3.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.visual_recognition.v3.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);
}
use of com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionTest method testCreateClassifierWOptions.
@Test
public void testCreateClassifierWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"classifier_id\": \"classifierId\", \"name\": \"name\", \"owner\": \"owner\", \"status\": \"ready\", \"core_ml_enabled\": false, \"explanation\": \"explanation\", \"created\": \"2019-01-01T12:00:00.000Z\", \"classes\": [{\"class\": \"xClass\"}], \"retrained\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}";
String createClassifierPath = "/v3/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().name("testString").positiveExamples(mockStreamMap).negativeExamples(TestUtilities.createMockStream("This is a mock file.")).negativeExamplesFilename("testString").build();
// Invoke operation with valid options model (positive test)
Response<Classifier> response = visualRecognitionService.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);
assertNotNull(query);
// Get query params
assertEquals(query.get("version"), "testString");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, createClassifierPath);
}
Aggregations