Search in sources :

Example 16 with TrainingQuery

use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method addTrainingDataIsSuccessful.

@Test
public void addTrainingDataIsSuccessful() {
    AddTrainingDataOptions.Builder builder = new AddTrainingDataOptions.Builder(environmentId, collectionId);
    String naturalLanguageQuery = "Example query" + UUID.randomUUID().toString();
    builder.naturalLanguageQuery(naturalLanguageQuery);
    TrainingExample example = new TrainingExample();
    String documentId = createTestDocument("test_document", collectionId).getDocumentId();
    example.setDocumentId(documentId);
    int relevance = 0;
    example.setRelevance(relevance);
    builder.addExamples(example);
    TrainingQuery response = discovery.addTrainingData(builder.build()).execute();
    assertFalse(response.getQueryId().isEmpty());
    assertEquals(response.getNaturalLanguageQuery(), naturalLanguageQuery);
    assertTrue(response.getFilter().isEmpty());
    assertEquals(response.getExamples().size(), 1);
    TrainingExample returnedExample = response.getExamples().get(0);
    assertEquals(returnedExample.getDocumentId(), documentId);
    assertTrue(returnedExample.getCrossReference().isEmpty());
    assertEquals(returnedExample.getRelevance(), new Long(relevance));
}
Also used : AddTrainingDataOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddTrainingDataOptions) TrainingExample(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 17 with TrainingQuery

use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceIT method deleteTrainingDataExampleIsSuccessful.

@Test
public void deleteTrainingDataExampleIsSuccessful() {
    TrainingQuery newQuery = createTestQuery(collectionId, "Query" + UUID.randomUUID().toString());
    String queryId = newQuery.getQueryId();
    String documentId = "document_id";
    String crossReference = "cross_reference";
    int relevance = 50;
    CreateTrainingExampleOptions.Builder exampleBuilder = new CreateTrainingExampleOptions.Builder(environmentId, collectionId, queryId);
    exampleBuilder.documentId(documentId);
    exampleBuilder.crossReference(crossReference);
    exampleBuilder.relevance(relevance);
    TrainingExample createdExample = discovery.createTrainingExample(exampleBuilder.build()).execute();
    String exampleId = createdExample.getDocumentId();
    GetTrainingDataOptions.Builder queryBuilder = new GetTrainingDataOptions.Builder(environmentId, collectionId, queryId);
    TrainingQuery queryWithAddedExample = discovery.getTrainingData(queryBuilder.build()).execute();
    int startingCount = queryWithAddedExample.getExamples().size();
    DeleteTrainingExampleOptions.Builder deleteBuilder = new DeleteTrainingExampleOptions.Builder(environmentId, collectionId, queryId, exampleId);
    discovery.deleteTrainingExample(deleteBuilder.build()).execute();
    GetTrainingDataOptions.Builder newQueryBuilder = new GetTrainingDataOptions.Builder(environmentId, collectionId, queryId);
    TrainingQuery queryWithDeletedExample = discovery.getTrainingData(newQueryBuilder.build()).execute();
    assertTrue(startingCount > queryWithDeletedExample.getExamples().size());
}
Also used : CreateTrainingExampleOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateTrainingExampleOptions) GetTrainingDataOptions(com.ibm.watson.developer_cloud.discovery.v1.model.GetTrainingDataOptions) TrainingExample(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample) DeleteTrainingExampleOptions(com.ibm.watson.developer_cloud.discovery.v1.model.DeleteTrainingExampleOptions) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 18 with TrainingQuery

use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.

the class Discovery method addTrainingData.

/**
 * Adds a query to the training data for this collection. The query can contain a filter and natural language query.
 *
 * @param addTrainingDataOptions the {@link AddTrainingDataOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link TrainingQuery}
 */
public ServiceCall<TrainingQuery> addTrainingData(AddTrainingDataOptions addTrainingDataOptions) {
    Validator.notNull(addTrainingDataOptions, "addTrainingDataOptions cannot be null");
    String[] pathSegments = { "v1/environments", "collections", "training_data" };
    String[] pathParameters = { addTrainingDataOptions.environmentId(), addTrainingDataOptions.collectionId() };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    if (addTrainingDataOptions.naturalLanguageQuery() != null) {
        contentJson.addProperty("natural_language_query", addTrainingDataOptions.naturalLanguageQuery());
    }
    if (addTrainingDataOptions.filter() != null) {
        contentJson.addProperty("filter", addTrainingDataOptions.filter());
    }
    if (addTrainingDataOptions.examples() != null) {
        contentJson.add("examples", GsonSingleton.getGson().toJsonTree(addTrainingDataOptions.examples()));
    }
    builder.bodyJson(contentJson);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TrainingQuery.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) TrainingQuery(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery)

