using Core.Entities;
using FuzzySharp;
namespace Core.Parsers.EventOccurrence;
///
/// Matches section headers to event definitions using fuzzy matching.
///
public static class SectionHeaderMatcher
{
///
/// Checks if a line is a general schedule header.
///
public static bool IsGeneralSchedule(string line)
{
return EventOccurrenceGrammar.IsGeneralSchedule(line);
}
///
/// Checks if a line contains school level markers (MS or HS).
///
public static bool HasSchoolLevel(string line)
{
return line.Contains("MS", StringComparison.Ordinal) ||
line.Contains("HS", StringComparison.Ordinal);
}
///
/// Matches a section header to the best matching event definition using fuzzy matching.
///
/// The section header text to match.
/// The collection of available event definitions.
/// The best matching EventDefinition, or null if no match is found (ratio > 50).
public static EventDefinition? MatchEventDefinition(string sectionHeader, ICollection events)
{
var evt =
(from e in events
let rat = Fuzz.Ratio(e.Name, sectionHeader)
where rat > 50
orderby rat descending
select e).FirstOrDefault();
return evt;
}
///
/// Gets the best match ratio for a section header against events.
/// Useful for error messages when no match is found.
///
/// The section header text.
/// The collection of available event definitions.
/// The best match ratio, or 0 if no events are available.
public static int GetBestMatchRatio(string sectionHeader, ICollection events)
{
if (events.Count == 0)
return 0;
var bestEvent = events.FirstOrDefault();
return bestEvent != null ? Fuzz.Ratio(sectionHeader, bestEvent.Name) : 0;
}
}