Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
<PackageReference Include="Hangfire.Mongo" Version="1.11.6" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.8" />
<PackageReference Include="SIL.Machine" Version="3.7.10" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
<PackageReference Include="SIL.Machine.Morphology.HermitCrab" Version="3.7.10" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine.Morphology.HermitCrab\SIL.Machine.Morphology.HermitCrab.csproj')" />
<PackageReference Include="SIL.Machine.Translation.Thot" Version="3.7.10" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine.Translation.Thot\SIL.Machine.Translation.Thot.csproj')" />
<PackageReference Include="SIL.Machine" Version="3.7.11" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
<PackageReference Include="SIL.Machine.Morphology.HermitCrab" Version="3.7.11" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine.Morphology.HermitCrab\SIL.Machine.Morphology.HermitCrab.csproj')" />
<PackageReference Include="SIL.Machine.Translation.Thot" Version="3.7.11" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine.Translation.Thot\SIL.Machine.Translation.Thot.csproj')" />
<PackageReference Include="SIL.WritingSystems" Version="14.1.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/Serval/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.65.0" />
<PackageReference Include="Grpc.HealthCheck" Version="2.65.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.65.0" />
<PackageReference Include="SIL.Machine" Version="3.7.10" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
<PackageReference Include="SIL.Machine" Version="3.7.11" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="3.5.0" />
</ItemGroup>

Expand Down
75 changes: 65 additions & 10 deletions src/Serval/src/Serval.Translation/Services/PretranslationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,26 +320,44 @@ private static string DenormalizeQuotationMarks(string usfm, ParallelCorpusAnaly
QuotationMarkDenormalizationFirstPass quotationMarkDenormalizationFirstPass = new(targetQuoteConvention);

UsfmParser.Parse(usfm, quotationMarkDenormalizationFirstPass);
List<QuotationMarkUpdateStrategy> bestChapterStrategies =
List<(int ChapterNumber, QuotationMarkUpdateStrategy Strategy)> bestChapterStrategies =
quotationMarkDenormalizationFirstPass.FindBestChapterStrategies();

QuotationMarkDenormalizationUsfmUpdateBlockHandler quotationMarkDenormalizer =
new(targetQuoteConvention, new QuotationMarkUpdateSettings(chapterStrategies: bestChapterStrategies));
new(
targetQuoteConvention,
new QuotationMarkUpdateSettings(
chapterStrategies: bestChapterStrategies.Select(tuple => tuple.Strategy).ToList()
)
);
int denormalizableChapterCount = bestChapterStrategies.Count(tup =>
tup.Strategy != QuotationMarkUpdateStrategy.Skip
);
List<string> remarks = [];
if (bestChapterStrategies.Any(s => s != QuotationMarkUpdateStrategy.Skip))
string quotationDenormalizationRemark;
if (denormalizableChapterCount == bestChapterStrategies.Count)
{
string quotationDenormalizationRemark =
quotationDenormalizationRemark =
"The quote style in all chapters has been automatically adjusted to match the rest of the project.";
}
else if (denormalizableChapterCount > 0)
{
quotationDenormalizationRemark =
"The quote style in the following chapters has been automatically adjusted to match the rest of the project: "
+ string.Join(
", ",
+ GetChapterRangesString(
bestChapterStrategies
.Select((strategy, index) => (strategy, index))
.Where(tuple => tuple.strategy != QuotationMarkUpdateStrategy.Skip)
.Select(tuple => tuple.index + 1)
.Where(tuple => tuple.Strategy != QuotationMarkUpdateStrategy.Skip)
.Select(tuple => tuple.ChapterNumber)
.ToList()
)
+ ".";
remarks.Add(quotationDenormalizationRemark);
}
else
{
quotationDenormalizationRemark =
"The quote style was not automatically adjusted to match the rest of your project in any chapters.";
}
remarks.Add(quotationDenormalizationRemark);

var updater = new UpdateUsfmParserHandler(updateBlockHandlers: [quotationMarkDenormalizer], remarks: remarks);
UsfmParser.Parse(usfm, updater);
Expand All @@ -348,6 +366,43 @@ private static string DenormalizeQuotationMarks(string usfm, ParallelCorpusAnaly
return usfm;
}

public static string GetChapterRangesString(List<int> chapterNumbers)
{
chapterNumbers = chapterNumbers.Order().ToList();
int start = chapterNumbers[0];
int end = chapterNumbers[0];
List<string> chapterRangeStrings = [];
foreach (int chapterNumber in chapterNumbers[1..])
{
if (chapterNumber == end + 1)
{
end = chapterNumber;
}
else
{
if (start == end)
{
chapterRangeStrings.Add(start.ToString(CultureInfo.InvariantCulture));
}
else
{
chapterRangeStrings.Add($"{start}-{end}");
}
start = chapterNumber;
end = chapterNumber;
}
}
if (start == end)
{
chapterRangeStrings.Add(start.ToString(CultureInfo.InvariantCulture));
}
else
{
chapterRangeStrings.Add($"{start}-{end}");
}
return string.Join(", ", chapterRangeStrings);
}

/// <summary>
/// Generate a natural sounding remark/comment describing marker placement.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ public void GetUsfmAsync_BadPretranslationVerseRef()
});
}

[Test]
[TestCase(new int[] { 1, 2, 3 }, "1-3")]
[TestCase(new int[] { 1, 3, 4 }, "1, 3-4")]
[TestCase(new int[] { 2, 3, 4, 6, 8, 9 }, "2-4, 6, 8-9")]
[TestCase(new int[] { 1, 3, 2 }, "1-3")]
[TestCase(new int[] { 1 }, "1")]
public void GetChapterRanges(int[] chapterNumbers, string expectedRangeString)
{
string actualRangeString = PretranslationService.GetChapterRangesString(chapterNumbers.ToList());
Assert.That(actualRangeString, Is.EqualTo(expectedRangeString));
}

private class TestEnvironment : IDisposable
{
public TestEnvironment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="SIL.WritingSystems" Version="14.1.1" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="SIL.Scripture" Version="12.0.1" />
<PackageReference Include="SIL.Machine" Version="3.7.10" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
<PackageReference Include="SIL.Machine" Version="3.7.11" Condition="!Exists('..\..\..\..\..\machine\src\SIL.Machine\SIL.Machine.csproj')" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading