Skip to content

Commit fc8b11f

Browse files
authored
fix: count dates written with a multi-character date separator as the culture's own (#186)
The profiler counts, per culture, how many dates are written with that culture's own date separator (`CultureParseCounts.DatesWithOwnSeparator`). That count breaks the tie between cultures that read every value as a date: it decides which culture's extremes `ColumnFacts` publishes and the order of date hypotheses (#58). **Defect.** The separator was kept only when it was one character; a longer one became `'\0'` and was never matched. 55 .NET cultures separate with more than one character: `". "` (27 — hu, ko, bs, hr-BA, en-HU, en-SI and others, e.g. `2024. 01. 15.`) and a right-to-left mark followed by `/` (28 Arabic cultures, e.g. `15‏/1‏/2024`). For them the count was always zero, so a column of Hungarian-written dates ranked en-US first whenever en-US read the same dates, only because `en-US` sorts before `hu-HU`. **Change.** `CultureAccumulator` keeps the culture's separator as a string and matches it whole, ordinal (`string.Contains(string, StringComparison.Ordinal)`, which allocates nothing). The match runs only for values that already read as a date under that culture, as before; an empty separator never counts. One-character separators (de-DE `.`, en-US `/`) count exactly as before. The `DatesWithOwnSeparator` docs say the separator is matched whole. **Tests** (`AmbiguousDateTests`), each failing before the change except the one-character case: - de-DE and en-US dates still count as written with their own separator; - under hu-HU, `2024. 01. 15.`-style dates count as written with its own separator; - with en-US and hu-HU both reading those dates, the hu-HU date hypothesis now ranks first; - under ar-EG, dates written with the host's ar-EG separator count as its own. The separator comes from the host's ICU data, so this test skips where ar-EG's separator is one character or such dates do not read; on macOS it runs and passes. **Measured**, full analysis, minimum of 3 runs each, main → branch: 5M-row csv 13,848 → 13,652 ms; 3M-row csv 9,553 → 9,656 ms. Within noise (other work shared the machine). Refs #184.
1 parent e4008fa commit fc8b11f

3 files changed

Lines changed: 69 additions & 13 deletions

File tree

‎src/TriasDev.Tabular/Analysis/ColumnProfiler.cs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,14 @@ private sealed class CultureAccumulator(string name, int outlierLimit)
515515
? CultureInfo.InvariantCulture.NumberFormat
516516
: CultureInfo.GetCultureInfo(name).NumberFormat);
517517

518-
/// <summary>This culture's date separator, when it is one character: what its own dates are written with.</summary>
519-
private char DateSeparator { get; } = DateSeparatorOf(name);
520-
521-
private static char DateSeparatorOf(string name)
522-
{
523-
string separator = (name.Length == 0 ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(name)).DateTimeFormat.DateSeparator;
524-
return separator.Length == 1 ? separator[0] : '\0';
525-
}
518+
/// <summary>This culture's date separator: what its own dates are written with.</summary>
519+
/// <remarks>
520+
/// Kept whole, not as its first character (#184). 55 cultures separate with more than one
521+
/// character — <c>". "</c> for hu-HU and ko-KR (<c>2024. 01. 15.</c>), a right-to-left mark and
522+
/// a slash for the Arabic ones — and a one-character match never found their dates written their
523+
/// own way, so the tie-break between cultures that read every date counted zero for them.
524+
/// </remarks>
525+
private string DateSeparator { get; } = (name.Length == 0 ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(name)).DateTimeFormat.DateSeparator;
526526

