use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionIT method testCreateClassifierAndClassifyImage.
/**
* Test create a classifier.
*
* @throws FileNotFoundException the file not found exception
* @throws InterruptedException the interrupted exception
*/
@Ignore
@Test
public void testCreateClassifierAndClassifyImage() throws FileNotFoundException, InterruptedException {
String classifierName = "integration-test-java-sdk";
String carClassifier = "car";
String baseballClassifier = "baseball";
File carImages = new File("src/test/resources/visual_recognition/car_positive.zip");
File baseballImages = new File("src/test/resources/visual_recognition/baseball_positive.zip");
File negativeImages = new File("src/test/resources/visual_recognition/negative.zip");
File imageToClassify = new File("src/test/resources/visual_recognition/car.png");
CreateClassifierOptions.Builder builder = new CreateClassifierOptions.Builder().name(classifierName);
builder.addClass(carClassifier, carImages);
builder.addClass(baseballClassifier, baseballImages);
builder.negativeExamples(negativeImages);
Classifier newClassifier = service.createClassifier(builder.build()).execute();
try {
assertEquals(classifierName, newClassifier.getName());
boolean ready = false;
for (int x = 0; (x < 20) && !ready; x++) {
Thread.sleep(2000);
GetClassifierOptions getOptions = new GetClassifierOptions.Builder(newClassifier.getClassifierId()).build();
newClassifier = service.getClassifier(getOptions).execute();
ready = newClassifier.getStatus().equals(Status.READY);
}
assertEquals(Status.READY, newClassifier.getStatus());
ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(imageToClassify).build();
ClassifiedImages classification = service.classify(options).execute();
assertNotNull(classification);
} finally {
DeleteClassifierOptions deleteOptions = new DeleteClassifierOptions.Builder(newClassifier.getClassifierId()).build();
service.deleteClassifier(deleteOptions).execute();
}
}
use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionTest method testClassifyWithBytes.
/**
* Test classify with bytes or stream.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws InterruptedException the interrupted exception
*/
@Test
public void testClassifyWithBytes() throws IOException, InterruptedException {
ClassifiedImages mockResponse = loadFixture(FIXTURE_CLASSIFICATION, ClassifiedImages.class);
server.enqueue(new MockResponse().setBody(mockResponse.toString()));
// execute request
File images = new File(SINGLE_IMAGE_FILE);
InputStream fileStream = new FileInputStream(images);
ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(fileStream).classifierIds(Arrays.asList("car")).build();
ClassifiedImages serviceResponse = service.classify(options).execute();
// first request
RecordedRequest request = server.takeRequest();
String path = PATH_CLASSIFY + "?" + VERSION_DATE + "=2016-05-20&api_key=" + API_KEY;
assertEquals(path, request.getPath());
assertEquals("POST", request.getMethod());
assertEquals(serviceResponse, mockResponse);
}
use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions in project java-sdk by watson-developer-cloud.
the class VisualRecognitionExample method main.
public static void main(String[] args) {
VisualRecognition service = new VisualRecognition("2016-05-20");
service.setApiKey("<api-key>");
System.out.println("Classify an image");
ClassifyOptions options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").build();
ClassifiedImages result = service.classify(options).execute();
System.out.println(result);
System.out.println("Create a classifier with positive and negative images");
CreateClassifierOptions createOptions = new CreateClassifierOptions.Builder().name("foo").addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).addClass("baseball", new File("src/test/resources/visual_recognition/baseball_positive.zip")).negativeExamples(new File("src/test/resources/visual_recognition/negative.zip")).build();
Classifier foo = service.createClassifier(createOptions).execute();
System.out.println(foo);
System.out.println("Classify using the 'Car' classifier");
options = new ClassifyOptions.Builder().imagesFile(new File("src/test/resources/visual_recognition/car.png")).imagesFilename("car.png").addClassifierId(foo.getClassifierId()).build();
result = service.classify(options).execute();
System.out.println(result);
System.out.println("Update a classifier with more positive images");
UpdateClassifierOptions updateOptions = new UpdateClassifierOptions.Builder().classifierId(foo.getClassifierId()).addClass("car", new File("src/test/resources/visual_recognition/car_positive.zip")).build();
Classifier updatedFoo = service.updateClassifier(updateOptions).execute();
System.out.println(updatedFoo);
}
use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions 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.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions 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