Exemplo n.º 1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var word = new Domain.Word {
                    Id     = request.Id,
                    Phrase = request.Phrase
                };

                _context.Words.Add(word);

                foreach (var tr in request.Translates)
                {
                    var translation = new Translate {
                        Id          = tr.Id,
                        Locale      = tr.Locale,
                        Translation = tr.Translation,
                        Word        = word
                    };

                    _context.Translates.Add(translation);
                }


                // var translation = new Translate{
                //     Locale= "az",
                //     Translation=request.Translation,
                //     Word=word
                // };

                // _context.Translates.Add(translation);


                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Proble on saving activity");
            }
Exemplo n.º 2
0
        public AddWordViewModel(Word wordPrototype, List<Pair> words)
        {
            if (wordPrototype == null)
            {
                throw new ArgumentNullException("wordPrototype");
            }
            if (words == null)
            {
                throw new ArgumentNullException("words");
            }

            this.wordPrototype = wordPrototype;
            wordPrototypeOriginal = ObjectCopier.Clone(wordPrototype);
            this.words = words;

            SetAllWordAttributesAsEditable(this.wordPrototype);
            SetAllowedValuesSetForWordIdAttribute(this.wordPrototype, this.words);
            SetAllowedValuesSetForHeadWordAttribute(this.wordPrototype, this.words);

            Word = new WordWrapper(this.wordPrototype);

            OkButtonCommand = new DelegateCommand(OkButtonCommandExecute, OkButtonCommandCanExecute);
        }
        public async Task<Sentence> LoadSentence(string sentenceId, string filepath, string configFilepath,
            DataStructure dataStructure = null,
            Definition definitionParam = null)
        {
            var appConfig = await AppConfigMapper.Map(configFilepath);

            if (appConfig == null)
            {
                throw new ArgumentNullException(
                    "configFilepath",
                    string.Format("Could not load configuration file from: {0}", configFilepath));
            }

            var datastructure =
                appConfig.DataStructures.FirstOrDefault(
                    d =>
                        (d.Format == ConfigurationStaticData.ConllxFormat) ||
                        (d.Format == ConfigurationStaticData.ConllFormat));

            if (datastructure == null)
            {
                EventAggregator.GetEvent<StatusNotificationEvent>()
                    .Publish(
                        "Could not load CONLLX file because the structure is not defined in the configuration file.");
                return null;
            }
            definition = definitionParam ?? appConfig.Definitions.FirstOrDefault();

            if (definition == null)
            {
                EventAggregator.GetEvent<StatusNotificationEvent>()
                    .Publish(
                        "Could not load XML file because the tree definitionParam is not defined in the configuration file.");
                return null;
            }


            wordPrototype = datastructure.Elements.OfType<Word>().Single();
            sentencePrototype = datastructure.Elements.OfType<Sentence>().Single();

            var sentence = await Task.FromResult(CreateSentence(filepath, sentenceId));

            return sentence;
        }
        public async Task<Document> Map(string filepath, string configFilepath, DataStructure dataStructure = null,
            Definition definitionParam = null)
        {
            var appConfig = await AppConfigMapper.Map(configFilepath);

            if (appConfig == null)
            {
                throw new ArgumentNullException(
                    "configFilepath",
                    string.Format("Could not load configuration file from: {0}", configFilepath));
            }

            var datastructure =
                appConfig.DataStructures.FirstOrDefault(
                    d =>
                        (d.Format == ConfigurationStaticData.ConllxFormat) ||
                        (d.Format == ConfigurationStaticData.ConllFormat));

            if (datastructure == null)
            {
                EventAggregator.GetEvent<StatusNotificationEvent>()
                    .Publish(
                        "Could not load CONLLX file because the structure is not defined in the configuration file.");
                return null;
            }
            definition = definitionParam ?? appConfig.Definitions.FirstOrDefault();

            if (definition == null)
            {
                EventAggregator.GetEvent<StatusNotificationEvent>()
                    .Publish(
                        "Could not load XML file because the tree definitionParam is not defined in the configuration file.");
                return null;
            }


            wordPrototype = datastructure.Elements.OfType<Word>().Single();
            sentencePrototype = datastructure.Elements.OfType<Sentence>().Single();
            documentPrototype = datastructure.Elements.OfType<Document>().Single();

            var document = await CreateDocument(filepath);

            document.FilePath = filepath;

            var filenameToPathMapping = AppConfig.GetConfigFileNameToFilePathMapping();

            document.Attributes.Add(
                new Attribute
                {
                    AllowedValuesSet = filenameToPathMapping.Values,
                    Value = appConfig.Name,
                    Name = "configuration",
                    DisplayName = "Configuration",
                    Entity = "attribute",
                    IsEditable = true,
                    IsOptional = false
                });

            document.Attributes.Add(
                new Attribute
                {
                    Value = appConfig.Filepath,
                    Name = "configurationFilePath",
                    DisplayName = "Configuration file path",
                    Entity = "attribute",
                    IsEditable = false,
                    IsOptional = false
                });

            return document;
        }
 private void AddWordInternalAttributes(Word word)
 {
     var formAttribute = word.Attributes.SingleOrDefault(a => a.Name.Equals("form"));
     var formValue = formAttribute != null ? formAttribute.Value : string.Empty;
     word.Attributes.Add(
         new Attribute
         {
             Name = "content",
             DisplayName = "Content",
             Value = formValue,
             IsOptional = true,
             IsEditable = false
         });
 }
