Search in sources :

Example 1 with Classifier

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

the class VisualRecognition method updateClassifier.

/**
 * Update a classifier.
 *
 * Update a custom classifier by adding new positive or negative classes (examples) or by adding new images to
 * existing classes. You must supply at least one set of positive or negative examples. For details, see [Updating
 * custom
 * classifiers]
 * (https://console.bluemix.net/docs/services/visual-recognition/customizing.html#updating-custom-classifiers).
 * 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. **Important:** You can't update a
 * custom classifier with an API key for a Lite plan. To update a custom classifier on a Lite plan, create another
 * service instance on a Standard plan and re-create your custom classifier. **Tip:** Don't make retraining calls on a
 * classifier until the status is ready. When you submit retraining requests in parallel, the last request overwrites
 * the previous requests. The retrained property shows the last time the classifier retraining finished.
 *
 * @param updateClassifierOptions the {@link UpdateClassifierOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Classifier}
 */
public ServiceCall<Classifier> updateClassifier(UpdateClassifierOptions updateClassifierOptions) {
    Validator.notNull(updateClassifierOptions, "updateClassifierOptions cannot be null");
    Validator.isTrue((updateClassifierOptions.classNames().size() > 0) || (updateClassifierOptions.negativeExamples() != null), "At least one of classnamePositiveExamples or negativeExamples must be supplied.");
    String[] pathSegments = { "v3/classifiers" };
    String[] pathParameters = { updateClassifierOptions.classifierId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    // Classes
    for (String className : updateClassifierOptions.classNames()) {
        String dataName = className + "_positive_examples";
        File positiveExamples = updateClassifierOptions.positiveExamplesByClassName(className);
        RequestBody body = RequestUtils.fileBody(positiveExamples, "application/octet-stream");
        multipartBuilder.addFormDataPart(dataName, positiveExamples.getName(), body);
    }
    if (updateClassifierOptions.negativeExamples() != null) {
        RequestBody negativeExamplesBody = RequestUtils.inputStreamBody(updateClassifierOptions.negativeExamples(), "application/octet-stream");
        multipartBuilder.addFormDataPart("negative_examples", updateClassifierOptions.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 2 with Classifier

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

the class VisualRecognition method getClassifier.

/**
 * Retrieve classifier details.
 *
 * Retrieve information about a custom classifier.
 *
 * @param getClassifierOptions the {@link GetClassifierOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Classifier}
 */
public ServiceCall<Classifier> getClassifier(GetClassifierOptions getClassifierOptions) {
    Validator.notNull(getClassifierOptions, "getClassifierOptions cannot be null");
    String[] pathSegments = { "v3/classifiers" };
    String[] pathParameters = { getClassifierOptions.classifierId() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Classifier(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)

Example 3 with Classifier

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

the class NaturalLanguageClassifierIT method dClassify.

/**
 * Test classify. Use the pre created classifier to avoid waiting for availability
 */
@Test
public void dClassify() {
    Classification classification = null;
    try {
        ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(preCreatedClassifierId).text("is it hot outside?").build();
        classification = service.classify(classifyOptions).execute();
    } catch (NotFoundException e) {
        // The build should not fail here, because this is out of our control.
        throw new AssumptionViolatedException(e.getMessage(), e);
    }
    assertNotNull(classification);
    assertEquals("temperature", classification.getTopClass());
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) Classification(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification) ClassifyOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 4 with Classifier

use of com.ibm.watson.developer_cloud.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().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 5 with Classifier

use of com.ibm.watson.developer_cloud.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();
    input1.setText("How hot will it be today?");
    ClassifyInput input2 = new ClassifyInput();
    input2.setText("Is it hot outside?");
    try {
        ClassifyCollectionOptions classifyOptions = new ClassifyCollectionOptions.Builder().classifierId(preCreatedClassifierId).addClassifyInput(input1).addClassifyInput(input2).build();
        classificationCollection = service.classifyCollection(classifyOptions).execute();
    } 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.developer_cloud.natural_language_classifier.v1.model.ClassificationCollection) ClassifyCollectionOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyCollectionOptions) AssumptionViolatedException(org.junit.AssumptionViolatedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ClassifyInput(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyInput) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Aggregations

Test (org.junit.Test)20 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)13 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)10 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)10 File (java.io.File)10 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)7 Classifier (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 Ignore (org.junit.Ignore)5 CreateClassifierOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.CreateClassifierOptions)4 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 JsonObject (com.google.gson.JsonObject)3 DeleteClassifierOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.DeleteClassifierOptions)3 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)3 DeleteClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DeleteClassifierOptions)3 GetClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.GetClassifierOptions)3 ListClassifiersOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ListClassifiersOptions)3 MultipartBody (okhttp3.MultipartBody)3 RequestBody (okhttp3.RequestBody)3