Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions 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;
using System.IO;
using System.Text;
using SIL.Scripture;

Expand Down Expand Up @@ -46,12 +47,15 @@ private static string GetId(string fileName, Encoding encoding)
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.StartsWith("\\id "))
if (line.StartsWith("\\id ", StringComparison.InvariantCulture))
{
string id = line.Substring(4);
int index = id.IndexOf(" ");
int index = id.IndexOf(" ", StringComparison.OrdinalIgnoreCase);
// If the id is longer than 3 characters, truncate it to 3 characters.
if ((index == -1 || index > 3) && id.Length >= 3)
index = 3;
if (index != -1)
id = id.Substring(0, index);
id = id.Substring(0, index).ToUpperInvariant();
return id.Trim();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/SIL.Machine/Corpora/UsfmParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ public bool ProcessToken()

// Code is always upper case
string code = token.Data.ToUpperInvariant();
if (code.Length > 3)
code = code.Substring(0, 3);

vref = State.VerseRef;
// Update verse ref. Leave book alone if not empty to prevent parsing errors
Expand Down
5 changes: 5 additions & 0 deletions src/SIL.Machine/Corpora/UsfmTextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public TextRowCollector(UsfmTextBase text)
public override void StartBook(UsfmParserState state, string marker, string code)
{
base.StartBook(state, marker, code);
if (!string.IsNullOrEmpty(state.VerseRef.Book) && state.VerseRef.Book != code)
{
// Ignore \id markers that don't match the book code in the verse ref, if it was set
return;
}
if (!Canon.AllBookIds.Contains(code, StringComparison.InvariantCulture))
{
throw new ArgumentException($"The book {code} is not a valid book id.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
\id LEV - Test
\id Leviticus
\h Leviticus
\mt Leviticus
\c 14
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
\id 1CH - Test
\id 1CH
\h 1 Chronicles
\mt 1 Chronicles
\c 12
Expand Down
93 changes: 93 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/UsfmMemoryTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,99 @@ public void GetRows_IncompleteVerseRange()
});
}

[Test]
public void GetRows_BookCodeDifferentToFilename() =>
Assert.Throws<InvalidOperationException>(() =>
GetRows(
@"\id LUK - Test
\c 1
\v 1 Verse 1 Text
",
includeAllText: true
)
);

[Test]
public void GetRows_BookCodeInvalid() =>
Assert.Throws<InvalidOperationException>(() =>
GetRows(
@"\id ZZZ - Test
\c 1
\v 1 Verse 1 Text
",
includeAllText: true
)
);

[Test]
public void GetRows_BookCodeTruncated() =>
Assert.Throws<InvalidOperationException>(() =>
GetRows(
@"\id MA
\c 1
\v 1 Verse 1 Text
",
includeAllText: true
)
);

[Test]
public void GetRows_BookCodeMultiple()
{
TextRow[] rows = GetRows(
@"\id MAT
\id LUK
\c 1
\v 1 Verse 1 Text
",
includeAllText: true
);

Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1));

Assert.That(
rows[0].Ref,
Is.EqualTo(ScriptureRef.Parse("MAT 1:1")),
string.Join(",", rows.ToList().Select(tr => tr.Ref.ToString()))
);
Assert.That(
rows[0].Text,
Is.EqualTo("Verse 1 Text"),
string.Join(",", rows.ToList().Select(tr => tr.Text))
);
});
}

[Test]
public void GetRows_BookCodeNoSpace()
{
TextRow[] rows = GetRows(
@"\id Matthew
\c 1
\v 1 Verse 1 Text
",
includeAllText: true
);

Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1));

Assert.That(
rows[0].Ref,
Is.EqualTo(ScriptureRef.Parse("MAT 1:1")),
string.Join(",", rows.ToList().Select(tr => tr.Ref.ToString()))
);
Assert.That(
rows[0].Text,
Is.EqualTo("Verse 1 Text"),
string.Join(",", rows.ToList().Select(tr => tr.Text))
);
});
}

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