Skip to content

Commit

Permalink
Allow USFM texts to be generated from the USFM string only - sillsdev…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Jun 26, 2024
1 parent ceb586c commit 2c35224
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/SIL.Machine/Corpora/StringStreamContainer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
27 changes: 26 additions & 1 deletion src/SIL.Machine/Corpora/UsfmFileTextCorpus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SIL.Scripture;

Expand Down Expand Up @@ -38,6 +39,30 @@ public UsfmFileTextCorpus(
}
}

public UsfmFileTextCorpus(
IReadOnlyDictionary<string, string> 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))
Expand Down
29 changes: 29 additions & 0 deletions src/SIL.Machine/Corpora/UsfmStringText.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 2c35224

Please sign in to comment.