Search in sources :

Example 1 with ClassifyOptions

use of com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions in project jbpm-work-items by kiegroup.

the class ClassifyImageWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Document classificationImage = (Document) workItem.getParameter("ImageToClassify");
    Map<String, Object> widResults = new HashMap<String, Object>();
    if (classificationImage != null) {
        try {
            VisualRecognition service = auth.getService(apiKey);
            ByteArrayInputStream imageStream = new ByteArrayInputStream(classificationImage.getContent());
            ClassifyOptions classifyOptions = new ClassifyOptions.Builder().imagesFile(imageStream).imagesFilename(classificationImage.getName()).parameters("{\"owners\": [\"me\"]}").build();
            ClassifiedImage result = service.classify(classifyOptions).execute().getImages().get(0);
            if (result.getError() != null) {
                ErrorInfo errorInfo = result.getError();
                logger.error("Error classifying image: " + errorInfo.getDescription());
                workItemManager.abortWorkItem(workItem.getId());
            } else {
                List<ImageClassificationResult> resultList = new ArrayList<>();
                for (ClassifierResult classification : result.getClassifiers()) {
                    for (ClassResult classResult : classification.getClasses()) {
                        resultList.add(new ImageClassificationResult(classification, classResult));
                    }
                    widResults.put(RESULT_VALUE, resultList);
                }
                workItemManager.completeWorkItem(workItem.getId(), widResults);
            }
        } catch (Exception e) {
            handleException(e);
        }
    } else {
        logger.error("Missing image for classification.");
        throw new IllegalArgumentException("Missing image for classification.");
    }
}
Also used : HashMap(java.util.HashMap) VisualRecognition(com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition) ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ErrorInfo(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ErrorInfo) ArrayList(java.util.ArrayList) Document(org.jbpm.document.Document) ImageClassificationResult(org.jbpm.process.workitem.ibm.watson.result.ImageClassificationResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassifiedImage(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage) ClassifierResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult) ClassResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)

Example 2 with ClassifyOptions

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

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

the class NaturalLanguageClassifierIT method fClassifyCollection.

/**
 * Test classifyCollection. Use the pre created classifier to avoid waiting for availability
 */
@Test
public void fClassifyCollection() {
    ClassificationCollection classificationCollection = null;
    ClassifyInput input1 = new ClassifyInput();
    input1.setText("How hot will it be today?");
    ClassifyInput input2 = new ClassifyInput();
    input2.setText("Is it hot outside?");
    try {
        ClassifyCollectionOptions classifyOptions = new ClassifyCollectionOptions.Builder().classifierId(preCreatedClassifierId).addClassifyInput(input1).addClassifyInput(input2).build();
        classificationCollection = service.classifyCollection(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(classificationCollection);
    assertEquals("temperature", classificationCollection.getCollection().get(0).getTopClass());
    assertEquals("temperature", classificationCollection.getCollection().get(1).getTopClass());
}
Also used : ClassificationCollection(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassificationCollection) ClassifyCollectionOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyCollectionOptions) AssumptionViolatedException(org.junit.AssumptionViolatedException) NotFoundException(com.ibm.watson.developer_cloud.service.exception.NotFoundException) ClassifyInput(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyInput) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 4 with ClassifyOptions

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

the class NaturalLanguageClassifierTest method testNullText.

/**
 * Test null text.
 */
@Test(expected = IllegalArgumentException.class)
public void testNullText() {
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId(classifierId).build();
    service.classify(classifyOptions);
}
Also used : ClassifyOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 5 with ClassifyOptions

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

the class NaturalLanguageClassifierTest method testNullClassifier.

// START NEGATIVE TESTS
/**
 * Test null classifier.
 */
@Test(expected = IllegalArgumentException.class)
public void testNullClassifier() {
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().text("test").build();
    service.classify(classifyOptions);
}
Also used : ClassifyOptions(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Aggregations

Test (org.junit.Test)13 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)9 ClassifiedImages (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImages)8 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)7 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)6 ClassifyOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyOptions)5 File (java.io.File)5 Classification (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classification)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 JsonObject (com.google.gson.JsonObject)2 ClassificationCollection (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassificationCollection)2 ClassifyCollectionOptions (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyCollectionOptions)2 ClassifyInput (com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.ClassifyInput)2 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)2 Classifier (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Classifier)2 CreateClassifierOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.CreateClassifierOptions)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 AssumptionViolatedException (org.junit.AssumptionViolatedException)2