Search in sources :

Example 1 with ClassifyElementsOptions

use of com.ibm.watson.compare_comply.v1.model.ClassifyElementsOptions in project java-sdk by watson-developer-cloud.

the class CompareComply method classifyElements.

/**
 * Classify the elements of a document.
 *
 * <p>Analyzes the structural and semantic elements of a document.
 *
 * @param classifyElementsOptions the {@link ClassifyElementsOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link ClassifyReturn}
 */
public ServiceCall<ClassifyReturn> classifyElements(ClassifyElementsOptions classifyElementsOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(classifyElementsOptions, "classifyElementsOptions cannot be null");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/element_classification"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("compare-comply", "v1", "classifyElements");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    if (classifyElementsOptions.model() != null) {
        builder.query("model", String.valueOf(classifyElementsOptions.model()));
    }
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    okhttp3.RequestBody fileBody = RequestUtils.inputStreamBody(classifyElementsOptions.file(), classifyElementsOptions.fileContentType());
    multipartBuilder.addFormDataPart("file", "filename", fileBody);
    builder.body(multipartBuilder.build());
    ResponseConverter<ClassifyReturn> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ClassifyReturn>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) ClassifyReturn(com.ibm.watson.compare_comply.v1.model.ClassifyReturn)

Example 2 with ClassifyElementsOptions

use of com.ibm.watson.compare_comply.v1.model.ClassifyElementsOptions in project java-sdk by watson-developer-cloud.

the class CompareComplyTest method testClassifyElementsWOptions.

