Files
chapter-organizer/tools/GoogleSheetsScheduleImport/OccurrenceEventMatcher.cs
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

72 lines
2.0 KiB
C#

using Core.Entities;
using Core.Models;
using FuzzySharp;
namespace GoogleSheetsScheduleImport;
/// <summary>
/// Maps a sheet cell title to the closest event definition (fuzzy) plus MS/HS for section headers.
/// </summary>
public static class OccurrenceEventMatcher
{
private const int MinTokenScore = 62;
public static bool TryMatch(
string occurrenceName,
IReadOnlyList<EventDefinition> events,
out EventDefinition? matched,
out SchoolLevel? level)
{
matched = null;
level = null;
if (string.IsNullOrWhiteSpace(occurrenceName) || events.Count == 0)
return false;
var normalized = TextNormalization.ForEmitLine(occurrenceName);
var (remainder, prefixLevel) = SchoolLevelPrefixParser.StripLeadingSchoolPrefix(normalized);
// Section headers require "Event Name - MS|HS". Titles without a clear school prefix stay in General Schedule.
if (!prefixLevel.HasValue)
return false;
var best = FindBest(remainder, events);
if (best == null || string.IsNullOrWhiteSpace(remainder))
return false;
matched = best;
level = prefixLevel;
return true;
}
private static EventDefinition? FindBest(string text, IReadOnlyList<EventDefinition> events)
{
if (string.IsNullOrWhiteSpace(text))
return null;
EventDefinition? best = null;
var bestScore = 0;
foreach (var e in events)
{
if (string.IsNullOrWhiteSpace(e.Name))
continue;
var score = Math.Max(
Fuzz.TokenSetRatio(text, e.Name),
Fuzz.PartialRatio(text, e.Name));
if (score > bestScore)
{
bestScore = score;
best = e;
}
else if (score == bestScore && best != null && e.Name.Length > best.Name.Length)
best = e;
}
if (best == null || bestScore < MinTokenScore)
return null;
return best;
}
}