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
21 changes: 16 additions & 5 deletions src/SIL.Machine/PunctuationAnalysis/QuotationMarkFinder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using PCRE;

Expand Down Expand Up @@ -43,11 +44,21 @@ public List<QuotationMarkStringMatch> FindAllPotentialQuotationMarksInTextSegmen
_quoteConventions.IsValidOpeningQuotationMark(match.Groups[0].Value)
|| _quoteConventions.IsValidClosingQuotationMark(match.Groups[0].Value)
)
.Select(m => new QuotationMarkStringMatch(
textSegment,
m.Groups[0].Index,
m.Groups[0].Index + m.Groups[0].Length
))
.Select(m =>
{
int[] textElementIndices = StringInfo.ParseCombiningCharacters(textSegment.Text);
int startIndex = 0;
int endIndex = textElementIndices.Length;
for (int textElementIndex = 0; textElementIndex < textElementIndices.Length; textElementIndex++)
{
int stringIndex = textElementIndices[textElementIndex];
if (stringIndex == m.Groups[0].Index)
startIndex = textElementIndex;
if (stringIndex == m.Groups[0].EndIndex)
endIndex = textElementIndex;
}
return new QuotationMarkStringMatch(textSegment, startIndex, endIndex);
})
.ToList();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/SIL.Machine/PunctuationAnalysis/TextSegment.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using SIL.Machine.Corpora;

namespace SIL.Machine.PunctuationAnalysis
Expand Down Expand Up @@ -70,7 +71,7 @@ public override int GetHashCode()
return hashCode * 31 + ImmediatePrecedingMarker.GetHashCode();
}

public int Length => Text.Length;
public int Length => StringInfo.ParseCombiningCharacters(Text).Length;

public string SubstringBefore(int index)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/CorporaTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ internal static class CorporaTestHelpers
);
public static readonly string UsfmTestProjectPath = Path.Combine(TestDataPath, "usfm", "Tes");
public static readonly string UsfmTargetProjectPath = Path.Combine(TestDataPath, "usfm", "target");
public static readonly string UsfmTargetProjectZipPath = Path.Combine(TestDataPath, "project", "target");
public static readonly string UsfmTargetCustomVrsPath = Path.Combine(TestDataPath, "usfm", "target", "custom.vrs");
public static readonly string UsfmSourceProjectPath = Path.Combine(TestDataPath, "usfm", "source");
public static readonly string UsfmSourceProjectZipPath = Path.Combine(TestDataPath, "project", "source");
public static readonly string UsxTestProjectPath = Path.Combine(TestDataPath, "usx", "Tes");
public static readonly string TextTestProjectPath = Path.Combine(TestDataPath, "txt");
public static readonly string DeuterocanonicalsSourcePath = Path.Combine(
Expand Down
25 changes: 25 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/UsfmManualTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO.Compression;
using System.Text.Json;
using NUnit.Framework;
using SIL.Machine.PunctuationAnalysis;

namespace SIL.Machine.Corpora;

Expand Down Expand Up @@ -170,4 +171,28 @@ async Task GetUsfmAsync(string projectPath)
await GetUsfmAsync(ParatextProjectPath);
}
}

[Test]
[Ignore("This is for manual testing only. Remove this tag to run the test.")]
public void AnalyzeCorporaQuoteConventions()
{
var sourceHandler = new QuoteConventionDetector();
using ZipArchive zipArchive = ZipFile.OpenRead(CorporaTestHelpers.UsfmSourceProjectZipPath);
var quoteConventionDetector = new ZipParatextProjectQuoteConventionDetector(zipArchive);
quoteConventionDetector.GetQuoteConventionAnalysis(sourceHandler);

var targetHandler = new QuoteConventionDetector();
using ZipArchive zipArchive2 = ZipFile.OpenRead(CorporaTestHelpers.UsfmTargetProjectZipPath);
var quoteConventionDetector2 = new ZipParatextProjectQuoteConventionDetector(zipArchive2);
quoteConventionDetector2.GetQuoteConventionAnalysis(targetHandler);

QuoteConventionAnalysis sourceAnalysis = sourceHandler.DetectQuotationConvention();
QuoteConventionAnalysis targetAnalysis = targetHandler.DetectQuotationConvention();

Assert.Multiple(() =>
{
Assert.NotNull(sourceAnalysis);
Assert.NotNull(targetAnalysis);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ public void ThatAllPossibleQuotationMarksAreIdentified()
]
)
);

Assert.That(
quotationMarkFinder
.FindAllPotentialQuotationMarksInTextSegment(
new TextSegment.Builder().SetText("उत्पत्ति \"पुस्तकले").Build()
)
.SequenceEqual(
[
new QuotationMarkStringMatch(
new TextSegment.Builder().SetText("उत्पत्ति \"पुस्तकले").Build(),
6,
7
),
]
)
);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public void Length()

textSegment = new TextSegment.Builder().SetText("new example text").Build();
Assert.That(textSegment.Length, Is.EqualTo("new example text".Length));
textSegment = new TextSegment.Builder().SetText("उत्पत्ति पुस्तकले").Build();
Assert.That(textSegment.Length, Is.EqualTo(11));
}

[Test]
Expand Down
Loading