Search in sources :

Example 11 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 testGetClassifierWOptions.

@Test
public void testGetClassifierWOptions() 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 getClassifierPath = "/v1/classifiers/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetClassifierOptions model
    GetClassifierOptions getClassifierOptionsModel = new GetClassifierOptions.Builder().classifierId("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<Classifier> response = naturalLanguageClassifierService.getClassifier(getClassifierOptionsModel).execute();
    assertNotNull(response);
    Classifier responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getClassifierPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetClassifierOptions(com.ibm.watson.natural_language_classifier.v1.model.GetClassifierOptions) Classifier(com.ibm.watson.natural_language_classifier.v1.model.Classifier) Test(org.testng.annotations.Test)

Example 12 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 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().getResult();
    } 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.natural_language_classifier.v1.model.Classification) ClassifyOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 13 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 bGetClassifier.

/**
 * Test get classifier.
 */
@Test
public void bGetClassifier() {
    final Classifier classifier;
    try {
        GetClassifierOptions getOptions = new GetClassifierOptions.Builder().classifierId(classifierId).build();
        classifier = service.getClassifier(getOptions).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(classifier);
    assertEquals(classifierId, classifier.getClassifierId());
    assertEquals(Classifier.Status.TRAINING, classifier.getStatus());
}
Also used : GetClassifierOptions(com.ibm.watson.natural_language_classifier.v1.model.GetClassifierOptions) AssumptionViolatedException(org.junit.AssumptionViolatedException) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Classifier(com.ibm.watson.natural_language_classifier.v1.model.Classifier) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 14 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 getClassifier.

/**
 * Get information about a classifier.
 *
 * <p>Returns status and other information about a classifier.
 *
 * @param getClassifierOptions the {@link GetClassifierOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link Classifier}
 */
public ServiceCall<Classifier> getClassifier(GetClassifierOptions getClassifierOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getClassifierOptions, "getClassifierOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("classifier_id", getClassifierOptions.classifierId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/classifiers/{classifier_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("natural_language_classifier", "v1", "getClassifier");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    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) HashMap(java.util.HashMap) Classifier(com.ibm.watson.natural_language_classifier.v1.model.Classifier)

Example 15 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 classifyCollection.

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

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