diff --git a/src/SIL.Machine/Corpora/StringStreamContainer.cs b/src/SIL.Machine/Corpora/StringStreamContainer.cs new file mode 100644 index 00000000..2a70274a --- /dev/null +++ b/src/SIL.Machine/Corpora/StringStreamContainer.cs @@ -0,0 +1,25 @@ +using System.IO; +using SIL.ObjectModel; + +namespace SIL.Machine.Corpora +{ + public class StringStreamContainer : DisposableBase, IStreamContainer + { + private readonly string _content; + + public StringStreamContainer(string content) + { + _content = content; + } + + public Stream OpenStream() + { + MemoryStream stream = new MemoryStream(); + StreamWriter writer = new StreamWriter(stream); + writer.Write(_content); + writer.Flush(); + stream.Position = 0; + return stream; + } + } +} diff --git a/src/SIL.Machine/Corpora/UsfmFileTextCorpus.cs b/src/SIL.Machine/Corpora/UsfmFileTextCorpus.cs index abb336df..b16bdcf2 100644 --- a/src/SIL.Machine/Corpora/UsfmFileTextCorpus.cs +++ b/src/SIL.Machine/Corpora/UsfmFileTextCorpus.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Text; using SIL.Scripture; @@ -38,6 +39,30 @@ public UsfmFileTextCorpus( } } + public UsfmFileTextCorpus( + IReadOnlyDictionary idToUsfmContent, + UsfmStylesheet stylesheet, + ScrVers versification, + bool includeMarkers = false, + bool includeAllText = false + ) + { + foreach (var pair in idToUsfmContent) + { + AddText( + new UsfmStringText( + stylesheet, + Encoding.UTF8, + pair.Key, + pair.Value, + Versification, + includeMarkers, + includeAllText + ) + ); + } + } + private static string GetId(string fileName, Encoding encoding) { using (var reader = new StreamReader(fileName, encoding)) diff --git a/src/SIL.Machine/Corpora/UsfmStringText.cs b/src/SIL.Machine/Corpora/UsfmStringText.cs new file mode 100644 index 00000000..bee898d4 --- /dev/null +++ b/src/SIL.Machine/Corpora/UsfmStringText.cs @@ -0,0 +1,29 @@ +using System.Text; +using SIL.Scripture; + +namespace SIL.Machine.Corpora +{ + public class UsfmStringText : UsfmTextBase + { + private readonly string _usfm; + + public UsfmStringText( + UsfmStylesheet stylesheet, + Encoding encoding, + string id, + string usfm, + ScrVers versification = null, + bool includeMarkers = false, + bool includeAllText = false + ) + : base(id, stylesheet, encoding, versification, includeMarkers, includeAllText) + { + _usfm = usfm; + } + + protected override IStreamContainer CreateStreamContainer() + { + return new StringStreamContainer(_usfm); + } + } +}