class TypeComponents:
def __init__(self, types, parts):
self.Types = types
self.Parts = parts
YearNum_Group = TypeComponents(
(
RefYearAbbrNum,
RefYearAbbrNumTeam,
YearAbbrNum
),
lambda cmpts: (cmpts[0], cmpts[1], "")
)
def canonicalise_reference(reference_type, reference_match, canonical_form):
components = re.findall(r"\d+", reference_match)
if reference_type in YearNum_Group.Types:
year, d1, d2 = YearNum_Group.Parts(components)
elif reference_type in YearNumNum_Group:
year, d1, d2 = components[0], components[1], components[2]
elif reference_type in NumNum_Group:
year, d1, d2 = "", components[0], components[1]
corrected_reference = (canonical_form.replace("dddd", year)
.replace("d1", d1)
.replace("d2", d2))
return corrected_reference, year, d1, d2