"""Representation-aware parsing; preserve explicit time qualifiers for separate checks.""" import re from numeric_target_scoring import N, U, SEP, unit, parse_answer as parse_v1 def parse_answer(text, expected_kind): if not isinstance(text, str): return None clean = text.strip().replace('−', '-') marker = re.match(r'^(approximately|about|~)\s*', clean, re.I) approximate = marker.group(1) if marker else None if marker: clean = clean[marker.end():] if clean.endswith('.'): clean = clean[:-1].rstrip() result = parse_v1(clean, expected_kind) if result is None and expected_kind == 'estimate_with_ci': pattern = (rf'(?P{N})\s*(?P{U})\s*' rf'(?:at\s+(?P{N})\s+(?Pyears?|months?|days?))?\s*\(\s*' rf'(?:(?P{N})\s*%\s*(?:CI|confidence interval)\s*[:,]?\s*)?' rf'(?P{N})\s*(?P{U})?\s*{SEP}\s*' rf'(?P{N})\s*(?P{U})?\s*\)') match = re.fullmatch(pattern, clean, re.I) if match: data = {k: v for k, v in match.groupdict().items() if v is not None} if any(unit(data[k]) != unit(data['unit']) for k in ('lower_unit', 'upper_unit') if k in data): return None data.pop('lower_unit', None); data.pop('upper_unit', None) if 'time_value' in data: data['reported_timepoint'] = {'value': data.pop('time_value'), 'unit': unit(data.pop('time_unit'))} result = {'kind': 'estimate_with_ci', **data} if result is not None and approximate: result['approximation_marker'] = approximate return result