use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifier method createClassifier.
/**
* Create classifier.
*
* <p>Sends data to create and train a classifier and returns information about the new
* classifier.
*
* @param createClassifierOptions the {@link CreateClassifierOptions} containing the options for
* the call
* @return a {@link ServiceCall} with a result of type {@link Classifier}
*/
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/classifiers"));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("natural_language_classifier", "v1", "createClassifier");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
okhttp3.RequestBody trainingMetadataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingMetadata(), "application/json");
multipartBuilder.addFormDataPart("training_metadata", "filename", trainingMetadataBody);
okhttp3.RequestBody trainingDataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingData(), "text/csv");
multipartBuilder.addFormDataPart("training_data", "filename", trainingDataBody);
builder.body(multipartBuilder.build());
ResponseConverter<Classifier> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Classifier>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.natural_language_classifier.v1.model.Classifier 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);
}
use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.
the class NaturalLanguageClassifierTest method testCreateClassifierWOptions.
@Test
public void testCreateClassifierWOptions() 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 createClassifierPath = "/v1/classifiers";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the CreateClassifierOptions model
CreateClassifierOptions createClassifierOptionsModel = new CreateClassifierOptions.Builder().trainingMetadata(TestUtilities.createMockStream("This is a mock file.")).trainingData(TestUtilities.createMockStream("This is a mock file.")).build();
// Invoke operation with valid options model (positive test)
Response<Classifier> response = naturalLanguageClassifierService.createClassifier(createClassifierOptionsModel).execute();
assertNotNull(response);
Classifier 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, createClassifierPath);
}
use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testListClassifiers.
/**
* Test list all the classifiers.
*/
@Ignore
@Test
public void testListClassifiers() {
ListClassifiersOptions options = new ListClassifiersOptions.Builder().verbose(true).build();
List<Classifier> classifiers = service.listClassifiers(options).execute().getResult().getClassifiers();
assertNotNull(classifiers);
assertTrue(!classifiers.isEmpty());
Classifier classifier = classifiers.get(0);
assertNotNull(classifier.getClassifierId());
assertNotNull(classifier.getName());
assertNotNull(classifier.getOwner());
assertNotNull(classifier.getStatus());
assertNotNull(classifier.getClasses());
assertNotNull(classifier.getCreated());
}
use of com.ibm.watson.natural_language_classifier.v1.model.Classifier in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testCreateClassifier.
/**
* Test create a classifier.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Test
public void testCreateClassifier() throws FileNotFoundException, InterruptedException {
String classifierName = "integration-test-java-sdk";
String carClassifier = "car";
File carImages = new File("src/test/resources/visual_recognition/v3/car_positive.zip");
InputStream negativeImages = new FileInputStream("src/test/resources/visual_recognition/v3/negative.zip");
CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
builder.addPositiveExamples(carClassifier, carImages);
builder.negativeExamples(negativeImages);
builder.negativeExamplesFilename("negative.zip");
Classifier newClass = service.createClassifier(builder.build()).execute().getResult();
try {
assertEquals(classifierName, newClass.getName());
boolean ready = false;
for (int x = 0; (x < 40) && !ready; x++) {
Thread.sleep(2000);
GetClassifierOptions getOptions = new GetClassifierOptions.Builder(newClass.getClassifierId()).build();
newClass = service.getClassifier(getOptions).execute().getResult();
ready = newClass.getStatus().equals(Status.READY);
}
// if it at least hasn't failed, we're probably fine
assertNotEquals(Status.FAILED, newClass.getStatus());
} finally {
DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClass.getClassifierId()).build();
service.deleteClassifier(deleteOptions).execute();
}
}
Aggregations