Exemple #1
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 <CustomModels>()
            .Returns(Task.FromResult(new CustomModels()
            {
                ModelId = "new_id"
            }));

            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)));

            Assert.IsNotNull(customModel);
            client.Received().PostAsync(Arg.Any <string>());
            Assert.IsFalse(string.IsNullOrEmpty(customModel.ModelId));
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ModelOptions" /> class.
 /// </summary>
 /// <param name="variables">The shared variables dictionary.</param>
 /// <param name="prefix">The variable key prefix.</param>
 public ModelOptions(VariableDictionary variables, string prefix)
     : base(variables, AppendPrefix(prefix, "Model"))
 {
     Shared    = new SharedModelOptions(Variables, Prefix);
     Read      = new ReadModelOptions(Variables, Prefix);
     Create    = new CreateModelOptions(Variables, Prefix);
     Update    = new UpdateModelOptions(Variables, Prefix);
     Mapper    = new MapperClassOptions(Variables, Prefix);
     Validator = new ValidatorClassOptions(Variables, Prefix);
 }
        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.");
                }
            }
        }
Exemple #4
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)));
        }
Exemple #5
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));
        }
Exemple #6
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)));
        }
Exemple #7
0
        public CustomModels CreateModel(CreateModelOptions _options)
        {
            CustomModels result = null;

            if (string.IsNullOrEmpty(_options.BaseModelId))
            {
                throw new ArgumentNullException($"Argument is not valid: {nameof(_options.BaseModelId)}");
            }

            if (_options.Name.Contains(" "))
            {
                throw new ArgumentException($"Argument is not valid (No spaces): {nameof(_options.Name)}");
            }

            if (_options.ForcedGlossary == null && _options.ParallelCorpus == null && _options.MonolingualCorpus == null)
            {
                throw new ArgumentNullException($"Glossary or Corpus file is not valid");
            }

            double fileSize = _options.ForcedGlossary != null ? (_options.ForcedGlossary.Length) / Math.Pow(1024, 2) : 0;

            fileSize += _options.ParallelCorpus != null ? (_options.ParallelCorpus.Length) / Math.Pow(1024, 2) : 0;
            fileSize += _options.MonolingualCorpus != null ? (_options.MonolingualCorpus.Length) / Math.Pow(1024, 2) : 0;

            if (fileSize > 250)
            {
                throw new Exception("The cumulative file size of all uploaded glossary and corpus files is limited to 250 MB.");
            }

            try
            {
                var formData = new MultipartFormDataContent();

                if (_options.ForcedGlossary != null)
                {
                    var forcedGlossaryContent = new ByteArrayContent((_options.ForcedGlossary as Stream).ReadAllBytes());
                    forcedGlossaryContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
                    formData.Add(forcedGlossaryContent, "forced_glossary", _options.ForcedGlossary.Name);
                }

                if (_options.ParallelCorpus != null)
                {
                    var parallelCorpusContent = new ByteArrayContent((_options.ParallelCorpus as Stream).ReadAllBytes());
                    parallelCorpusContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
                    formData.Add(parallelCorpusContent, "forced_glossary", _options.ParallelCorpus.Name);
                }

                if (_options.MonolingualCorpus != null)
                {
                    var monolingualCorpusContent = new ByteArrayContent((_options.MonolingualCorpus as Stream).ReadAllBytes());
                    monolingualCorpusContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
                    formData.Add(monolingualCorpusContent, "forced_glossary", _options.MonolingualCorpus.Name);
                }

                result =
                    this.Client.WithAuthentication(this.UserName, this.Password)
                    .PostAsync($"{this.Endpoint}{PATH_MODEL}")
                    .WithArgument("base_model_id", _options.BaseModelId)
                    .WithArgument("name", _options.Name)
                    .WithBodyContent(formData)
                    .As <CustomModels>()
                    .Result;
            }
            catch (AggregateException ae)
            {
                throw ae.Flatten();
            }

            return(result);
        }