Evaluates a single target-prediction pair: - Numeric within tolerance or exact year match inside this helper. - Falls back to ANLS for text.
(
target: str,
prediction: str,
max_relative_change: float = 0.05
)
| 55 | |
| 56 | |
| 57 | def evaluate_single_answer( |
| 58 | target: str, |
| 59 | prediction: str, |
| 60 | max_relative_change: float = 0.05 |
| 61 | ) -> float: |
| 62 | """ |
| 63 | Evaluates a single target-prediction pair: |
| 64 | - Numeric within tolerance or exact year match inside this helper. |
| 65 | - Falls back to ANLS for text. |
| 66 | """ |
| 67 | t = target.strip().strip('%').strip() |
| 68 | p = prediction.strip().strip('%').strip() |
| 69 | #print("Stripped", t, p) |
| 70 | # Attempt numeric |
| 71 | t_f = to_float(t) |
| 72 | p_f = to_float(p) |
| 73 | if t_f is not None and p_f is not None: |
| 74 | if t_f == 0.0: |
| 75 | return 1.0 if p_f == 0.0 else 0.0 |
| 76 | change = abs(p_f - t_f) / abs(t_f) |
| 77 | return 1.0 if change <= max_relative_change else 0.0 |
| 78 | # Fallback text |
| 79 | #print("P:", p, "T: ", t) |
| 80 | return anls_score(prediction=p.lower(), gold_labels=[t.lower()], threshold=0.5) |
| 81 | |
| 82 | |
| 83 | def relaxed_correctness_chartqapro( |
no test coverage detected