Example #1
0
        public void CreateModel()
        {
            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey  = apikey,
                ServiceUrl = url
            };

            LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);

            DetailedResponse <TranslationModel> result;

            using (FileStream fs = File.OpenRead(glossaryPath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.CreateModel(
                        baseModelId: "en-fr",
                        forcedGlossary: ms,
                        name: "dotnetExampleModel"
                        );
                }
            }

            Console.WriteLine(result.Response);
            modelId = result.Result.ModelId;
        }
        public void Model_Success()
        {
            _service =
                new LanguageTranslatorService(_username, _password);
            _service.Endpoint = _endpoint;

            TranslationModel createModelResult;

            using (FileStream fs = File.OpenRead(_glossaryPath))
            {
                createModelResult = _service.CreateModel(_baseModel, _customModelName, forcedGlossary: fs);

                if (createModelResult != null)
                {
                    _customModelID = createModelResult.ModelId;
                }
                else
                {
                    Console.WriteLine("result is null.");
                }
            }

            var result = _service.DeleteModel(_customModelID);

            Assert.IsNotNull(createModelResult);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Status == "OK");
        }
Example #3
0
        public void CreateModel()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            LanguageTranslatorService service = new LanguageTranslatorService(versionDate, config);

            DetailedResponse <TranslationModel> result;

            using (FileStream fs = File.OpenRead(glossaryPath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.CreateModel(
                        baseModelId: "en-fr",
                        forcedGlossary: ms,
                        name: "dotnetExampleModel"
                        );
                }
            }

            Console.WriteLine(result.Response);
            modelId = result.Result.ModelId;
        }
Example #4
0
        public void CreateModel_Success()
        {
            IClient client = this.CreateClient();

            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <string>())
            .Returns(request);

            request.WithBodyContent(Arg.Any <HttpContent>())
            .Returns(request);

            request.As <TranslationModel>()
            .Returns(Task.FromResult(new TranslationModel()
            {
                ModelId = "new_id"
            }));

            LanguageTranslatorService service =
                new LanguageTranslatorService(client);

            var customModel = service.CreateModel("model_unit_test", "base_id", Substitute.For <FileStream>("any_file", FileMode.Create));

            Assert.IsNotNull(customModel);
            client.Received().PostAsync(Arg.Any <string>());
            Assert.IsFalse(string.IsNullOrEmpty(customModel.ModelId));
        }
Example #5
0
        public void CreateModel_Success()
        {
            IClient client = this.CreateClient();

            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <string>())
            .Returns(request);

            request.WithBodyContent(Arg.Any <HttpContent>())
            .Returns(request);

            request.As <TranslationModel>()
            .Returns(Task.FromResult(new DetailedResponse <TranslationModel>()
            {
                Result = new TranslationModel()
                {
                    ModelId = "new_id"
                }
            }));

            LanguageTranslatorService service =
                new LanguageTranslatorService(client);

            service.VersionDate = "versionDate";

            var customModel = service.CreateModel("model_unit_test");

            Assert.IsNotNull(customModel);
            client.Received().PostAsync(Arg.Any <string>());
            Assert.IsFalse(string.IsNullOrEmpty(customModel.Result.ModelId));
        }
Example #6
0
        public void CreateModel_Catch_Exception()
        {
            IClient client = this.CreateClient();

            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <string>())
            .Returns(request);

            request.WithBodyContent(Arg.Any <HttpContent>())
            .Returns(x =>
            {
                throw new AggregateException(new ServiceResponseException(Substitute.For <IResponse>(),
                                                                          Substitute.For <HttpResponseMessage>(HttpStatusCode.BadRequest),
                                                                          string.Empty));
            });

            LanguageTranslatorService service =
                new LanguageTranslatorService(client);

            service.VersionDate = "versionDate";

            var customModel = service.CreateModel("model_unit_test");
        }
