Search in sources :

Example 6 with TranslationModel

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel 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 7 with TranslationModel

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel in project java-sdk by watson-developer-cloud.

the class LanguageTranslatorTest method testListModels.

/**
 * Test Get Models.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testListModels() throws InterruptedException {
    server.enqueue(jsonResponse(models));
    ListModelsOptions options = new ListModelsOptions.Builder().build();
    final List<TranslationModel> modelList = service.listModels(options).execute().getModels();
    final RecordedRequest request = server.takeRequest();
    assertEquals(GET_MODELS_PATH, request.getPath());
    assertEquals(GSON.toJson(models.getModels()), GSON.toJson(modelList));
}
Also used : ListModelsOptions(com.ibm.watson.developer_cloud.language_translator.v2.model.ListModelsOptions) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TranslationModel(com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 8 with TranslationModel

use of com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel in project java-sdk by watson-developer-cloud.

the class LanguageTranslator method getModel.

/**
 * Get model details.
 *
 * Gets information about a translation model, including training status for custom models.
 *
 * @param getModelOptions the {@link GetModelOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link TranslationModel}
 */
public ServiceCall<TranslationModel> getModel(GetModelOptions getModelOptions) {
    Validator.notNull(getModelOptions, "getModelOptions cannot be null");
    String[] pathSegments = { "v2/models" };
    String[] pathParameters = { getModelOptions.modelId() };
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
    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)

Aggregations

TranslationModel (com.ibm.watson.developer_cloud.language_translator.v2.model.TranslationModel)8 Test (org.junit.Test)6 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)4 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)2 DeleteModelOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.DeleteModelOptions)2 GetModelOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.GetModelOptions)2 ListModelsOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.ListModelsOptions)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 CreateModelOptions (com.ibm.watson.developer_cloud.language_translator.v2.model.CreateModelOptions)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1 Ignore (org.junit.Ignore)1