use of com.ibm.watson.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());
}
use of com.ibm.watson.natural_language_classifier.v1.model.Classification in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierTest method testClassifyWOptions.
@Test
public void testClassifyWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"classifier_id\": \"classifierId\", \"url\": \"url\", \"text\": \"text\", \"top_class\": \"topClass\", \"classes\": [{\"confidence\": 10, \"class_name\": \"className\"}]}";
String classifyPath = "/v1/classifiers/testString/classify";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the ClassifyOptions model
ClassifyOptions classifyOptionsModel = new ClassifyOptions.Builder().classifierId("testString").text("testString").build();
// Invoke operation with valid options model (positive test)
Response<Classification> response = naturalLanguageClassifierService.classify(classifyOptionsModel).execute();
assertNotNull(response);
Classification 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, classifyPath);
}
use of com.ibm.watson.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().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());
}
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.
*
* 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));
}
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) {
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);
}
Aggregations