private async Task LoadWordsForSentence( SentenceWrapper selectedSentence, string documentFilePath, string configFilePath) { var extension = Path.GetExtension(documentFilePath); if (extension != null) { var lowercaseExtension = extension.Substring(1).ToLowerInvariant(); DocumentMapperClient documentMapper = null; if (lowercaseExtension.Equals(ConfigurationStaticData.XmlFormat)) { documentMapper = new DocumentMapperClient( new LightDocumentMapperWithReader { AppConfigMapper = appConfigMapper, EventAggregator = eventAggregator }); } else if (lowercaseExtension.Equals(ConfigurationStaticData.ConllxFormat) || lowercaseExtension.Equals(ConfigurationStaticData.ConllFormat)) { documentMapper = new DocumentMapperClient( new LightConllxDocumentMapper { AppConfigMapper = appConfigMapper, EventAggregator = eventAggregator }); } if (documentMapper == null) { return; } var sentenceLoaded = await documentMapper.LoadSentence(selectedSentence.Id.Value, documentFilePath, configFilePath); if (sentenceLoaded == null) { return; } var selectedSentenceIndex = SelectedDocument.Sentences.IndexOf(SelectedSentence); SelectedDocument.Sentences[selectedSentenceIndex] = new SentenceWrapper(sentenceLoaded); SelectedSentence = SelectedDocument.Sentences[selectedSentenceIndex]; } SelectedDocument.AcceptChanges(); }
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); }