Search in sources :

Example 6 with Classification

use of com.ibm.watson.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)

Example 7 with Classification

use of com.ibm.watson.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) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    NaturalLanguageClassifier service = new NaturalLanguageClassifier(authenticator);
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId("<classifierId>").text("Is it sunny?").build();
    Classification classification = service.classify(classifyOptions).execute().getResult();
    System.out.println(classification);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) ClassifyOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.natural_language_classifier.v1.model.Classification) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Example 8 with Classification

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

Aggregations

Classification (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification)4 Classification (com.ibm.watson.natural_language_classifier.v1.model.Classification)4 JsonObject (com.google.gson.JsonObject)3 ClassifyOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions)3 ClassifyOptions (com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions)3 Test (org.junit.Test)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 AssumptionViolatedException (org.junit.AssumptionViolatedException)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)1 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)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 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)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