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

Update to Machine 3.2.0 #402

Merged
merged 1 commit into from
Jun 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/Serval.Shared/Serval.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="Grpc.Core.Api" Version="2.52.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.52.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.52.0" />
<PackageReference Include="SIL.Machine" Version="3.1.3" Condition="!Exists('..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
<PackageReference Include="SIL.Machine" Version="3.2.0" Condition="!Exists('..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 11 additions & 15 deletions src/Serval.Translation/Services/PretranslationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,27 @@ public async Task<string> GetUsfmAsync(
targetFile.Filename
);

IReadOnlyList<(IReadOnlyList<ScriptureRef>, string)> pretranslations = (
IEnumerable<(IReadOnlyList<ScriptureRef> Refs, string Translation)> pretranslations = (
await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
)
.Select(p =>
(
(IReadOnlyList<ScriptureRef>)
p.Refs.Select(r => ScriptureRef.Parse(r, targetSettings.Versification)).ToList(),
Refs: (IReadOnlyList<ScriptureRef>)
p.Refs.Select(r => ScriptureRef.Parse(r, targetSettings.Versification)).ToArray(),
p.Translation
)
)
.OrderBy(p => p.Item1[0])
.ToList();
.OrderBy(p => p.Refs[0]);

// Update the target book if it exists
string? usfm = await _scriptureDataFileService.ReadParatextProjectBookAsync(targetFile.Filename, textId);
if (usfm is not null)
{
// the pretranslations are generated from the source book and inserted into the target book
// use relaxed references since the USFM structure may not be the same
pretranslations = pretranslations.Select(p =>
((IReadOnlyList<ScriptureRef>)p.Refs.Select(r => r.ToRelaxed()).ToArray(), p.Translation)
);
switch (textOrigin)
{
case PretranslationUsfmTextOrigin.PreferExisting:
Expand All @@ -81,7 +85,6 @@ await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
pretranslations,
fullName: targetSettings.FullName,
stripAllText: false,
strictComparison: false,
preferExistingText: true
);
case PretranslationUsfmTextOrigin.PreferPretranslated:
Expand All @@ -91,7 +94,6 @@ await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
pretranslations,
fullName: targetSettings.FullName,
stripAllText: false,
strictComparison: false,
preferExistingText: false
);
case PretranslationUsfmTextOrigin.OnlyExisting:
Expand All @@ -100,7 +102,6 @@ await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
usfm,
pretranslations: [], // don't put any pretranslations, we only want the existing text.
fullName: targetSettings.FullName,
strictComparison: false,
stripAllText: false,
preferExistingText: false
);
Expand All @@ -110,7 +111,6 @@ await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
usfm,
pretranslations,
fullName: targetSettings.FullName,
strictComparison: false,
stripAllText: true,
preferExistingText: false
);
Expand All @@ -132,7 +132,6 @@ await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
pretranslations,
fullName: targetSettings.FullName,
stripAllText: true,
strictComparison: true,
preferExistingText: true
);
case PretranslationUsfmTextOrigin.OnlyExisting:
Expand All @@ -142,7 +141,6 @@ await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
pretranslations: [], // don't pass the pretranslations, we only want the existing text.
fullName: targetSettings.FullName,
stripAllText: true,
strictComparison: true,
preferExistingText: true
);
}
Expand All @@ -154,18 +152,16 @@ await GetAllAsync(engineId, modelRevision, corpusId, textId, cancellationToken)
private static string UpdateUsfm(
ParatextProjectSettings settings,
string usfm,
IReadOnlyList<(IReadOnlyList<ScriptureRef>, string)> pretranslations,
IEnumerable<(IReadOnlyList<ScriptureRef>, string)> pretranslations,
string? fullName = null,
bool stripAllText = false,
bool strictComparison = false,
bool preferExistingText = true
)
{
var updater = new UsfmTextUpdater(
pretranslations,
pretranslations.ToArray(),
fullName is null ? null : $"- {fullName}",
stripAllText,
strictComparison: strictComparison,
preferExistingText: preferExistingText
);
UsfmParser.Parse(usfm, updater, settings.Stylesheet, settings.Versification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task GetUsfmAsync_SourceBook(PretranslationUsfmTextOrigin textOrigi
{
TestEnvironment env = new();
string usfm = await env.Service.GetUsfmAsync("engine1", 1, "corpus1", "MAT", textOrigin: textOrigin);
Assert.That(usfm.Replace("\r\n", "\n"), Is.EqualTo(TestEnvironment.GetUsfm(returnUsfmType, id: "MAT - TRG")));
Assert.That(usfm.Replace("\r\n", "\n"), Is.EqualTo(TestEnvironment.GetUsfm(returnUsfmType)));
}

[Test]
Expand All @@ -25,7 +25,7 @@ public async Task GetUsfmAsync_TargetBook(PretranslationUsfmTextOrigin textOrigi
TestEnvironment env = new();
env.AddMatthewToTarget();
string usfm = await env.Service.GetUsfmAsync("engine1", 1, "corpus1", "MAT", textOrigin: textOrigin);
Assert.That(usfm.Replace("\r\n", "\n"), Is.EqualTo(TestEnvironment.GetUsfm(returnUsfmType, id: "MAT - TRG")));
Assert.That(usfm.Replace("\r\n", "\n"), Is.EqualTo(TestEnvironment.GetUsfm(returnUsfmType)));
}

private class TestEnvironment
Expand Down Expand Up @@ -227,7 +227,7 @@ private static string CreateBlank(string id = "MAT - TRG")

public static string GetUsfm(string type, string book = "MAT", string id = "MAT - TRG")
{
return type switch
string usfm = type switch
{
"OnlyPretranslated" => CreatePretranslationsOnly(id),
"PreferPretranslated" => CreatePreferPretranslations(book, id),
Expand All @@ -236,6 +236,7 @@ public static string GetUsfm(string type, string book = "MAT", string id = "MAT
"Blank" => CreateBlank(id),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
return usfm.Replace("\r\n", "\n");
}
}
}
Loading