Files
poprhythm f916cfad6b Refactor event occurrence parsing by introducing modular components for improved maintainability
This commit restructures the EventOccurrenceParser by breaking down its functionality into modular components, including EventDefinitionResolver, LineClassifier, LocationPatternMatcher, SectionHeaderMatcher, TimeLocationParser, and TimeParser. This refactoring enhances code readability and maintainability, allowing for easier updates and testing. Additionally, the TextUtil class has been updated to include input sanitization methods. Comprehensive unit tests have been added to ensure the correctness of the new parsing logic and to validate the handling of various event occurrence scenarios.
2026-01-08 20:23:57 -05:00

63 lines
2.2 KiB
C#

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