Search in sources :

Example 1 with ClassifyCollectionOptions

use of com.ibm.watson.natural_language_classifier.v1.model.ClassifyCollectionOptions 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)

Example 2 with ClassifyCollectionOptions

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

the class NaturalLanguageClassifierTest method testClassifyCollectionWOptions.

@Test
public void testClassifyCollectionWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"classifier_id\": \"classifierId\", \"url\": \"url\", \"collection\": [{\"text\": \"text\", \"top_class\": \"topClass\", \"classes\": [{\"confidence\": 10, \"class_name\": \"className\"}]}]}";
    String classifyCollectionPath = "/v1/classifiers/testString/classify_collection";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the ClassifyInput model
    ClassifyInput classifyInputModel = new ClassifyInput.Builder().text("How hot will it be today?").build();
    // Construct an instance of the ClassifyCollectionOptions model
    ClassifyCollectionOptions classifyCollectionOptionsModel = new ClassifyCollectionOptions.Builder().classifierId("testString").collection(new java.util.ArrayList<ClassifyInput>(java.util.Arrays.asList(classifyInputModel))).build();
    // Invoke operation with valid options model (positive test)
    Response<ClassificationCollection> response = naturalLanguageClassifierService.classifyCollection(classifyCollectionOptionsModel).execute();
    assertNotNull(response);
    ClassificationCollection 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, classifyCollectionPath);
}
Also used : ClassificationCollection(com.ibm.watson.natural_language_classifier.v1.model.ClassificationCollection) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ClassifyCollectionOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyCollectionOptions) ClassifyInput(com.ibm.watson.natural_language_classifier.v1.model.ClassifyInput) Test(org.testng.annotations.Test)

Example 3 with ClassifyCollectionOptions

use of com.ibm.watson.natural_language_classifier.v1.model.ClassifyCollectionOptions 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)

Example 4 with ClassifyCollectionOptions

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

the class NaturalLanguageClassifierTest method testClassifyCollection.

/**
 * Test classifying a collection.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testClassifyCollection() throws InterruptedException {
    final String path = String.format(CLASSIFY_COLLECTION_PATH, classifierId);
    server.enqueue(jsonResponse(classificationCollection));
    ClassifyInput input1 = new ClassifyInput();
    input1.setText("How hot will it be today?");
    ClassifyInput input2 = new ClassifyInput();
    input2.setText("Is it hot outside?");
    List<ClassifyInput> inputCollection = Arrays.asList(input1, input2);
    ClassifyCollectionOptions classifyOptions = new ClassifyCollectionOptions.Builder().classifierId(classifierId).collection(inputCollection).build();
    final ClassificationCollection result = service.classifyCollection(classifyOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals(path, request.getPath());
    assertEquals("POST", request.getMethod());
    assertEquals(classificationCollection, result);
}
Also used : ClassificationCollection(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassificationCollection) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ClassifyCollectionOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyCollectionOptions) ClassifyInput(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyInput) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 5 with ClassifyCollectionOptions

use of com.ibm.watson.natural_language_classifier.v1.model.ClassifyCollectionOptions 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)

Aggregations

ClassificationCollection (com.ibm.watson.natural_language_classifier.v1.model.ClassificationCollection)3 Test (org.junit.Test)3 ClassificationCollection (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassificationCollection)2 ClassifyCollectionOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyCollectionOptions)2 ClassifyInput (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyInput)2 ClassifyCollectionOptions (com.ibm.watson.natural_language_classifier.v1.model.ClassifyCollectionOptions)2 ClassifyInput (com.ibm.watson.natural_language_classifier.v1.model.ClassifyInput)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 AssumptionViolatedException (org.junit.AssumptionViolatedException)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)1 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)1 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)1 HashMap (java.util.HashMap)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Test (org.testng.annotations.Test)1