Search in sources :

Example 1 with CreateModelOptions

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();
        }
    }
}
Also used : TranslationModel(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DeleteModelOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.DeleteModelOptions) CreateModelOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions) File(java.io.File) FileInputStream(java.io.FileInputStream) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 2 with CreateModelOptions

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));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) TranslationModel(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel) MultipartBody(okhttp3.MultipartBody) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) RequestBody(okhttp3.RequestBody)

Example 3 with CreateModelOptions

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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CreateModelOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 4 with CreateModelOptions

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CreateModelOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions) File(java.io.File) FileInputStream(java.io.FileInputStream) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Aggregations

CreateModelOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 TranslationModel (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 DeleteModelOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.DeleteModelOptions)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1