Exemplo n.º 6
0
 public void PressKey(Word word)
 {
     this.PressKeys(new List<Word> { word });
 }
Exemplo n.º 7
0
        public async Task Save(Document document, string filepathToSaveTo = "", bool overwrite = true)
        {
            var filepath = string.IsNullOrWhiteSpace(filepathToSaveTo) ? document.FilePath : filepathToSaveTo;

            if (string.IsNullOrWhiteSpace(filepath))
            {
                return;
            }

            var fileName = Path.GetFileName(filepath);
            var newFilepath = filepath.Replace(fileName, "New" + fileName);

            var mapper = new AppConfigMapper();

            var configFilePath = document.GetAttributeByName("configurationFilePath");

            if (string.IsNullOrWhiteSpace(configFilePath))
            {
                return;
            }

            var appConfig = mapper.Map(configFilePath).GetAwaiter().GetResult();
            var dataStructure =
                appConfig.DataStructures.FirstOrDefault(
                    d =>
                        d.Format.Equals(ConfigurationStaticData.ConllxFormat)
                        || d.Format.Equals(ConfigurationStaticData.ConllFormat));

            if (dataStructure == null)
            {
                return;
            }

            wordPrototype = dataStructure.Elements.OfType<Word>().Single();

            var documentMapper =
                new DocumentMapperClient(
                    new LightConllxDocumentMapper {AppConfigMapper = mapper, EventAggregator = eventAggregator});

            using (var writer = new StreamWriter(newFilepath))
            {
                foreach (var sentence in document.Sentences)
                {
                    if (!sentence.Words.Any())
                    {
                        var oldSentence =
                            await
                                documentMapper.LoadSentence(sentence.GetAttributeByName("id"), filepath,
                                    configFilePath);

                        WriteSentenceWords(oldSentence, writer);
                    }
                    else
                    {
                        WriteSentenceWords(sentence, writer);
                    }
                }

                writer.Flush();
            }


            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }

            File.Move(newFilepath, filepath);
        }
Exemplo n.º 8
0
        private string GetWordLine(Word word)
        {
            var result = new StringBuilder();

            var internalAttributes = new List<string> {"configuration", "content", "configurationFilePath"};

            var sortedAttributes = wordPrototype.Attributes.ToList();

            sortedAttributes.Sort((left, right) => left.Position.CompareTo(right.Position));

            foreach (var attribute in sortedAttributes)
            {
                if (internalAttributes.Contains(attribute.Name))
                {
                    continue;
                }

                var attributeFromWord = word.Attributes.FirstOrDefault(a => a.Name.Equals(attribute.Name));

                var attributeValue = attributeFromWord != null ? attributeFromWord.Value : string.Empty;

                if ((attributeFromWord != null) && !string.IsNullOrEmpty(attributeFromWord.Value))
                {
                    var splits = attributeFromWord.Value.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

                    if (splits.Length > 1)
                    {
                        attributeValue = string.Join("_", splits);
                    }
                }

                result.AppendFormat("{0}\t", attributeFromWord != null ? attributeValue : "_");
            }

            return result.ToString().TrimEnd('\t');
        }
Exemplo n.º 9
0
 private void SetAllWordAttributesAsEditable(Word word)
 {
     foreach (var attribute in word.Attributes)
     {
         attribute.IsEditable = true;
     }
 }
Exemplo n.º 10
0
 private void SetAllowedValuesSetForWordIdAttribute(Word word, List<Pair> wordsParam)
 {
     var newId = wordsParam.Any() ? wordsParam.Max(w => w.Id) + 1 : 0;
     var idAttribute = word.Attributes.Single(a => a.Name.ToLowerInvariant().Equals("id"));
     idAttribute.IsEditable = false;
     idAttribute.Value = newId.ToString();
 }
Exemplo n.º 11
0
        private void SetAllowedValuesSetForHeadWordAttribute(Word word, List<Pair> wordsParam)
        {
            var localCopy = new List<Pair>(wordsParam) {new Pair {Id = 0, Form = string.Empty}};

            var headWordIdAttribute = word.Attributes.Single(a => a.Name.ToLowerInvariant().Equals("head"));
            localCopy.Sort((l, r) => l.Id == r.Id ? 0 : l.Id < r.Id ? -1 : 1);
            headWordIdAttribute.AllowedValuesSet = localCopy.Select(p => p.Form).ToList();
            headWordIdAttribute.Value = localCopy.Aggregate((l, r) => l.Id > r.Id ? l : r).Form;
        }