File size: 1,710 Bytes
d6f4a79 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | """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<value>{N})\s*(?P<unit>{U})\s*'
rf'(?:at\s+(?P<time_value>{N})\s+(?P<time_unit>years?|months?|days?))?\s*\(\s*'
rf'(?:(?P<confidence_level>{N})\s*%\s*(?:CI|confidence interval)\s*[:,]?\s*)?'
rf'(?P<lower>{N})\s*(?P<lower_unit>{U})?\s*{SEP}\s*'
rf'(?P<upper>{N})\s*(?P<upper_unit>{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
|