Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Serval.Machine.Shared.Services;
using Trace = System.Diagnostics.Trace;

namespace Serval.Machine.Shared.Services;

public abstract class PreprocessBuildJob<TEngine>(
IPlatformService platformService,
Expand Down Expand Up @@ -139,21 +141,34 @@ IReadOnlyList<ParallelCorpus> corpora
{
List<string> warnings = [];

foreach (ParallelCorpus parallelCorpus in corpora)
// Listen to the trace messages from libpalaso, and record them as warnings
var listener = new WarningsTraceListener(warnings, "USFM Parsing error: ");
try
{
IReadOnlyList<(string MonolingualCorpusId, IReadOnlyList<UsfmVersificationError> errors)> errorsPerCorpus =
ParallelCorpusPreprocessingService.AnalyzeUsfmVersification(parallelCorpus);

foreach ((string monolingualCorpusId, IReadOnlyList<UsfmVersificationError> errors) in errorsPerCorpus)
Trace.Listeners.Add(listener);
foreach (ParallelCorpus parallelCorpus in corpora)
{
foreach (UsfmVersificationError error in errors)
IReadOnlyList<(
string MonolingualCorpusId,
IReadOnlyList<UsfmVersificationError> errors
)> errorsPerCorpus = ParallelCorpusPreprocessingService.AnalyzeUsfmVersification(parallelCorpus);

foreach ((string monolingualCorpusId, IReadOnlyList<UsfmVersificationError> errors) in errorsPerCorpus)
{
warnings.Add(
$"USFM versification error in project {error.ProjectName}, expected verse “{error.ExpectedVerseRef}”, actual verse “{error.ActualVerseRef}”, mismatch type {error.Type} (parallel corpus {parallelCorpus.Id}, monolingual corpus {monolingualCorpusId})"
);
foreach (UsfmVersificationError error in errors)
{
warnings.Add(
$"USFM versification error in project {error.ProjectName}, expected verse “{error.ExpectedVerseRef}”, actual verse “{error.ActualVerseRef}”, mismatch type {error.Type} (parallel corpus {parallelCorpus.Id}, monolingual corpus {monolingualCorpusId})"
);
}
}
}
}
finally
{
Trace.Listeners.Remove(listener);
}

return warnings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Serval.Machine.Shared.Services;

public class WarningsTraceListener(List<string> outputList, string prefix = "") : TraceListener
{
public override void Write(string? message) { }

public override void WriteLine(string? message)
{
if (!string.IsNullOrWhiteSpace(message))
outputList.Add(prefix + message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Serval.Machine.Shared.Services;

[TestFixture]
public class WarningsTraceListenerTests
{
[Test]
public void CapturesChapterParsingWarnings()
{
using var testEnvironment = new TestEnvironment();
const string Chapter = "1.";

var verseRef = new VerseRef { Chapter = Chapter };
Assert.Multiple(() =>
{
Assert.That(verseRef.Chapter, Is.Empty);
Assert.That(testEnvironment.Warnings, Has.Count.EqualTo(1));
Assert.That(
testEnvironment.Warnings[0],
Is.EqualTo(testEnvironment.Prefix + "Just failed to parse a chapter number: " + Chapter)
);
});
}

[Test]
public void CapturesVerseParsingWarnings()
{
using var testEnvironment = new TestEnvironment();
const string Verse = "v1";

var verseRef = new VerseRef { Verse = Verse };
Assert.Multiple(() =>
{
Assert.That(verseRef.Chapter, Is.EqualTo("0"));
Assert.That(testEnvironment.Warnings, Has.Count.EqualTo(1));
Assert.That(
testEnvironment.Warnings[0],
Is.EqualTo(testEnvironment.Prefix + "Just failed to parse a verse number: " + Verse)
);
});
}

public class TestEnvironment : DisposableBase
{
private readonly WarningsTraceListener _listener;

public TestEnvironment()
{
_listener = new WarningsTraceListener(Warnings, Prefix);
Trace.Listeners.Add(_listener);
}

public string Prefix { get; } = "USFM Parsing error: ";
public List<string> Warnings { get; } = [];

protected override void DisposeManagedResources()
{
Trace.Listeners.Remove(_listener);
_listener.Dispose();
}
}
}
1 change: 1 addition & 0 deletions src/Machine/test/Serval.Machine.Shared.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using System.Collections;
global using System.Diagnostics;
global using System.IO.Compression;
global using System.Text.Json;
global using System.Text.Json.Nodes;
Expand Down
Loading