Search in sources :

Example 36 with Classifier

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

the class NaturalLanguageClassifier method createClassifier.

/**
 * Create classifier.
 *
 * <p>Sends data to create and train a classifier and returns information about the new
 * classifier.
 *
 * @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(), "/v1/classifiers"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("natural_language_classifier", "v1", "createClassifier");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    okhttp3.RequestBody trainingMetadataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingMetadata(), "application/json");
    multipartBuilder.addFormDataPart("training_metadata", "filename", trainingMetadataBody);
    okhttp3.RequestBody trainingDataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingData(), "text/csv");
    multipartBuilder.addFormDataPart("training_data", "filename", trainingDataBody);
    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) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) Classifier(com.ibm.watson.natural_language_classifier.v1.model.Classifier) MultipartBody(okhttp3.MultipartBody)

Example 37 with Classifier

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

the class NaturalLanguageClassifier method classify.

/**
 * Classify a phrase.
 *
 * <p>Returns label information for the input. The status must be `Available` before you can use
 * the classifier to classify text.
 *
 * @param classifyOptions the {@link ClassifyOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Classification}
 */
public ServiceCall<Classification> classify(ClassifyOptions classifyOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(classifyOptions, "classifyOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("classifier_id", classifyOptions.classifierId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/classifiers/{classifier_id}/classify", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("natural_language_classifier", "v1", "classify");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("text", classifyOptions.text());
    builder.bodyJson(contentJson);
    ResponseConverter<Classification> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Classification>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Classification(com.ibm.watson.natural_language_classifier.v1.model.Classification) JsonObject(com.google.gson.JsonObject)

Example 38 with Classifier

use of com.ibm.watson.natural_language_classifier.v1.model.Classifier 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 39 with Classifier

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

the class VisualRecognitionIT method testListClassifiers.

/**
 * Test list all the classifiers.
 */
@Ignore
@Test
public void testListClassifiers() {
    ListClassifiersOptions options = new ListClassifiersOptions.Builder().verbose(true).build();
    List<Classifier> classifiers = service.listClassifiers(options).execute().getResult().getClassifiers();
    assertNotNull(classifiers);
    assertTrue(!classifiers.isEmpty());
    Classifier classifier = classifiers.get(0);
    assertNotNull(classifier.getClassifierId());
    assertNotNull(classifier.getName());
    assertNotNull(classifier.getOwner());
    assertNotNull(classifier.getStatus());
    assertNotNull(classifier.getClasses());
    assertNotNull(classifier.getCreated());
}
Also used : ListClassifiersOptions(com.ibm.watson.visual_recognition.v3.model.ListClassifiersOptions) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) Ignore(org.junit.Ignore) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 40 with Classifier

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

the class VisualRecognitionIT method testCreateClassifier.

/**
 * Test create a classifier.
 *
 * @throws FileNotFoundException the file not found exception
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testCreateClassifier() throws FileNotFoundException, InterruptedException {
    String classifierName = "integration-test-java-sdk";
    String carClassifier = "car";
    File carImages = new File("src/test/resources/visual_recognition/v3/car_positive.zip");
    InputStream negativeImages = new FileInputStream("src/test/resources/visual_recognition/v3/negative.zip");
    CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
    builder.addPositiveExamples(carClassifier, carImages);
    builder.negativeExamples(negativeImages);
    builder.negativeExamplesFilename("negative.zip");
    Classifier newClass = service.createClassifier(builder.build()).execute().getResult();
    try {
        assertEquals(classifierName, newClass.getName());
        boolean ready = false;
        for (int x = 0; (x < 40) && !ready; x++) {
            Thread.sleep(2000);
            GetClassifierOptions getOptions = new GetClassifierOptions.Builder(newClass.getClassifierId()).build();
            newClass = service.getClassifier(getOptions).execute().getResult();
            ready = newClass.getStatus().equals(Status.READY);
        }
        // if it at least hasn't failed, we're probably fine
        assertNotEquals(Status.FAILED, newClass.getStatus());
    } finally {
        DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClass.getClassifierId()).build();
        service.deleteClassifier(deleteOptions).execute();
    }
}
Also used : DeleteClassifierOptions(com.ibm.watson.visual_recognition.v3.model.DeleteClassifierOptions) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Classifier(com.ibm.watson.visual_recognition.v3.model.Classifier) FileInputStream(java.io.FileInputStream) GetClassifierOptions(com.ibm.watson.visual_recognition.v3.model.GetClassifierOptions) CreateClassifierOptions(com.ibm.watson.visual_recognition.v3.model.CreateClassifierOptions) File(java.io.File) 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