Enhance event occurrence parsing to skip unmatched high school section headers
This commit introduces a new property to track skipped high school section headers in the EventOccurrenceParseResult and EventOccurrenceParserResult classes. The EventOccurrenceParser has been updated to gracefully skip HS section headers that do not match any event definitions, improving the parsing logic. Additionally, the LocationParsingConfiguration has been removed from the EventOccurrenceParser, simplifying its constructor. Unit tests have been updated to reflect these changes and ensure correct behavior during parsing.
This commit is contained in:
@@ -13,19 +13,18 @@ public class EventOccurrenceParserResult
|
||||
{
|
||||
public IDictionary<EventDefinition, List<Entities.EventOccurrence>> Occurrences { get; set; } = new Dictionary<EventDefinition, List<Entities.EventOccurrence>>();
|
||||
public List<ParsingIssue> Issues { get; set; } = new();
|
||||
public List<string> SkippedHSSectionHeaders { get; set; } = new();
|
||||
}
|
||||
|
||||
public class EventOccurrenceParser
|
||||
{
|
||||
private FileSystemInfo _txtFile;
|
||||
private ICollection<EventDefinition> _events;
|
||||
private LocationParsingConfiguration? _locationConfig;
|
||||
|
||||
public EventOccurrenceParser(FileSystemInfo txtFile, ICollection<EventDefinition> events, LocationParsingConfiguration? locationConfig = null)
|
||||
public EventOccurrenceParser(FileSystemInfo txtFile, ICollection<EventDefinition> events)
|
||||
{
|
||||
_events = events;
|
||||
_txtFile = txtFile;
|
||||
_locationConfig = locationConfig;
|
||||
}
|
||||
|
||||
public EventOccurrenceParserResult Parse()
|
||||
@@ -34,6 +33,8 @@ public class EventOccurrenceParser
|
||||
var occurrences = result.Occurrences;
|
||||
var issues = result.Issues;
|
||||
EventDefinition? currentEventDefinition = null;
|
||||
bool inContinuationMode = false;
|
||||
bool inHSSection = false;
|
||||
|
||||
var lines = File.ReadLines(_txtFile.FullName);
|
||||
foreach (var (line, index) in lines.Select((line, index) => (line, index + 1)))
|
||||
@@ -44,11 +45,19 @@ public class EventOccurrenceParser
|
||||
|
||||
// Skip empty lines
|
||||
if (EventOccurrenceParsers.LineClassifier.IsEmptyLine(normalizedLine))
|
||||
{
|
||||
// Empty lines break continuation mode
|
||||
inContinuationMode = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip comment lines (starting with "#") - use grammar parser
|
||||
if (EventOccurrenceParsers.LineClassifier.IsCommentLine(normalizedLine))
|
||||
{
|
||||
// Comment lines break continuation mode
|
||||
inContinuationMode = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Try to parse occurrence line using grammar parser
|
||||
var occurrenceLine = EventOccurrenceGrammar.TryParseOccurrenceLine(normalizedLine);
|
||||
@@ -61,10 +70,23 @@ public class EventOccurrenceParser
|
||||
{
|
||||
var (eventNamePart, schoolLevel) = sectionHeader.Value;
|
||||
|
||||
// Section headers break continuation mode
|
||||
inContinuationMode = false;
|
||||
|
||||
// Use fuzzy matching to find the best matching event definition
|
||||
var evt = EventOccurrenceParsers.SectionHeaderMatcher.MatchEventDefinition(eventNamePart, _events);
|
||||
if (evt == null)
|
||||
{
|
||||
// Check if this is an HS event - if so, skip gracefully
|
||||
if (schoolLevel.Equals("HS", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
result.SkippedHSSectionHeaders.Add(normalizedLine);
|
||||
currentEventDefinition = null; // Skip subsequent occurrences
|
||||
inHSSection = true; // Mark that we're in an HS section
|
||||
continue; // No issue created
|
||||
}
|
||||
|
||||
// For non-HS unmatched headers, create issue as before
|
||||
var bestRatio = EventOccurrenceParsers.SectionHeaderMatcher.GetBestMatchRatio(eventNamePart, _events);
|
||||
issues.Add(new ParsingIssue
|
||||
{
|
||||
@@ -76,12 +98,16 @@ public class EventOccurrenceParser
|
||||
continue;
|
||||
}
|
||||
currentEventDefinition = evt;
|
||||
inHSSection = false; // Reset HS section flag for MS events
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for General Schedule/Session using grammar parser
|
||||
if (EventOccurrenceParsers.SectionHeaderMatcher.IsGeneralSchedule(normalizedLine))
|
||||
{
|
||||
// General schedule breaks continuation mode
|
||||
inContinuationMode = false;
|
||||
inHSSection = false; // Reset HS section flag
|
||||
currentEventDefinition = EventDefinition.GeneralSchedule;
|
||||
continue;
|
||||
}
|
||||
@@ -89,9 +115,22 @@ public class EventOccurrenceParser
|
||||
// Also check for simple "MS" or "HS" in line (backward compatibility)
|
||||
if (EventOccurrenceParsers.SectionHeaderMatcher.HasSchoolLevel(normalizedLine))
|
||||
{
|
||||
// Section headers break continuation mode
|
||||
inContinuationMode = false;
|
||||
|
||||
var evt = EventOccurrenceParsers.SectionHeaderMatcher.MatchEventDefinition(normalizedLine, _events);
|
||||
if (evt == null)
|
||||
{
|
||||
// Check if this is an HS event - if so, skip gracefully
|
||||
if (normalizedLine.Contains("HS", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
result.SkippedHSSectionHeaders.Add(normalizedLine);
|
||||
currentEventDefinition = null; // Skip subsequent occurrences
|
||||
inHSSection = true; // Mark that we're in an HS section
|
||||
continue; // No issue created
|
||||
}
|
||||
|
||||
// For non-HS unmatched headers, create issue as before
|
||||
var bestRatio = EventOccurrenceParsers.SectionHeaderMatcher.GetBestMatchRatio(normalizedLine, _events);
|
||||
issues.Add(new ParsingIssue
|
||||
{
|
||||
@@ -103,11 +142,18 @@ public class EventOccurrenceParser
|
||||
continue;
|
||||
}
|
||||
currentEventDefinition = evt;
|
||||
inHSSection = false; // Reset HS section flag for MS events
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip continuation lines (lines that look like they're continuing from previous line)
|
||||
if (EventOccurrenceParsers.LineClassifier.IsContinuationLine(normalizedLine))
|
||||
// Check if line starts with "*" to enter continuation mode
|
||||
if (normalizedLine.TrimStart().StartsWith("*", StringComparison.Ordinal))
|
||||
{
|
||||
inContinuationMode = true;
|
||||
}
|
||||
|
||||
// Skip continuation lines (in continuation mode OR line starts with "*" or is parenthetical)
|
||||
if (inContinuationMode || EventOccurrenceParsers.LineClassifier.IsContinuationLine(normalizedLine))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -127,6 +173,15 @@ public class EventOccurrenceParser
|
||||
continue;
|
||||
}
|
||||
|
||||
// Occurrence lines break continuation mode
|
||||
inContinuationMode = false;
|
||||
|
||||
// Skip occurrences under HS sections (they won't match any event definition)
|
||||
if (inHSSection)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var (occurrenceName, month, dayOfMonthStr, timeAndLocation) = occurrenceLine.Value;
|
||||
|
||||
// Remove weekday suffix from occurrence name if present
|
||||
@@ -151,23 +206,8 @@ public class EventOccurrenceParser
|
||||
|
||||
// timeAndLocation is already normalized (hyphens normalized) since normalizedLine was sanitized
|
||||
|
||||
// Parse time and location using configurable patterns
|
||||
EventOccurrenceParsers.TimeLocationParser.Parse(timeAndLocation, _locationConfig, out string time, out string location, out bool locationParseSuccess);
|
||||
|
||||
// Track location parsing failure if patterns are configured but none matched
|
||||
if (!locationParseSuccess && !string.IsNullOrWhiteSpace(location))
|
||||
{
|
||||
if (_locationConfig != null && _locationConfig.LocationPatterns.Any())
|
||||
{
|
||||
issues.Add(new ParsingIssue
|
||||
{
|
||||
LineNumber = index,
|
||||
LineContent = normalizedLine,
|
||||
IssueType = ParsingIssueType.LocationParseFailure,
|
||||
Message = $"Location '{location}' does not match any configured pattern"
|
||||
});
|
||||
}
|
||||
}
|
||||
// Parse time and location - extract time using regex, then use everything after time as location
|
||||
EventOccurrenceParsers.TimeLocationParser.Parse(timeAndLocation, out string time, out string location);
|
||||
|
||||
// Parse date
|
||||
DateOnly? startDate = null;
|
||||
@@ -222,6 +262,9 @@ public class EventOccurrenceParser
|
||||
if (!occurrences.ContainsKey(eventDefinition))
|
||||
occurrences.Add(eventDefinition, []);
|
||||
occurrences[eventDefinition].Add(eventOccurrence);
|
||||
|
||||
// Reset HS section flag when we successfully parse an occurrence (means we're in a valid section)
|
||||
inHSSection = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user