"""Numerical content only; no independent source entailment or clinical-gold claim.""" import json import re from numeric_target_scoring import number, score, unit from numeric_target_scoring_v2 import parse_answer def parse_json_output(text): try: return json.loads(text) except (ValueError, TypeError): try: return json.loads(re.sub(r'^```(?:json)?\s*|\s*```$', '', text.strip())) except (ValueError, TypeError, AttributeError): return None def structured_numeric_content(target, parsed): readable = isinstance(parsed, dict) and isinstance(parsed.get('answerable'), bool) numeric = ({k: v for k, v in parsed.items() if v not in ('', None)} if readable and parsed['answerable'] and parsed.get('kind') in ('scalar', 'range', 'estimate_with_ci') else None) rejected = bool(readable and not parsed['answerable'] and parsed.get('kind') == 'unanswerable' and all(parsed.get(k) in ('', None) for k in ('value', 'lower', 'upper', 'confidence_level', 'unit'))) result = score(target, numeric) if target is not None else None confidence_ok = target is not None and (target['kind'] != 'estimate_with_ci' or bool(numeric) and number(numeric.get('confidence_level')) == number(target['confidence_level'])) return {'numeric_projection': numeric, 'numeric_contract': result, 'numeric_content_complete': bool(result and result['fully_correct_on_numeric_contract'] and confidence_ok), 'content_unanswerable': rejected} def plain_numeric_content(target, parsed, requested_timepoint=None): answer = parsed.get('answer') if isinstance(parsed, dict) else None readable = isinstance(answer, str) numeric = parse_answer(answer, target['kind']) if target is not None else None result = score(target, numeric) if target is not None else None confidence_ok = target is not None and (target['kind'] != 'estimate_with_ci' or bool(numeric) and number(numeric.get('confidence_level')) == number(target['confidence_level'])) time = numeric.get('reported_timepoint') if numeric else None time_ok = (not time or bool(requested_timepoint) and number(time['value']) == number(requested_timepoint['value']) and unit(time['unit']) == unit(requested_timepoint['unit'])) return {'numeric_contract': result, 'numeric_content_complete': bool(result and result['fully_correct_on_numeric_contract'] and confidence_ok and time_ok), 'content_unanswerable': bool(readable and answer == 'unanswerable')} def numeric_reference_gate(target, answer): parsed = parse_answer(answer, target['kind']); result = score(target, parsed) if result['fully_correct_on_numeric_contract']: return {'state': 'complete_numeric_reference_match', 'numeric_contract': result, 'whole_question_semantics_verified': False} if (target['kind'] == 'estimate_with_ci' and parsed and parsed['kind'] == 'scalar' and number(parsed.get('value')) == number(target['value']) and result['unit_matches']): state = 'matching_point_estimate_missing_requested_interval' elif (target['kind'] == 'range' and parsed and parsed['kind'] == 'scalar' and result['unit_matches'] and number(parsed.get('value')) in (number(target['lower']), number(target['upper']))): state = 'range_endpoint_only_semantic_status_unresolved' else: state = 'numeric_reference_conflict' return {'state': state, 'numeric_contract': result, 'whole_question_semantics_verified': False}