Example 19 with TrainingQuery

use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testGetTrainingQueryWOptions.

@Test
public void testGetTrainingQueryWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"query_id\": \"queryId\", \"natural_language_query\": \"naturalLanguageQuery\", \"filter\": \"filter\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"examples\": [{\"document_id\": \"documentId\", \"collection_id\": \"collectionId\", \"relevance\": 9, \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}]}";
    String getTrainingQueryPath = "/v2/projects/testString/training_data/queries/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetTrainingQueryOptions model
    GetTrainingQueryOptions getTrainingQueryOptionsModel = new GetTrainingQueryOptions.Builder().projectId("testString").queryId("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<TrainingQuery> response = discoveryService.getTrainingQuery(getTrainingQueryOptionsModel).execute();
    assertNotNull(response);
    TrainingQuery responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("version"), "testString");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getTrainingQueryPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetTrainingQueryOptions(com.ibm.watson.discovery.v2.model.GetTrainingQueryOptions) TrainingQuery(com.ibm.watson.discovery.v2.model.TrainingQuery) Test(org.testng.annotations.Test)

Example 20 with TrainingQuery

use of com.ibm.watson.discovery.v2.model.TrainingQuery in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testUpdateTrainingQueryWOptions.

@Test
public void testUpdateTrainingQueryWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"query_id\": \"queryId\", \"natural_language_query\": \"naturalLanguageQuery\", \"filter\": \"filter\", \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\", \"examples\": [{\"document_id\": \"documentId\", \"collection_id\": \"collectionId\", \"relevance\": 9, \"created\": \"2019-01-01T12:00:00.000Z\", \"updated\": \"2019-01-01T12:00:00.000Z\"}]}";
    String updateTrainingQueryPath = "/v2/projects/testString/training_data/queries/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the TrainingExample model
    TrainingExample trainingExampleModel = new TrainingExample.Builder().documentId("testString").collectionId("testString").relevance(Long.valueOf("26")).build();
    // Construct an instance of the UpdateTrainingQueryOptions model
    UpdateTrainingQueryOptions updateTrainingQueryOptionsModel = new UpdateTrainingQueryOptions.Builder().projectId("testString").queryId("testString").naturalLanguageQuery("testString").examples(new java.util.ArrayList<TrainingExample>(java.util.Arrays.asList(trainingExampleModel))).filter("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<TrainingQuery> response = discoveryService.updateTrainingQuery(updateTrainingQueryOptionsModel).execute();
    assertNotNull(response);
    TrainingQuery 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");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, updateTrainingQueryPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) UpdateTrainingQueryOptions(com.ibm.watson.discovery.v2.model.UpdateTrainingQueryOptions) TrainingExample(com.ibm.watson.discovery.v2.model.TrainingExample) TrainingQuery(com.ibm.watson.discovery.v2.model.TrainingQuery) Test(org.testng.annotations.Test)

Aggregations

TrainingQuery (com.ibm.watson.developer_cloud.discovery.v1.model.TrainingQuery)12 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 Test (org.junit.Test)9 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)7 TrainingExample (com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample)6 TrainingQuery (com.ibm.watson.discovery.v1.model.TrainingQuery)6 TrainingQuery (com.ibm.watson.discovery.v2.model.TrainingQuery)6 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)5 AddTrainingDataOptions (com.ibm.watson.developer_cloud.discovery.v1.model.AddTrainingDataOptions)5 HashMap (java.util.HashMap)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 Test (org.testng.annotations.Test)5 JsonObject (com.google.gson.JsonObject)4 GetTrainingDataOptions (com.ibm.watson.developer_cloud.discovery.v1.model.GetTrainingDataOptions)4 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 CreateTrainingExampleOptions (com.ibm.watson.developer_cloud.discovery.v1.model.CreateTrainingExampleOptions)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)2 AddTrainingDataOptions (com.ibm.watson.discovery.v1.model.AddTrainingDataOptions)2 GetTrainingDataOptions (com.ibm.watson.discovery.v1.model.GetTrainingDataOptions)2