Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes https://github.com/sillsdev/serval/issues/449 #232

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/SIL.Machine/Corpora/ScriptureRefUsfmParserHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ IReadOnlyList<UsfmAttribute> attributes
CheckConvertVerseParaToNonVerse(state);
}

public override void OptBreak(UsfmParserState state)
{
if (_curTextType.Count == 0)
_curTextType.Push(ScriptureTextType.NonVerse);
}

protected virtual void StartVerseText(UsfmParserState state, IReadOnlyList<ScriptureRef> scriptureRefs) { }

protected virtual void EndVerseText(UsfmParserState state, IReadOnlyList<ScriptureRef> scriptureRefs) { }
Expand Down
12 changes: 8 additions & 4 deletions src/SIL.Machine/Corpora/UsfmTextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,20 @@ public override void EndNote(UsfmParserState state, string marker, bool closed)

public override void OptBreak(UsfmParserState state)
{
base.OptBreak(state);

if (_text._includeMarkers)
{
_rowTexts.Peek().Append("//");
if (_rowTexts.Count > 0)
_rowTexts.Peek().Append("//");
else
_rowTexts.Push(new StringBuilder("//"));
}
else if (CurrentTextType != ScriptureTextType.Verse || state.IsVerseText)
else if (CurrentTextType != ScriptureTextType.Verse || state.IsVerseText) //When is this not true?
{
if (_rowTexts.Count == 0)
return;
_rowTexts.Peek().TrimEnd();
}
base.OptBreak(state);
}

public override void Text(UsfmParserState state, string text)
Expand Down
54 changes: 53 additions & 1 deletion tests/SIL.Machine.Tests/Corpora/UsfmMemoryTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,29 @@ public void GetRows_TriplicateVerse()
});
}

[Test]
public void GetRows_OptBreak()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\c 1
\v 1 First verse in line // More text
\c 2
\v 1
",
includeAllText: true,
includeMarkers: true
);
Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(2), string.Join(",", rows.ToList().Select(tr => tr.Text)));
Assert.That(rows[0].Text, Is.EqualTo(@"First verse in line // More text"));
});
}

[Test]
public void GetRows_VersePara_BeginningNonVerseSegment()
{
// a verse paragraph that begins with a non-verse segment followed by a verse segment
TextRow[] rows = GetRows(
@"\id MAT - Test
\c 1
Expand All @@ -132,6 +151,39 @@ public void GetRows_VersePara_BeginningNonVerseSegment()
Assert.That(rows, Has.Length.EqualTo(4), string.Join(",", rows.ToList().Select(tr => tr.Text)));
}

[Test]
public void GetRows_OptBreakAtBeginning()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\li //
",
includeAllText: true
);
Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1), string.Join(",", rows.ToList().Select(tr => tr.Text)));
Assert.That(rows[0].Text, Is.EqualTo(""));
});
}

[Test]
public void GetRows_OptBreakAtBeginningIncludeMarkers()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\li //
",
includeAllText: true,
includeMarkers: true
);
Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1), string.Join(",", rows.ToList().Select(tr => tr.Text)));
Assert.That(rows[0].Text, Is.EqualTo("//"));
});
}

private static TextRow[] GetRows(string usfm, bool includeMarkers = false, bool includeAllText = false)
{
UsfmMemoryText text =
Expand Down
Loading