refactor: simplify AgeGroup enum and enhance normalization
Remove redundant ELIT variants from AgeGroup enum and implement case/spelling tolerant normalization function. The new normalize_age_group function handles case-insensitive matching and spelling variations (Elite/ELIT/Elit) while mapping to canonical enum values. This reduces the enum from 15 to 13 entries while maintaining full backward compatibility.
This commit is contained in:
@@ -102,8 +102,6 @@ class AgeGroup(str, Enum):
|
|||||||
u13p = "U13 (Prép)"
|
u13p = "U13 (Prép)"
|
||||||
u14ev = "U14 (Elite Vernets)"
|
u14ev = "U14 (Elite Vernets)"
|
||||||
u14esmv = "U14 (Elite Sous-Moulin/Vergers)"
|
u14esmv = "U14 (Elite Sous-Moulin/Vergers)"
|
||||||
u14elsmv = "U14 (ELIT Sous-Moulin/Vergers)"
|
|
||||||
u14elv = "U14 (ELIT Vernets)"
|
|
||||||
u15t = "U15 (Top)"
|
u15t = "U15 (Top)"
|
||||||
u15a = "U15 (A)"
|
u15a = "U15 (A)"
|
||||||
u16e = "U16 (Elite)"
|
u16e = "U16 (Elite)"
|
||||||
@@ -113,6 +111,38 @@ class AgeGroup(str, Enum):
|
|||||||
u21e = "U21 (ELIT)"
|
u21e = "U21 (ELIT)"
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_age_group(value: str) -> AgeGroup | None:
|
||||||
|
"""Normalize age group string to handle case and spelling variations."""
|
||||||
|
import re
|
||||||
|
|
||||||
|
if not isinstance(value, str):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def normalize_spelling(text: str) -> str:
|
||||||
|
"""Normalize spelling variations of 'elit' to 'elite'."""
|
||||||
|
# Replace accented versions first
|
||||||
|
text = text.replace("élit", "elite")
|
||||||
|
# Use regex to replace "elit" with "elite" only when it's a complete word
|
||||||
|
# This avoids replacing "elit" within "elite"
|
||||||
|
text = re.sub(r"\belit\b", "elite", text)
|
||||||
|
return text
|
||||||
|
|
||||||
|
# Convert to lowercase for case-insensitive comparison
|
||||||
|
input_lower = value.lower()
|
||||||
|
input_normalized = normalize_spelling(input_lower)
|
||||||
|
|
||||||
|
for member in AgeGroup:
|
||||||
|
# Convert enum value to lowercase and normalize
|
||||||
|
member_lower = member.value.lower()
|
||||||
|
member_normalized = normalize_spelling(member_lower)
|
||||||
|
|
||||||
|
# Check for match
|
||||||
|
if member_normalized == input_normalized:
|
||||||
|
return member
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class EventType(str, Enum):
|
class EventType(str, Enum):
|
||||||
game = "game"
|
game = "game"
|
||||||
practice = "practice"
|
practice = "practice"
|
||||||
@@ -396,9 +426,9 @@ def parse_schedule(
|
|||||||
data = json.loads(sanitized_content)
|
data = json.loads(sanitized_content)
|
||||||
# age_group filter
|
# age_group filter
|
||||||
if age_group:
|
if age_group:
|
||||||
events = [x for x in data if x["agegroup"] == age_group]
|
events = [x for x in data if normalize_age_group(x["agegroup"]) == age_group]
|
||||||
else:
|
else:
|
||||||
events = [x for x in data if x["agegroup"] in AgeGroup]
|
events = [x for x in data if normalize_age_group(x["agegroup"]) is not None]
|
||||||
# event_type filter
|
# event_type filter
|
||||||
if event_type_filter:
|
if event_type_filter:
|
||||||
if event_type_filter.value == EventType.game:
|
if event_type_filter.value == EventType.game:
|
||||||
@@ -438,7 +468,9 @@ def check_with_ai(
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
with schedule_file.open("r") as f:
|
with schedule_file.open("r") as f:
|
||||||
schedule_data = json.load(f)
|
schedule_data = json.load(f)
|
||||||
schedule_data = [x for x in schedule_data if x["agegroup"] in AgeGroup]
|
schedule_data = [
|
||||||
|
x for x in schedule_data if normalize_age_group(x["agegroup"]) is not None
|
||||||
|
]
|
||||||
for event in schedule_data:
|
for event in schedule_data:
|
||||||
event["team"] = event["agegroup"].replace("(", "").replace(")", "")
|
event["team"] = event["agegroup"].replace("(", "").replace(")", "")
|
||||||
del event["agegroup"]
|
del event["agegroup"]
|
||||||
|
|||||||
Reference in New Issue
Block a user