Skip to content

Commit

Permalink
fix missing last verse (#212)
Browse files Browse the repository at this point in the history
* #408 - fix missing last verse

* Fix issue without change semantics of EndBook

---------

Co-authored-by: Damien Daspit <[email protected]>
  • Loading branch information
johnml1135 and ddaspit authored Jun 19, 2024
1 parent 8c39e3f commit 4e88953
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/SIL.Machine/Corpora/UsfmTextUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public UsfmTextUpdater(

public IReadOnlyList<UsfmToken> Tokens => _tokens;

public override void EndUsfm(UsfmParserState state)
{
CollectTokens(state);
base.EndUsfm(state);
}

public override void StartBook(UsfmParserState state, string marker, string code)
{
CollectTokens(state);
Expand Down Expand Up @@ -314,7 +320,7 @@ private IReadOnlyList<string> AdvanceRows(IReadOnlyList<ScriptureRef> segScrRefs
{
compare = rowScrRef.CompareTo(segScrRefs[sourceIndex], compareSegments: false);
if (compare > 0)
// source is ahead of row, increment source
// row is ahead of source, increment source
sourceIndex++;
else
break;
Expand All @@ -330,7 +336,7 @@ private IReadOnlyList<string> AdvanceRows(IReadOnlyList<ScriptureRef> segScrRefs
}
if (compare <= 0)
{
// row is ahead of source, increment row
// source is ahead row, increment row
_rowIndex++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@
\b
\q1 3.1 part 3
\q1 3.1 part 4
\c 4
\v 1
10 changes: 5 additions & 5 deletions tests/SIL.Machine.Tests/Corpora/UsfmFileTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void GetRows_NonEmptyText()

IText text = corpus["MAT"];
TextRow[] rows = text.GetRows().ToArray();
Assert.That(rows, Has.Length.EqualTo(23));
Assert.That(rows, Has.Length.EqualTo(24));

Assert.That(rows[0].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:1", corpus.Versification)));
Assert.That(rows[0].Text, Is.EqualTo("Chapter one, verse one."));
Expand Down Expand Up @@ -73,7 +73,7 @@ public void GetRows_NonEmptyText_AllText()

IText text = corpus["MAT"];
TextRow[] rows = text.GetRows().ToArray();
Assert.That(rows, Has.Length.EqualTo(49));
Assert.That(rows, Has.Length.EqualTo(50));

Assert.That(rows[0].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:0/1:h", corpus.Versification)));
Assert.That(rows[0].Text, Is.EqualTo("Matthew"));
Expand Down Expand Up @@ -146,7 +146,7 @@ public void GetRows_SentenceStart()

IText text = corpus["MAT"];
TextRow[] rows = text.GetRows().ToArray();
Assert.That(rows, Has.Length.EqualTo(23));
Assert.That(rows, Has.Length.EqualTo(24));

Assert.That(rows[3].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:4", corpus.Versification)));
Assert.That(rows[3].Text, Is.EqualTo("Chapter one, verse four,"));
Expand Down Expand Up @@ -179,7 +179,7 @@ public void GetRows_IncludeMarkers()

IText text = corpus["MAT"];
TextRow[] rows = text.GetRows().ToArray();
Assert.That(rows, Has.Length.EqualTo(23));
Assert.That(rows, Has.Length.EqualTo(24));

Assert.That(rows[0].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:1", corpus.Versification)));
Assert.That(
Expand Down Expand Up @@ -254,7 +254,7 @@ public void GetRows_IncludeMarkers_AllText()

IText text = corpus["MAT"];
TextRow[] rows = text.GetRows().ToArray();
Assert.That(rows, Has.Length.EqualTo(45));
Assert.That(rows, Has.Length.EqualTo(46));

Assert.That(rows[2].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:0/3:ip", corpus.Versification)));
Assert.That(rows[2].Text, Is.EqualTo("An introduction to Matthew\\fe + \\ft This is an endnote.\\fe*"));
Expand Down
10 changes: 10 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/UsfmTextUpdaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ public void GetUsfm_Verse_DoubleVaVp()
);
}

[Test]
public void GetUsfm_Verse_LastVerse()
{
var rows = new List<(IReadOnlyList<ScriptureRef>, string)> { (ScrRef("MAT 4:1"), "Updating the last verse.") };

string target = UpdateUsfm(rows);
Assert.That(target, Contains.Substring("\\id MAT - Test\r\n"));
Assert.That(target, Contains.Substring("\\v 1 Updating the last verse.\r\n"));
}

[Test]
public void GetUsfm_Verse_PretranslationsBeforeText()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/SIL.Machine.Tests/Corpora/UsfmTokenizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public void Tokenize()
string usfm = ReadUsfm();
var tokenizer = new UsfmTokenizer();
IReadOnlyList<UsfmToken> tokens = tokenizer.Tokenize(usfm);
Assert.That(tokens, Has.Count.EqualTo(234));
Assert.That(tokens, Has.Count.EqualTo(236));

Assert.That(tokens[0].Type, Is.EqualTo(UsfmTokenType.Book));
Assert.That(tokens[0].Marker, Is.EqualTo("id"));
Expand Down

0 comments on commit 4e88953

Please sign in to comment.