Example #1
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);
        }
        private void NotifyIfAnyNonOptionalAttributeIsMissing(
            Document document,
            Element elementPrototype,
            Element newElement)
        {
            const string MissingNonOptionalAttributeErrorMessage =
                "Value missing for: attribute {0}, word id {1}, sentence id: {2}, document id: {3}";

            foreach (var wordPrototypeAttribute in elementPrototype.Attributes)
            {
                if (!wordPrototypeAttribute.IsOptional)
                {
                    var wordAttribute =
                        newElement.Attributes.SingleOrDefault(atr => atr.Name.Equals(wordPrototypeAttribute.Name));
                    if ((wordAttribute == null) || string.IsNullOrEmpty(wordAttribute.Value))
                    {
                        if ((document == null) || !document.Sentences.Any())
                        {
                            continue;
                        }

                        var lastSentence = document.Sentences.Last();

                        var newWordId = newElement.GetAttributeByName("id");
                        var sentenceId = lastSentence.GetAttributeByName("id");
                        var documentId = document.GetAttributeByName("id");

                        EventAggregator.GetEvent<ValidationExceptionEvent>()
                            .Publish(
                                string.Format(
                                    MissingNonOptionalAttributeErrorMessage,
                                    wordPrototypeAttribute.Name,
                                    newWordId,
                                    sentenceId,
                                    documentId));
                    }
                }
            }
        }