File size: 3,558 Bytes
05872d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""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}