Search in sources :

Example 1 with Classification

use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification 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 2 with Classification

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

the class NaturalLanguageClassifier method classify.

/**
 * Classify a phrase.
 *
 * 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 response type of {@link Classification}
 */
public ServiceCall<Classification> classify(ClassifyOptions classifyOptions) {
    Validator.notNull(classifyOptions, "classifyOptions cannot be null");
    String[] pathSegments = { "v1/classifiers", "classify" };
    String[] pathParameters = { classifyOptions.classifierId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("text", classifyOptions.text());
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classification.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Classification(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification) JsonObject(com.google.gson.JsonObject)

Example 3 with Classification

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

the class NaturalLanguageClassifierExample method main.

public static void main(String[] args) {
    NaturalLanguageClassifier service = new NaturalLanguageClassifier();
    service.setUsernameAndPassword("<username>", "<password>");
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId("<classifierId>").text("Is it sunny?").build();
    Classification classification = service.classify(classifyOptions).execute();
    System.out.println(classification);
}
Also used : ClassifyOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification)

Example 4 with Classification

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

the class NaturalLanguageClassifierTest method testClassify.

/**
 * Test classify.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testClassify() throws InterruptedException {
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty(TEXT, classification.getText());
    final String path = String.format(CLASSIFY_PATH, classifierId);
    server.enqueue(jsonResponse(classification));
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(classifierId).text(classification.getText()).build();
    final Classification result = service.classify(classifyOptions).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals(path, request.getPath());
    assertEquals("POST", request.getMethod());
    assertEquals(contentJson.toString(), request.getBody().readUtf8());
    assertEquals(classification, result);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ClassifyOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Aggregations

Classification (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification)4 ClassifyOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions)3 JsonObject (com.google.gson.JsonObject)2 Test (org.junit.Test)2 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)1 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 AssumptionViolatedException (org.junit.AssumptionViolatedException)1