use of com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testListSynonyms.
/**
* Test listSynonyms.
*/
@Test
public void testListSynonyms() {
String entity = "beverage";
String entityValue = "coffee";
String synonym1 = "java";
String synonym2 = "joe";
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
service.createValue(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
service.createSynonym(createOptions).execute();
service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
ListSynonymsOptions listOptions = new ListSynonymsOptions.Builder().workspaceId(workspaceId).entity(entity).value(entityValue).build();
final SynonymCollection response = service.listSynonyms(listOptions).execute();
assertNotNull(response);
assertNotNull(response.getSynonyms());
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
// nextUrl may be null
if (response.getPagination().getNextUrl() == null) {
assertNull(response.getPagination().getCursor());
} else {
assertNotNull(response.getPagination().getCursor());
}
assertTrue(response.getSynonyms().size() >= 2);
// Should not be paginated, but just to be sure
if (response.getPagination().getNextUrl() == null) {
// assertTrue(response.getSynonyms().stream().filter(r -> r.getSynonym().equals(synonym1)).count() == 1);
boolean found1 = false, found2 = false;
for (Synonym synonymResponse : response.getSynonyms()) {
found1 |= synonymResponse.getSynonymText().equals(synonym1);
found2 |= synonymResponse.getSynonymText().equals(synonym2);
}
assertTrue(found1 && found2);
}
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
service.deleteSynonym(deleteOptions).execute();
service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute();
}
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection in project java-sdk by watson-developer-cloud.
the class Conversation method listSynonyms.
/**
* List entity value synonyms.
*
* List the synonyms for an entity value.
*
* @param listSynonymsOptions the {@link ListSynonymsOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link SynonymCollection}
*/
public ServiceCall<SynonymCollection> listSynonyms(ListSynonymsOptions listSynonymsOptions) {
Validator.notNull(listSynonymsOptions, "listSynonymsOptions cannot be null");
String[] pathSegments = { "v1/workspaces", "entities", "values", "synonyms" };
String[] pathParameters = { listSynonymsOptions.workspaceId(), listSynonymsOptions.entity(), listSynonymsOptions.value() };
RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
if (listSynonymsOptions.pageLimit() != null) {
builder.query("page_limit", String.valueOf(listSynonymsOptions.pageLimit()));
}
if (listSynonymsOptions.includeCount() != null) {
builder.query("include_count", String.valueOf(listSynonymsOptions.includeCount()));
}
if (listSynonymsOptions.sort() != null) {
builder.query("sort", listSynonymsOptions.sort());
}
if (listSynonymsOptions.cursor() != null) {
builder.query("cursor", listSynonymsOptions.cursor());
}
if (listSynonymsOptions.includeAudit() != null) {
builder.query("include_audit", String.valueOf(listSynonymsOptions.includeAudit()));
}
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(SynonymCollection.class));
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection in project java-sdk by watson-developer-cloud.
the class Assistant method listSynonyms.
/**
* List entity value synonyms.
*
* List the synonyms for an entity value. This operation is limited to 2500 requests per 30 minutes. For more
* information, see **Rate limiting**.
*
* @param listSynonymsOptions the {@link ListSynonymsOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link SynonymCollection}
*/
public ServiceCall<SynonymCollection> listSynonyms(ListSynonymsOptions listSynonymsOptions) {
Validator.notNull(listSynonymsOptions, "listSynonymsOptions cannot be null");
String[] pathSegments = { "v1/workspaces", "entities", "values", "synonyms" };
String[] pathParameters = { listSynonymsOptions.workspaceId(), listSynonymsOptions.entity(), listSynonymsOptions.value() };
RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
if (listSynonymsOptions.pageLimit() != null) {
builder.query("page_limit", String.valueOf(listSynonymsOptions.pageLimit()));
}
if (listSynonymsOptions.includeCount() != null) {
builder.query("include_count", String.valueOf(listSynonymsOptions.includeCount()));
}
if (listSynonymsOptions.sort() != null) {
builder.query("sort", listSynonymsOptions.sort());
}
if (listSynonymsOptions.cursor() != null) {
builder.query("cursor", listSynonymsOptions.cursor());
}
if (listSynonymsOptions.includeAudit() != null) {
builder.query("include_audit", String.valueOf(listSynonymsOptions.includeAudit()));
}
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(SynonymCollection.class));
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testListSynonymsWithPaging.
/**
* Test listSynonyms with pagination.
*/
@Test
public void testListSynonymsWithPaging() {
String entity = "beverage";
String entityValue = "coffee";
String synonym1 = "java";
String synonym2 = "joe";
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
service.createValue(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
try {
service.createSynonym(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
ListSynonymsOptions.Builder listOptionsBuilder = new ListSynonymsOptions.Builder(workspaceId, entity, entityValue);
listOptionsBuilder.sort("modified");
listOptionsBuilder.pageLimit(1L);
SynonymCollection response = service.listSynonyms(listOptionsBuilder.build()).execute();
assertNotNull(response);
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
assertNotNull(response.getPagination().getNextUrl());
assertNotNull(response.getPagination().getCursor());
boolean found1 = false, found2 = false;
while (true) {
assertNotNull(response.getSynonyms());
assertTrue(response.getSynonyms().size() == 1);
found1 |= response.getSynonyms().get(0).getSynonymText().equals(synonym1);
found2 |= response.getSynonyms().get(0).getSynonymText().equals(synonym2);
if (response.getPagination().getCursor() == null) {
break;
}
String cursor = response.getPagination().getCursor();
response = service.listSynonyms(listOptionsBuilder.cursor(cursor).build()).execute();
}
assertTrue(found1 && found2);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
service.deleteSynonym(deleteOptions).execute();
service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute();
}
}
use of com.ibm.watson.developer_cloud.conversation.v1.model.SynonymCollection in project java-sdk by watson-developer-cloud.
the class SynonymsIT method testListSynonymsWithPaging.
/**
* Test listSynonyms with pagination.
*/
@Test
public void testListSynonymsWithPaging() {
String entity = "beverage";
String entityValue = "coffee";
String synonym1 = "java";
String synonym2 = "joe";
try {
CreateEntityOptions createOptions = new CreateEntityOptions.Builder(workspaceId, entity).build();
service.createEntity(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
CreateValueOptions createOptions = new CreateValueOptions.Builder(workspaceId, entity, entityValue).build();
service.createValue(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
CreateSynonymOptions createOptions = new CreateSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
try {
service.createSynonym(createOptions).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
service.createSynonym(createOptions.newBuilder().synonym(synonym2).build()).execute();
} catch (Exception ex) {
// Exception is okay if is for Unique Violation
assertTrue(ex.getLocalizedMessage().startsWith("Unique Violation"));
}
try {
ListSynonymsOptions.Builder listOptionsBuilder = new ListSynonymsOptions.Builder(workspaceId, entity, entityValue);
listOptionsBuilder.sort("modified");
listOptionsBuilder.pageLimit(1L);
SynonymCollection response = service.listSynonyms(listOptionsBuilder.build()).execute();
assertNotNull(response);
assertNotNull(response.getPagination());
assertNotNull(response.getPagination().getRefreshUrl());
assertNotNull(response.getPagination().getNextUrl());
assertNotNull(response.getPagination().getCursor());
boolean found1 = false, found2 = false;
while (true) {
assertNotNull(response.getSynonyms());
assertTrue(response.getSynonyms().size() == 1);
found1 |= response.getSynonyms().get(0).getSynonymText().equals(synonym1);
found2 |= response.getSynonyms().get(0).getSynonymText().equals(synonym2);
if (response.getPagination().getCursor() == null) {
break;
}
String cursor = response.getPagination().getCursor();
response = service.listSynonyms(listOptionsBuilder.cursor(cursor).build()).execute();
}
assertTrue(found1 && found2);
} catch (Exception ex) {
fail(ex.getMessage());
} finally {
DeleteSynonymOptions deleteOptions = new DeleteSynonymOptions.Builder(workspaceId, entity, entityValue, synonym1).build();
service.deleteSynonym(deleteOptions).execute();
service.deleteSynonym(deleteOptions.newBuilder().synonym(synonym2).build()).execute();
}
}
Aggregations