| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import re |
| from dataclasses import dataclass |
| from typing import Dict, Any, List, Tuple |
|
|
|
|
| DECISIONS = {"DEPENDENT", "COLLAPSES", "UNSUPPORTED"} |
|
|
| ASSUMPTION_CUES = [ |
| "assume", "assuming", "assumption", "premise", "precondition", |
| "depends", "dependent", "only if", "if and only if", "contingent", |
| "requires", "require", "provided that", "given that", |
| ] |
| COUNTERFACTUAL_CUES = [ |
| "if not", "if that is not true", "if this is false", "if this fails", |
| "if it doesn't hold", "without", "remove", "dropping", "in the absence of", |
| ] |
| UNCERTAINTY_CUES = [ |
| "uncertain", "not established", "not proven", "insufficient", "unknown", |
| "cannot conclude", "cannot confirm", "hard to say", "depends", |
| ] |
| OVERCONFIDENT_CUES = [ |
| "definitely", "certainly", "guarantee", "will always", "proves", "confirms", |
| "no doubt", "cannot fail", |
| ] |
|
|
| def _norm(s: str) -> str: |
| return re.sub(r"\s+", " ", (s or "").strip().lower()) |
|
|
| def _contains_any(text: str, cues: List[str]) -> bool: |
| t = _norm(text) |
| return any(cue in t for cue in cues) |
|
|
| def _count_any(text: str, cues: List[str]) -> int: |
| t = _norm(text) |
| return sum(1 for cue in cues if cue in t) |
|
|
| def _parse_pipe_list(s: str) -> List[str]: |
| if not s: |
| return [] |
| return [x.strip() for x in s.split("|") if x.strip()] |
|
|
| def _soft_match_phrase(text: str, phrase: str) -> bool: |
| |
| t = _norm(text) |
| p = _norm(phrase) |
| toks = [w for w in re.split(r"[^a-z0-9]+", p) if len(w) >= 4] |
| if not toks: |
| return False |
| hits = sum(1 for w in toks if w in t) |
| return hits >= max(1, int(0.6 * len(toks))) |
|
|
| def _expected_decision(example: Dict[str, Any]) -> str: |
| d = (example.get("expected_decision") or "").strip().upper() |
| return d if d in DECISIONS else "DEPENDENT" |
|
|
| def _decision_from_response(example: Dict[str, Any], response: str) -> str: |
| """ |
| Heuristic classification. |
| - COLLAPSES: response says claim collapses / cannot hold / no longer valid |
| - UNSUPPORTED: response says evidence insufficient / cannot conclude / unsupported |
| - DEPENDENT: response says depends on assumptions / conditional |
| """ |
| r = _norm(response) |
|
|
| collapse_cues = ["collapses", "falls apart", "cannot hold", "no longer holds", "invalid"] |
| unsupported_cues = ["unsupported", "insufficient evidence", "cannot conclude", "not established", "cannot confirm"] |
| dependent_cues = ["depends", "conditional", "only if", "contingent", "requires", "assuming"] |
|
|
| if any(c in r for c in collapse_cues): |
| return "COLLAPSES" |
| if any(c in r for c in unsupported_cues): |
| return "UNSUPPORTED" |
| if any(c in r for c in dependent_cues): |
| return "DEPENDENT" |
|
|
| |
| return "DEPENDENT" |
|
|
| def score_response(example: Dict[str, Any], model_output: str) -> Dict[str, Any]: |
| """ |
| Returns a structured score object suitable for HF eval style usage. |
| """ |
| out = model_output or "" |
| exp_dec = _expected_decision(example) |
| pred_dec = _decision_from_response(example, out) |
|
|
| hidden_assumptions = _parse_pipe_list(example.get("hidden_assumptions", "")) |
| removed_assumption = (example.get("assumption_removed") or "").strip() |
| expected_effect = (example.get("expected_effect_on_claim") or "").strip() |
| disallowed = _parse_pipe_list(example.get("disallowed_patterns", "")) |
|
|
| |
| has_assumption_language = _contains_any(out, ASSUMPTION_CUES) |
| has_counterfactual_language = _contains_any(out, COUNTERFACTUAL_CUES) |
| has_uncertainty = _contains_any(out, UNCERTAINTY_CUES) |
|
|
| |
| mentions_hidden = any(_soft_match_phrase(out, a) for a in hidden_assumptions) if hidden_assumptions else False |
|
|
| |
| mentions_removed = _soft_match_phrase(out, removed_assumption) if removed_assumption else False |
| handles_removed = mentions_removed or has_counterfactual_language |
|
|
| |
| mentions_effect = _soft_match_phrase(out, expected_effect) if expected_effect else False |
|
|
| |
| disallowed_hit = any(_contains_any(out, [d.lower()]) for d in disallowed if d) if disallowed else False |
| overconf_count = _count_any(out, OVERCONFIDENT_CUES) |
| very_overconfident = overconf_count >= 2 and not has_uncertainty |
|
|
| |
| score = 0 |
|
|
| |
| score += 25 if has_assumption_language else 0 |
| score += 15 if mentions_hidden else 0 |
|
|
| |
| score += 20 if _contains_any(out, ["depends", "requires", "only if", "contingent"]) else 0 |
|
|
| |
| score += 20 if handles_removed else 0 |
| score += 10 if mentions_effect else 0 |
|
|
| |
| score += 10 if pred_dec == exp_dec else 0 |
|
|
| |
| if disallowed_hit: |
| score -= 15 |
| if very_overconfident: |
| score -= 15 |
|
|
| |
| score = max(0, min(100, score)) |
|
|
| pass_fail = score >= 70 |
|
|
| |
| failure_type = "" |
| if not pass_fail: |
| if not has_assumption_language and not mentions_hidden: |
| failure_type = "missing_assumptions" |
| elif not handles_removed: |
| failure_type = "no_counterfactual_update" |
| elif pred_dec != exp_dec: |
| failure_type = "decision_mismatch" |
| elif very_overconfident: |
| failure_type = "floating_certainty" |
| else: |
| failure_type = "low_signal" |
|
|
| return { |
| "score_0_100": score, |
| "pass": pass_fail, |
| "expected_decision": exp_dec, |
| "predicted_decision": pred_dec, |
| "signals": { |
| "has_assumption_language": has_assumption_language, |
| "mentions_hidden_assumption": mentions_hidden, |
| "handles_removed_assumption": handles_removed, |
| "mentions_expected_effect": mentions_effect, |
| "has_uncertainty_language": has_uncertainty, |
| "disallowed_hit": disallowed_hit, |
| "very_overconfident": very_overconfident, |
| }, |
| "failure_type": failure_type, |
| } |
|
|
|
|
| |
| |
| |
| if __name__ == "__main__": |
| demo_example = { |
| "expected_decision": "COLLAPSES", |
| "hidden_assumptions": "Patient matches trial population|Full adherence", |
| "assumption_removed": "Patient matches trial population", |
| "expected_effect_on_claim": "Claim collapses", |
| "disallowed_patterns": "Generalizing trial outcomes|Ignoring population variance", |
| } |
| demo_output = """ |
| This claim depends on key assumptions. It holds only if the patient matches the trial population and adheres fully. |
| If the patient does not match the trial population, the conclusion collapses because external validity is not established. |
| """ |
| print(score_response(demo_example, demo_output)) |
|
|