@Test
public void testClassifyElementsWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"document\": {\"title\": \"title\", \"html\": \"html\", \"hash\": \"hash\", \"label\": \"label\"}, \"model_id\": \"modelId\", \"model_version\": \"modelVersion\", \"elements\": [{\"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\", \"types\": [{\"label\": {\"nature\": \"nature\", \"party\": \"party\"}, \"provenance_ids\": [\"provenanceIds\"], \"modification\": \"added\"}], \"categories\": [{\"label\": \"Amendments\", \"provenance_ids\": [\"provenanceIds\"], \"modification\": \"added\"}], \"attributes\": [{\"type\": \"Currency\", \"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}]}], \"effective_dates\": [{\"confidence_level\": \"High\", \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"provenance_ids\": [\"provenanceIds\"], \"location\": {\"begin\": 5, \"end\": 3}}], \"contract_amounts\": [{\"confidence_level\": \"High\", \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"interpretation\": {\"value\": \"value\", \"numeric_value\": 12, \"unit\": \"unit\"}, \"provenance_ids\": [\"provenanceIds\"], \"location\": {\"begin\": 5, \"end\": 3}}], \"termination_dates\": [{\"confidence_level\": \"High\", \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"provenance_ids\": [\"provenanceIds\"], \"location\": {\"begin\": 5, \"end\": 3}}], \"contract_types\": [{\"confidence_level\": \"High\", \"text\": \"text\", \"provenance_ids\": [\"provenanceIds\"], \"location\": {\"begin\": 5, \"end\": 3}}], \"contract_terms\": [{\"confidence_level\": \"High\", \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"interpretation\": {\"value\": \"value\", \"numeric_value\": 12, \"unit\": \"unit\"}, \"provenance_ids\": [\"provenanceIds\"], \"location\": {\"begin\": 5, \"end\": 3}}], \"payment_terms\": [{\"confidence_level\": \"High\", \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"interpretation\": {\"value\": \"value\", \"numeric_value\": 12, \"unit\": \"unit\"}, \"provenance_ids\": [\"provenanceIds\"], \"location\": {\"begin\": 5, \"end\": 3}}], \"contract_currencies\": [{\"confidence_level\": \"High\", \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"provenance_ids\": [\"provenanceIds\"], \"location\": {\"begin\": 5, \"end\": 3}}], \"tables\": [{\"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\", \"section_title\": {\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}, \"title\": {\"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\"}, \"table_headers\": [{\"cell_id\": \"cellId\", \"location\": {\"mapKey\": \"anyValue\"}, \"text\": \"text\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14}], \"row_headers\": [{\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14}], \"column_headers\": [{\"cell_id\": \"cellId\", \"location\": {\"mapKey\": \"anyValue\"}, \"text\": \"text\", \"text_normalized\": \"textNormalized\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14}], \"body_cells\": [{\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\", \"row_index_begin\": 13, \"row_index_end\": 11, \"column_index_begin\": 16, \"column_index_end\": 14, \"row_header_ids\": [\"rowHeaderIds\"], \"row_header_texts\": [\"rowHeaderTexts\"], \"row_header_texts_normalized\": [\"rowHeaderTextsNormalized\"], \"column_header_ids\": [\"columnHeaderIds\"], \"column_header_texts\": [\"columnHeaderTexts\"], \"column_header_texts_normalized\": [\"columnHeaderTextsNormalized\"], \"attributes\": [{\"type\": \"Currency\", \"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}]}], \"contexts\": [{\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}], \"key_value_pairs\": [{\"key\": {\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\"}, \"value\": [{\"cell_id\": \"cellId\", \"location\": {\"begin\": 5, \"end\": 3}, \"text\": \"text\"}]}]}], \"document_structure\": {\"section_titles\": [{\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}, \"level\": 5, \"element_locations\": [{\"begin\": 5, \"end\": 3}]}], \"leading_sentences\": [{\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}, \"element_locations\": [{\"begin\": 5, \"end\": 3}]}], \"paragraphs\": [{\"location\": {\"begin\": 5, \"end\": 3}}]}, \"parties\": [{\"party\": \"party\", \"role\": \"role\", \"importance\": \"Primary\", \"addresses\": [{\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}], \"contacts\": [{\"name\": \"name\", \"role\": \"role\"}], \"mentions\": [{\"text\": \"text\", \"location\": {\"begin\": 5, \"end\": 3}}]}]}";
    String classifyElementsPath = "/v1/element_classification";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the ClassifyElementsOptions model
    ClassifyElementsOptions classifyElementsOptionsModel = new ClassifyElementsOptions.Builder().file(TestUtilities.createMockStream("This is a mock file.")).fileContentType("application/pdf").model("contracts").build();
    // Invoke operation with valid options model (positive test)
    Response<ClassifyReturn> response = compareComplyService.classifyElements(classifyElementsOptionsModel).execute();
    assertNotNull(response);
    ClassifyReturn 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);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    assertEquals(query.get("model"), "contracts");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, classifyElementsPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ClassifyElementsOptions(com.ibm.watson.compare_comply.v1.model.ClassifyElementsOptions) ClassifyReturn(com.ibm.watson.compare_comply.v1.model.ClassifyReturn) Test(org.testng.annotations.Test)

Example 3 with ClassifyElementsOptions

use of com.ibm.watson.compare_comply.v1.model.ClassifyElementsOptions in project java-sdk by watson-developer-cloud.

the class CompareComplyServiceIT method testClassifyElements.

@Test
public void testClassifyElements() throws FileNotFoundException {
    ClassifyElementsOptions classifyElementsOptions = new ClassifyElementsOptions.Builder().file(CONTRACT_A).fileContentType(HttpMediaType.APPLICATION_PDF).build();
    ClassifyReturn response = service.classifyElements(classifyElementsOptions).execute().getResult();
    assertNotNull(response);
// System.out.println(response);
}
Also used : ClassifyElementsOptions(com.ibm.watson.compare_comply.v1.model.ClassifyElementsOptions) ClassifyReturn(com.ibm.watson.compare_comply.v1.model.ClassifyReturn) Test(org.junit.Test)

Aggregations

ClassifyReturn (com.ibm.watson.compare_comply.v1.model.ClassifyReturn)3 ClassifyElementsOptions (com.ibm.watson.compare_comply.v1.model.ClassifyElementsOptions)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 MultipartBody (okhttp3.MultipartBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.junit.Test)1 Test (org.testng.annotations.Test)1