Search in sources :

Example 31 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class VisualRecognitionTest method testGetClassifier.

/**
 * Test get classifier.
 *
 * @throws InterruptedException the interrupted exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testGetClassifier() throws InterruptedException, IOException {
    try {
        Classifier mockResponse = loadFixture(FIXTURE_CLASSIFIER, Classifier.class);
        server.enqueue(new MockResponse().setBody(mockResponse.toString()));
        // execute request
        String class1 = "class1";
        GetClassifierOptions getOptions = new GetClassifierOptions.Builder(class1).build();
        Classifier serviceResponse = service.getClassifier(getOptions).execute();
        // first request
        RecordedRequest request = server.takeRequest();
        String path = String.format(PATH_CLASSIFIER + "?" + VERSION_DATE + "=2016-05-20&api_key=" + API_KEY, class1);
        assertEquals(path, request.getPath());
        assertEquals("GET", request.getMethod());
        assertEquals(serviceResponse, mockResponse);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetClassifierOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.GetClassifierOptions) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) IOException(java.io.IOException) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 32 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class VisualRecognition method createClassifier.

/**
 * Create a classifier.
 *
 * Train a new multi-faceted classifier on the uploaded image data. Create your custom classifier with positive or
 * negative examples. 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. 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 response type of {@link Classifier}
 */
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
    Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
    String[] pathSegments = { "v3/classifiers" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.query(VERSION, versionDate);
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    multipartBuilder.addFormDataPart("name", createClassifierOptions.name());
    // Classes
    for (String className : createClassifierOptions.classNames()) {
        String dataName = className + "_positive_examples";
        File positiveExamples = createClassifierOptions.positiveExamplesByClassName(className);
        RequestBody body = RequestUtils.fileBody(positiveExamples, "application/octet-stream");
        multipartBuilder.addFormDataPart(dataName, positiveExamples.getName(), body);
    }
    if (createClassifierOptions.negativeExamples() != null) {
        RequestBody negativeExamplesBody = RequestUtils.inputStreamBody(createClassifierOptions.negativeExamples(), "application/octet-stream");
        multipartBuilder.addFormDataPart("negative_examples", createClassifierOptions.negativeExamplesFilename(), negativeExamplesBody);
    }
    builder.body(multipartBuilder.build());
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 33 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.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();
    }
}
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 34 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class NaturalLanguageClassifierIT method fClassifyCollection.

/**
 * Test classifyCollection. Use the pre created classifier to avoid waiting for availability
 */
@Test
public void fClassifyCollection() {
    ClassificationCollection classificationCollection = null;
    ClassifyInput input1 = new ClassifyInput.Builder().text("How hot will it be today?").build();
    ClassifyInput input2 = new ClassifyInput.Builder().text("Is it hot outside?").build();
    try {
        ClassifyCollectionOptions classifyOptions = new ClassifyCollectionOptions.Builder().classifierId(preCreatedClassifierId).addClassifyInput(input1).addClassifyInput(input2).build();
        classificationCollection = service.classifyCollection(classifyOptions).execute().getResult();
    } catch (NotFoundException e) {
        // The build should not fail here, because this is out of our control.
        throw new AssumptionViolatedException(e.getMessage(), e);
    }
    assertNotNull(classificationCollection);
    assertEquals("temperature", classificationCollection.getCollection().get(0).getTopClass());
    assertEquals("temperature", classificationCollection.getCollection().get(1).getTopClass());
}
Also used : ClassificationCollection(com.ibm.watson.natural_language_classifier.v1.model.ClassificationCollection) ClassifyCollectionOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyCollectionOptions) AssumptionViolatedException(org.junit.AssumptionViolatedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) ClassifyInput(com.ibm.watson.natural_language_classifier.v1.model.ClassifyInput) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 35 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.

the class NaturalLanguageClassifierIT method eDelete.

/**
 * Test delete classifier. Only delete the classifier we created earlier.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void eDelete() throws InterruptedException {
    DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder().classifierId(classifierId).build();
    service.deleteClassifier(deleteOptions).execute();
}
Also used : DeleteClassifierOptions(com.ibm.watson.natural_language_classifier.v1.model.DeleteClassifierOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)24 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)13 File (java.io.File)12 Classifier (com.ibm.watson.visual_recognition.v3.model.Classifier)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)11 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)10 MockResponse (okhttp3.mockwebserver.MockResponse)9 Ignore (org.junit.Ignore)9 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)8 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)7 Classifier (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier)7 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)6 Classifier (com.ibm.watson.natural_language_classifier.v1.model.Classifier)6 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 MultipartBody (okhttp3.MultipartBody)6 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)5 Test (org.testng.annotations.Test)5 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)4 FileInputStream (java.io.FileInputStream)4