527527
private int _integer;
528528
private int _decimal;
@@ -720,7 +720,8 @@ public void AcceptText(string text, int rowNumber, NumberRead number, bool could
720720
_date++;
721721
Widen(date);
722722

723-
if (DateSeparator != '\0' && text.Contains(DateSeparator, StringComparison.Ordinal))
723+
// Ordinal over the whole separator, which allocates nothing; an empty one never counts.
724+
if (DateSeparator.Length > 0 && text.Contains(DateSeparator, StringComparison.Ordinal))
724725
{
725726
_datesWithOwnSeparator++;
726727
}

‎src/TriasDev.Tabular/Analysis/CultureParseCounts.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public sealed record CultureParseCounts
2222
public required int Date { get; init; }
2323

2424
/// <summary>
25-
/// How many of <see cref="Date"/> are written with this culture's own date separator — a dot for
26-
/// de-DE, a slash for en-US. What breaks a tie between cultures that read every value as a date.
25+
/// How many of <see cref="Date"/> are written with this culture's own date separator, matched as a
26+
/// whole — a dot for de-DE, a slash for en-US, a dot and a space for hu-HU. What breaks a tie between
27+
/// cultures that read every value as a date.
2728
/// </summary>
2829
public int DatesWithOwnSeparator { get; init; }
2930

‎tests/TriasDev.Tabular.Tests/Analysis/AmbiguousDateTests.cs‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Text;
23

34
using TriasDev.Tabular.Csv;
@@ -17,14 +18,19 @@ public sealed class AmbiguousDateTests
1718

1819
private static readonly DateTime JanuaryEleventh = new(2018, 1, 11, 0, 0, 0, DateTimeKind.Unspecified);
1920

20-
private static ColumnProfile Profile(params string[] values)
21+
private static ColumnProfile Profile(params string[] values) => Profile(["", "de-DE", "en-US"], values);
22+
23+
private static ColumnProfile Profile(string[] cultures, params string[] values)
2124
{
2225
string csv = "id;signed\n" + string.Concat(values.Select((v, i) => $"{i + 1};{v}\n"));
2326
using CsvCursor cursor = new(new MemoryStream(Encoding.UTF8.GetBytes(csv)), "t.csv");
2427

25-
return TabularAnalyzer.Analyze(cursor, new AnalysisOptions { Cultures = ["", "de-DE", "en-US"] }, cancellationToken: Token).Sheets[0].Columns[1];
28+
return TabularAnalyzer.Analyze(cursor, new AnalysisOptions { Cultures = cultures }, cancellationToken: Token).Sheets[0].Columns[1];
2629
}
2730

31+
private static CultureParseCounts CountsOf(ColumnProfile column, string culture) =>
32+
column.Facts.ParseCounts.Single(c => c.Culture == culture);
33+
2834
[Fact]
2935
public void PutsTheCultureWhoseSeparatorTheDatesUseFirst()
3036
{
@@ -73,4 +79,52 @@ public void PublishesTheEarliestAndLatestDateOfTheReadingThatTakesThemAll()
7379
Assert.Equal(new DateTime(2018, 12, 24, 0, 0, 0, DateTimeKind.Unspecified), facts.MinDate);
7480
Assert.Equal(new DateTime(2018, 12, 25, 0, 0, 0, DateTimeKind.Unspecified), facts.MaxDate);
7581
}
82+
83+
[Theory]
84+
[InlineData("de-DE", "15.01.2024", "16.02.2024", "17.03.2024")]
85+
[InlineData("en-US", "1/15/2024", "2/16/2024", "3/17/2024")]
86+
public void CountsTheDatesWrittenWithAOneCharacterSeparator(string culture, params string[] values)
87+
{
88+
CultureParseCounts counts = CountsOf(Profile([culture], values), culture);
89+
90+
Assert.Equal(values.Length, counts.Date);
91+
Assert.Equal(values.Length, counts.DatesWithOwnSeparator);
92+
}
93+
94+
[Fact]
95+
public void CountsTheDatesWrittenWithTheHungarianSeparatorOfDotAndSpace()
96+
{
97+
// hu-HU separates the parts of a date with two characters, ". ", which a one-character match
98+
// never looked for: its own dates were counted as written with no separator of its own (#184).
99+
Assert.Equal(". ", CultureInfo.GetCultureInfo("hu-HU").DateTimeFormat.DateSeparator);
100+
101+
CultureParseCounts counts = CountsOf(Profile(["hu-HU"], "2024. 01. 15.", "2024. 02. 16.", "2024. 03. 17."), "hu-HU");
102+
103+
Assert.Equal(3, counts.Date);
104+
Assert.Equal(3, counts.DatesWithOwnSeparator);
105+
}
106+
107+
[Fact]
108+
public void PutsTheHungarianReadingFirstForDatesWrittenTheHungarianWay()
109+
{
110+
ColumnProfile column = Profile(["en-US", "hu-HU"], "2024. 01. 15.", "2024. 02. 16.", "2024. 03. 17.");
111+
112+
Assert.Equal(ColumnType.Date, column.Hypotheses[0].Type);
113+
Assert.Equal("hu-HU", column.Hypotheses[0].Culture);
114+
}
115+
116+
[Fact]
117+
public void CountsTheDatesWrittenWithTheArabicSeparatorOfMarkAndSlash()
118+
{
119+
// ar-EG writes a right-to-left mark before its slash. Which separator a culture has comes from
120+
// the host's ICU data, so the dates are written with whatever separator this host gives it.
121+
string separator = CultureInfo.GetCultureInfo("ar-EG").DateTimeFormat.DateSeparator;
122+
Assert.SkipWhen(separator.Length < 2, $"This host's ar-EG date separator is one character ('{separator}').");
123+
124+
string[] values = [.. new[] { 15, 16, 17 }.Select(day => string.Join(separator, day, 1, 2024))];
125+
CultureParseCounts counts = CountsOf(Profile(["ar-EG"], values), "ar-EG");
126+
Assert.SkipWhen(counts.Date != values.Length, "This host does not read ar-EG dates written with its own separator.");
127+
128+
Assert.Equal(values.Length, counts.DatesWithOwnSeparator);
129+
}
76130
}

0 commit comments

Comments
 (0)