Example #7
0
        public void CreateModel_Success()
        {
            languageTranslator =
                new LanguageTranslatorService(_userName, _password);
            languageTranslator.Endpoint = _endpoint;

            TranslationModel result;

            using (FileStream fs = File.OpenRead(_glossaryPath))
            {
                result = languageTranslator.CreateModel(_baseModel, _customModelName, forcedGlossary: fs);

                if (result != null)
                {
                    _customModelID = result.ModelId;
                }
                else
                {
                    Console.WriteLine("result is null.");
                }
            }

            Assert.IsNotNull(result);
            Assert.IsFalse(string.IsNullOrEmpty(result.ModelId));
        }
        public IEnumerator TestCreateModel()
        {
            Log.Debug("LanguageTranslatorServiceV3IntegrationTests", "Attempting to CreateModel...");
            TranslationModel createModelResponse = null;

            using (FileStream fs = File.OpenRead(forcedGlossaryFilepath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    service.CreateModel(
                        callback: (DetailedResponse <TranslationModel> response, IBMError error) =>
                    {
                        Log.Debug("LanguageTranslatorServiceV3IntegrationTests", "CreateModel result: {0}", response.Response);
                        createModelResponse = response.Result;
                        customModelId       = createModelResponse.ModelId;
                        Assert.IsNotNull(createModelResponse);
                        Assert.IsNotNull(customModelId);
                        Assert.IsTrue(createModelResponse.Source == "en");
                        Assert.IsTrue(createModelResponse.Target == "fr");
                        Assert.IsTrue(createModelResponse.Name == customModelName);
                        Assert.IsNull(error);
                    },
                        baseModelId: englishToFrenchModel,
                        forcedGlossary: ms,
                        name: customModelName
                        );

                    while (createModelResponse == null)
                    {
                        yield return(null);
                    }
                }
            }
        }
        public void CreateModel()
        {
            IamAuthenticator authenticator = new IamAuthenticator(
                apikey: "{apikey}");

            LanguageTranslatorService service = new LanguageTranslatorService("2018-05-01", authenticator);

            DetailedResponse <TranslationModel> result;

            using (FileStream fs = File.OpenRead("glossary.tmx"))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.CreateModel(
                        baseModelId: "en-es",
                        forcedGlossary: ms,
                        name: "custom-en-es"
                        );
                }
            }

            Console.WriteLine(result.Response);
            modelId = result.Result.ModelId;
        }
        private TranslationModel CreateModel(string baseModelId, string name = null, System.IO.FileStream forcedGlossary = null, System.IO.FileStream parallelCorpus = null, Dictionary <string, object> customData = null)
        {
            Console.WriteLine("\nAttempting to CreateModel()");
            var result = service.CreateModel(baseModelId: baseModelId, name: name, forcedGlossary: forcedGlossary, parallelCorpus: parallelCorpus, customData: customData);

            if (result != null)
            {
                Console.WriteLine("CreateModel() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented));
            }
            else
            {
                Console.WriteLine("Failed to CreateModel()");
            }

            return(result);
        }
Example #11
0
        private void CreateModel()
        {
            using (FileStream fs = File.OpenRead(_glossaryPath))
            {
                Console.WriteLine(string.Format("Calling CreateModel({0}, {1}, {2})...", _baseModel, _customModelName, _glossaryPath));

                var result = _languageTranslator.CreateModel(_baseModel, _customModelName, forcedGlossary: fs);

                if (result != null)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
                    _customModelID = result.ModelId;
                }
                else
                {
                    Console.WriteLine("result is null.");
                }
            }
        }
