Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@
QuotationMarkDenormalizationFirstPass quotationMarkDenormalizationFirstPass = new(targetQuoteConvention);

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

Check failure on line 324 in src/Serval/src/Serval.Translation/Services/PretranslationService.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot implicitly convert type 'System.Collections.Generic.List<SIL.Machine.PunctuationAnalysis.QuotationMarkUpdateStrategy>' to 'System.Collections.Generic.List<(int ChapterNumber, SIL.Machine.PunctuationAnalysis.QuotationMarkUpdateStrategy Strategy)>'

Check failure on line 324 in src/Serval/src/Serval.Translation/Services/PretranslationService.cs

View workflow job for this annotation

GitHub Actions / Build

Cannot implicitly convert type 'System.Collections.Generic.List<SIL.Machine.PunctuationAnalysis.QuotationMarkUpdateStrategy>' to 'System.Collections.Generic.List<(int ChapterNumber, SIL.Machine.PunctuationAnalysis.QuotationMarkUpdateStrategy Strategy)>'

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 @@
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
Loading