Search in sources :

Example 6 with CreateClassifierOptions

use of com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.

the class NaturalLanguageClassifierTest method testNullTrainingDataFile.

/**
 * Test null training data file.
 */
@Test(expected = FileNotFoundException.class)
public void testNullTrainingDataFile() throws FileNotFoundException {
    server.enqueue(jsonResponse(classifier));
    File metadata = new File(RESOURCE + "metadata.json");
    File trainingData = new File(RESOURCE + "notfound.txt");
    CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().metadata(metadata).trainingData(trainingData).trainingDataFilename("notfound.txt").build();
    service.createClassifier(createOptions).execute();
}
Also used : CreateClassifierOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions) File(java.io.File) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 7 with CreateClassifierOptions

use of com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.

the class VisualRecognitionTest method testCreateClassifier.

/**
 * Test create classifier.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testCreateClassifier() throws IOException, InterruptedException {
    Classifier mockResponse = loadFixture(FIXTURE_CLASSIFIER, Classifier.class);
    server.enqueue(new MockResponse().setBody(mockResponse.toString()));
    // execute request
    File positiveImages = new File(IMAGE_FILE);
    File negativeImages = new File(IMAGE_FILE);
    String class1 = "class1";
    CreateClassifierOptions options = new CreateClassifierOptions.Builder().name(class1).addClass(class1, positiveImages).negativeExamples(negativeImages).build();
    Classifier serviceResponse = service.createClassifier(options).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = PATH_CLASSIFIERS + "?" + VERSION_DATE + "=2016-05-20&api_key=" + API_KEY;
    assertEquals(path, request.getPath());
    assertEquals("POST", request.getMethod());
    String body = request.getBody().readUtf8();
    String contentDisposition = "Content-Disposition: form-data; name=\"class1_positive_examples\"; filename=\"test.zip\"";
    assertTrue(body.contains(contentDisposition));
    assertTrue(body.contains("Content-Disposition: form-data; name=\"name\""));
    assertEquals(serviceResponse, mockResponse);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CreateClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) File(java.io.File) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

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

Example 9 with CreateClassifierOptions

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

Example 10 with CreateClassifierOptions

use of com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions in project java-sdk by watson-developer-cloud.

the class VisualRecognition method createClassifier.

/**
 * Create a classifier.
 *
 * <p>Train a new multi-faceted classifier on the uploaded image data. Create your custom
 * classifier with positive or negative example training images. Include at least two sets of
 * examples, either two positive example files or one positive and one negative file. You can
 * upload a maximum of 256 MB per call.
 *
 * <p>**Tips when creating:**
 *
 * <p>- If you set the **X-Watson-Learning-Opt-Out** header parameter to `true` when you create a
 * classifier, the example training images are not stored. Save your training images locally. For
 * more information, see [Data collection](#data-collection).
 *
 * <p>- Encode all names in UTF-8 if they contain non-ASCII characters (.zip and image file names,
 * and classifier and class names). The service assumes UTF-8 encoding if it encounters non-ASCII
 * characters.
 *
 * @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(), "/v3/classifiers"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("watson_vision_combined", "v3", "createClassifier");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    multipartBuilder.addFormDataPart("name", createClassifierOptions.name());
    for (Map.Entry<String, InputStream> entry : createClassifierOptions.positiveExamples().entrySet()) {
        String partName = String.format("%s_positive_examples", entry.getKey());
        okhttp3.RequestBody part = RequestUtils.inputStreamBody(entry.getValue(), "application/octet-stream");
        multipartBuilder.addFormDataPart(partName, entry.getKey() + ".zip", part);
    }
    if (createClassifierOptions.negativeExamples() != null) {
        okhttp3.RequestBody negativeExamplesBody = RequestUtils.inputStreamBody(createClassifierOptions.negativeExamples(), "application/octet-stream");
        String negativeExamplesFilename = createClassifierOptions.negativeExamplesFilename();
        if (!negativeExamplesFilename.contains(".")) {
            negativeExamplesFilename += ".zip";
        }
        multipartBuilder.addFormDataPart("negative_examples", negativeExamplesFilename, negativeExamplesBody);
    }
    builder.body(multipartBuilder.build());
    ResponseConverter<Classifier> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Classifier>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) InputStream(java.io.InputStream) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) MultipartBody(okhttp3.MultipartBody) HashMap(java.util.HashMap) Map(java.util.Map)

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