use of com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorIT method testCreateAndDeleteModel.
/**
* Test create and delete model.
*/
@Test
public void testCreateAndDeleteModel() throws IOException {
String modelName = "integration-test";
String baseModelId = "en-es";
InputStream glossary = new FileInputStream(new File(RESOURCE + "glossary.tmx"));
CreateModelOptions options = new CreateModelOptions.Builder().name(modelName).baseModelId(baseModelId).forcedGlossary(glossary).forcedGlossaryFilename("test_glossary").build();
TranslationModel model = null;
try {
model = service.createModel(options).execute();
Thread.sleep(3000);
assertNotNull(model);
assertTrue(model.getModelId() != null && model.getModelId().length() > 0);
assertEquals(model.getName(), modelName);
assertEquals(model.getBaseModelId(), baseModelId);
assertEquals(model.isCustomizable(), false);
assertEquals(model.isDefaultModel(), false);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (model != null) {
DeleteModelOptions deleteOptions = new DeleteModelOptions.Builder(model.getModelId()).build();
service.deleteModel(deleteOptions).execute();
}
}
}
use of com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions in project java-sdk by watson-developer-cloud.
the class LanguageTranslator method createModel.
/**
* Create model.
*
* Uploads a TMX glossary file on top of a domain to customize a translation model. Depending on the size of the file,
* training can range from minutes for a glossary to several hours for a large parallel corpus. Glossary files must be
* less than 10 MB. The cumulative file size of all uploaded glossary and corpus files is limited to 250 MB.
*
* @param createModelOptions the {@link CreateModelOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link TranslationModel}
*/
public ServiceCall<TranslationModel> createModel(CreateModelOptions createModelOptions) {
Validator.notNull(createModelOptions, "createModelOptions cannot be null");
Validator.isTrue((createModelOptions.forcedGlossary() != null) || (createModelOptions.parallelCorpus() != null) || (createModelOptions.monolingualCorpus() != null), "At least one of forcedGlossary, parallelCorpus, or monolingualCorpus must be supplied.");
String[] pathSegments = { "v2/models" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
builder.query("base_model_id", createModelOptions.baseModelId());
if (createModelOptions.name() != null) {
builder.query("name", createModelOptions.name());
}
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
if (createModelOptions.forcedGlossary() != null) {
RequestBody forcedGlossaryBody = RequestUtils.inputStreamBody(createModelOptions.forcedGlossary(), "application/octet-stream");
multipartBuilder.addFormDataPart("forced_glossary", createModelOptions.forcedGlossaryFilename(), forcedGlossaryBody);
}
if (createModelOptions.parallelCorpus() != null) {
RequestBody parallelCorpusBody = RequestUtils.inputStreamBody(createModelOptions.parallelCorpus(), "application/octet-stream");
multipartBuilder.addFormDataPart("parallel_corpus", createModelOptions.parallelCorpusFilename(), parallelCorpusBody);
}
if (createModelOptions.monolingualCorpus() != null) {
RequestBody monolingualCorpusBody = RequestUtils.inputStreamBody(createModelOptions.monolingualCorpus(), "text/plain");
multipartBuilder.addFormDataPart("monolingual_corpus", createModelOptions.monolingualCorpusFilename(), monolingualCorpusBody);
}
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TranslationModel.class));
}
use of com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorTest method testCreateModelOptions.
/**
* Test create model options.
*/
@Test
public void testCreateModelOptions() {
String myParallelCorpus = "{\"field\":\"value\"}";
InputStream parallelCorpusStream = new ByteArrayInputStream(myParallelCorpus.getBytes());
String myMonolingualCorpus = "{\"field\":\"value\"}";
InputStream monolingualCorpusStream = new ByteArrayInputStream(myMonolingualCorpus.getBytes());
CreateModelOptions options1 = new CreateModelOptions.Builder(modelId).parallelCorpus(parallelCorpusStream).monolingualCorpus(monolingualCorpusStream).build();
CreateModelOptions.Builder builder = options1.newBuilder();
CreateModelOptions options2 = builder.name("baz").build();
assertEquals(options2.baseModelId(), modelId);
assertEquals(options2.parallelCorpus(), parallelCorpusStream);
assertEquals(options2.monolingualCorpus(), monolingualCorpusStream);
assertEquals(options2.name(), "baz");
}
use of com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorTest method testcreateModelWithBaseModelNull.
/**
* Test create model with base model null.
*/
@Test(expected = IllegalArgumentException.class)
public void testcreateModelWithBaseModelNull() throws IOException {
InputStream glossary = new FileInputStream(new File(RESOURCE + "glossary.tmx"));
final CreateModelOptions options = new CreateModelOptions.Builder().forcedGlossary(glossary).build();
service.createModel(options).execute();
}
Aggregations