Example #12
0
        public void CreateModel_BaseModelId_Null()
        {
            IClient client = this.CreateClient();

            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <string>())
            .Returns(request);

            request.WithBodyContent(Arg.Any <HttpContent>())
            .Returns(request);

            LanguageTranslatorService service =
                new LanguageTranslatorService(client);

            var customModel = service.CreateModel(null);
        }
        private void CreateModel()
        {
            using (FileStream fs = File.OpenRead(_glossaryPath))
            {
                Console.WriteLine(string.Format("Calling CreateModel({0}, {1}, {2})...", _baseModel, _customModelName, _glossaryPath));
                var result =
                    _languageTranslator.CreateModel(CreateModelOptions.CreateOptions()
                                                    .WithBaseModelId(_baseModel)
                                                    .WithName(_customModelName)
                                                    .SetForcedGlossary(fs));

                if (result != null)
                {
                    Console.WriteLine(string.Format("Model ID: {0}", result.ModelId));
                    _customModelID = result.ModelId;
                }
                else
                {
                    Console.WriteLine("Failed to create custom model.");
                }
            }
        }
        public void CreateModel_Success()
        {
            IClient  client  = Substitute.For <IClient>();
            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            LanguageTranslatorService service = new LanguageTranslatorService(client);
            var versionDate = "versionDate";

            service.VersionDate = versionDate;

            var baseModelId    = "baseModelId";
            var forcedGlossary = new MemoryStream();
            var parallelCorpus = new MemoryStream();
            var name           = "name";

            var result = service.CreateModel(baseModelId: baseModelId, forcedGlossary: forcedGlossary, parallelCorpus: parallelCorpus, name: name);

            request.Received().WithArgument("version", versionDate);
        }
        public void Model_Success()
        {
            DetailedResponse <TranslationModel> createModelResult;

            using (FileStream fs = File.OpenRead(glossaryPath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    service.WithHeader("X-Watson-Test", "1");
                    createModelResult = service.CreateModel(
                        baseModelId: baseModel,
                        forcedGlossary: ms,
                        name: customModelName
                        );

                    if (createModelResult != null)
                    {
                        customModelID = createModelResult.Result.ModelId;
                    }
                    else
                    {
                        Console.WriteLine("result is null.");
                    }
                }
            }

            service.WithHeader("X-Watson-Test", "1");
            var result = service.DeleteModel(
                modelId: customModelID
                );

            Assert.IsNotNull(createModelResult);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Result.Status == "OK");
        }
Example #16
0
        public void CreateModel_Catch_Exception()
        {
            IClient client = this.CreateClient();

            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <string>())
            .Returns(request);

            request.WithBodyContent(Arg.Any <HttpContent>())
            .Returns(x => { throw new AggregateException(new Exception()); });

            LanguageTranslatorService service =
                new LanguageTranslatorService(client);

            var customModel =
                service.CreateModel(CreateModelOptions.CreateOptions()
                                    .WithName("base_id")
                                    .WithBaseModelId("model_unit_test")
                                    .SetForcedGlossary(Substitute.For <FileStream>("any_file", FileMode.Create)));
        }
Example #17
0
        public void CreateModel_File_Null()
        {
            IClient client = this.CreateClient();

            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <string>())
            .Returns(request);

            request.WithBodyContent(Arg.Any <HttpContent>())
            .Returns(request);

            LanguageTranslatorService service =
                new LanguageTranslatorService(client);

            var customModel =
                service.CreateModel(CreateModelOptions.CreateOptions()
                                    .WithName("base_id")
                                    .WithBaseModelId("model_unit_test")
                                    .SetForcedGlossary(null));
        }
Example #18
0
        public void CreateModel_ModelName_WithSpaces()
        {
            IClient client = this.CreateClient();

            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <string>())
            .Returns(request);

            request.WithBodyContent(Arg.Any <HttpContent>())
            .Returns(request);

            LanguageTranslatorService service =
                new LanguageTranslatorService(client);

            var customModel =
                service.CreateModel(CreateModelOptions.CreateOptions()
                                    .WithName("model name")
                                    .WithBaseModelId("base_id")
                                    .SetForcedGlossary(Substitute.For <FileStream>("any_file_model_name_with_spaces", FileMode.Create)));
        }