Files
chapter-organizer/tools/GoogleSheetsScheduleImport/GlobalEventDeduplicator.cs
T
poprhythm f400813667 Update EventOccurrence parsing to use EventOccurrenceParseGroup for improved data structure
This commit refactors the EventOccurrence parsing logic to utilize the EventOccurrenceParseGroup class, enhancing the organization of parsed occurrences by grouping them based on event definitions and optional section levels. The changes include updates to the EventOccurrenceParseResult, EventOccurrenceParser, and EventOccurrenceParserService to accommodate the new grouping structure. Additionally, tests are modified to reflect these changes, ensuring that the parsing functionality remains intact and accurate. This refactor improves data handling and aligns with the overall architecture of the application.
2026-04-04 21:55:44 -04:00

55 lines
2.0 KiB
C#

namespace GoogleSheetsScheduleImport;
/// <summary>
/// Collapses duplicate site-wide rows (same event name, date, time) that appear in every location column
/// (e.g. CURFEW shaded across all rooms) into a single line with no location — still under
/// <c>General Schedule</c>, which the parser maps to <see cref="Core.Entities.EventDefinition.GeneralSchedule"/>.
/// </summary>
public static class GlobalEventDeduplicator
{
private static readonly HashSet<string> BuiltinSiteWideNames = new(StringComparer.OrdinalIgnoreCase)
{
"CURFEW"
};
/// <summary>
/// First occurrence in row/column order is kept; location is cleared so the import line is not room-specific.
/// </summary>
public static List<ParsedOccurrenceLine> Deduplicate(
IReadOnlyList<ParsedOccurrenceLine> lines,
IReadOnlyCollection<string>? extraSiteWideNames = null)
{
var siteWide = new HashSet<string>(BuiltinSiteWideNames, StringComparer.OrdinalIgnoreCase);
if (extraSiteWideNames != null)
{
foreach (var n in extraSiteWideNames)
{
if (!string.IsNullOrWhiteSpace(n))
siteWide.Add(n.Trim());
}
}
var seen = new HashSet<(string Name, string Month, int Day, string Time)>();
var ordered = lines.OrderBy(l => l.SourceRowStart).ThenBy(l => l.SourceCol).ToList();
var result = new List<ParsedOccurrenceLine>(ordered.Count);
foreach (var line in ordered)
{
var name = TextNormalization.ForEmitLine(line.Name);
if (string.IsNullOrEmpty(name) || !siteWide.Contains(name))
{
result.Add(line);
continue;
}
var key = (name, line.Month, line.Day, line.TimeRange);
if (!seen.Add(key))
continue;
result.Add(line with { Location = string.Empty, Name = name });
}
return result;
}
}