Search in sources :

Example 1 with CreateClassifierOptions

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();
    }
}
Also used : CreateClassifierOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions) Classifier(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier) File(java.io.File) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 2 with CreateClassifierOptions

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) CreateClassifierOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions) Classifier(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier) File(java.io.File) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 3 with CreateClassifierOptions

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);
}
Also used : CreateClassifierOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 4 with 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);
}
Also used : CreateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) UpdateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) ClassifiedImages(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages) File(java.io.File)

Example 5 with CreateClassifierOptions

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CreateClassifierOptions(com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)6 Test (org.junit.Test)5 CreateClassifierOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 Classifier (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier)2 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)2 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)2 Classifier (com.ibm.watson.natural_language_classifier.v1.model.Classifier)2 CreateClassifierOptions (com.ibm.watson.natural_language_classifier.v1.model.CreateClassifierOptions)2 Classifier (com.ibm.watson.visual_recognition.v3.model.Classifier)2 HashMap (java.util.HashMap)2 Test (org.testng.annotations.Test)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)1 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)1 ClassifiedImages (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages)1 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)1 UpdateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.UpdateClassifierOptions)1