summaryrefslogtreecommitdiff
path: root/utils_math_eval.py
blob: d4a1db2bd0b723c6917785387105dcdde9c3a2e0 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# utils_math_eval.py
"""
Math Evaluation Utilities for RLVR Experiments.

This module provides utilities for:
- Extracting answers from model responses
- Verifying mathematical answers
- Computing accuracy metrics
"""

import re
from typing import Optional, List, Dict, Any, Tuple
import logging

logger = logging.getLogger(__name__)


# ============================================================================
# Answer Extraction
# ============================================================================

def extract_boxed_content(text: str) -> List[str]:
    """
    Extract all content from \\boxed{} patterns.
    
    Handles nested braces correctly.
    """
    results = []
    i = 0
    
    while i < len(text):
        # Find \boxed{
        idx = text.find("\\boxed{", i)
        if idx == -1:
            break
        
        # Find matching closing brace
        start = idx + 7  # len("\\boxed{")
        depth = 1
        j = start
        
        while j < len(text) and depth > 0:
            if text[j] == "{":
                depth += 1
            elif text[j] == "}":
                depth -= 1
            j += 1
        
        if depth == 0:
            content = text[start:j-1]
            results.append(content.strip())
        
        i = j
    
    return results


def extract_answer_from_boxed(text: str) -> Optional[str]:
    """Extract the last boxed answer from text."""
    boxed_contents = extract_boxed_content(text)
    if boxed_contents:
        return boxed_contents[-1]
    return None


def extract_answer_from_patterns(text: str) -> Optional[str]:
    """
    Extract answer using common natural language patterns.
    """
    # Patterns in order of priority
    patterns = [
        # Explicit answer statements
        (r"[Tt]he\s+(?:final\s+)?answer\s+is\s*[:\s]*(.+?)(?:\.|,|$)", 1),
        (r"[Aa]nswer\s*[:\s]+(.+?)(?:\.|,|$)", 1),
        
        # Conclusion patterns
        (r"[Tt]herefore\s*,?\s*(?:the\s+answer\s+is\s*)?(.+?)(?:\.|$)", 1),
        (r"[Hh]ence\s*,?\s*(?:the\s+answer\s+is\s*)?(.+?)(?:\.|$)", 1),
        (r"[Ss]o\s*,?\s*(?:the\s+answer\s+is\s*)?(.+?)(?:\.|$)", 1),
        (r"[Tt]hus\s*,?\s*(?:the\s+answer\s+is\s*)?(.+?)(?:\.|$)", 1),
        
        # Equation result
        (r"=\s*(\S+)\s*$", 1),
    ]
    
    for pattern, group in patterns:
        match = re.search(pattern, text, re.MULTILINE | re.IGNORECASE)
        if match:
            answer = match.group(group).strip()
            # Clean up trailing punctuation
            answer = re.sub(r"[.,;:!?]+$", "", answer).strip()
            if answer:
                return answer
    
    return None


def extract_final_answer(text: str) -> Optional[str]:
    """
    Extract the final answer from a model response.
    
    Priority:
    1. \\boxed{} format
    2. Natural language patterns
    """
    # Try boxed format first
    boxed = extract_answer_from_boxed(text)
    if boxed:
        return boxed
    
    # Try natural language patterns
    pattern_answer = extract_answer_from_patterns(text)
    if pattern_answer:
        return pattern_answer
    
    return None


# ============================================================================
# Answer Normalization
# ============================================================================

def normalize_numeric_answer(answer: str) -> Optional[float]:
    """
    Normalize a numeric answer for comparison.
    
    Handles:
    - Integers and decimals
    - Fractions (simple forms like a/b)
    - Scientific notation
    - Percentages
    """
    if not answer:
        return None
    
    # Clean up the string
    cleaned = answer.strip().lower()
    cleaned = cleaned.replace(" ", "")
    cleaned = cleaned.replace(",", "")
    
    # Handle percentages
    if cleaned.endswith("%"):
        cleaned = cleaned[:-1]
        try:
            return float(cleaned) / 100
        except ValueError:
            pass
    
    # Handle fractions (a/b)
    if "/" in cleaned:
        parts = cleaned.split("/")
        if len(parts) == 2:
            try:
                num = float(parts[0])
                denom = float(parts[1])
                if denom != 0:
                    return num / denom
            except ValueError:
                pass
    
    # Handle scientific notation and regular numbers
    try:
        return float(cleaned)
    except ValueError:
        pass
    
    return None


def normalize_text_answer(answer: str) -> str:
    """
    Normalize a text answer for comparison.
    
    - Lowercase
    - Remove extra whitespace
    - Remove common formatting
    """
    if not answer:
        return ""
    
    normalized = answer.strip().lower()
    
    # Remove LaTeX formatting
    normalized = re.sub(r"\\[a-zA-Z]+", "", normalized)
    normalized = re.sub(r"[{}$]", "", normalized)
    
    # Normalize whitespace
    normalized = " ".join(normalized.split())
    
    # Remove common punctuation
    normalized = re.sub(r"[.,;:!?]+$", "", normalized).strip()
    
    return normalized


# ============================================================================
# Answer Comparison
# ============================================================================

def compare_numeric_answers(
    predicted: str,
    ground_truth: str,
    tolerance: float = 1e-6
) -> bool:
    """
    Compare two answers numerically.
    
    Returns True if both can be parsed as numbers and are within tolerance.
    """
    pred_num = normalize_numeric_answer(predicted)
    gt_num = normalize_numeric_answer(ground_truth)
    
    if pred_num is None or gt_num is None:
        return False
    
    # Absolute tolerance for small numbers
    if abs(gt_num) < 1e-6:
        return abs(pred_num - gt_num) < tolerance
    
    # Relative tolerance for larger numbers
    rel_diff = abs(pred_num - gt_num) / abs(gt_num)
    return rel_diff < tolerance


def compare_text_answers(
    predicted: str,
    ground_truth: str
) -> bool:
    """Compare two text answers after normalization."""
    pred_norm = normalize_text_answer(predicted)
    gt_norm = normalize_text_answer(ground_truth)
    
    return pred_norm == gt_norm


def verify_answer(
    response: str,
    ground_truth: str,
    task_type: str = "math"
) -> Tuple[bool, Optional[str]]:
    """
    Verify if the response contains the correct answer.
    
    Args:
        response: Model's full response
        ground_truth: Expected answer
        task_type: Type of task ("math", "qa", "code")
        
    Returns:
        Tuple of (is_correct, extracted_answer)
    """
    # Extract predicted answer
    predicted = extract_final_answer(response)
    
    if predicted is None:
        return False, None
    
    # Try numeric comparison first
    if compare_numeric_answers(predicted, ground_truth):
        return True, predicted
    
    # Try text comparison
    if compare_text_answers(predicted, ground_truth):
        return True, predicted
    
    # Check if ground truth is contained in predicted
    gt_norm = normalize_text_answer(ground_truth)
    pred_norm = normalize_text_answer(predicted)
    
    if gt_norm and gt_norm in pred_norm:
        return True, predicted
    
    return False, predicted


# ============================================================================
# Batch Evaluation
# ============================================================================

def evaluate_batch(
    responses: List[str],
    ground_truths: List[str],
    task_type: str = "math"
) -> Dict[str, Any]:
    """
    Evaluate a batch of responses.
    
    Args:
        responses: List of model responses
        ground_truths: List of expected answers
        task_type: Type of task
        
    Returns:
        Dictionary with evaluation metrics
    """
    assert len(responses) == len(ground_truths), \
        "Number of responses must match ground truths"
    
    correct = 0
    total = len(responses)
    results = []
    
    for response, gt in zip(responses, ground_truths):
        is_correct, extracted = verify_answer(response, gt, task_type)
        correct += int(is_correct)
        results.append({
            "is_correct": is_correct,
            "extracted_answer": extracted,
            "ground_truth": gt
        })
    
    accuracy = correct / total if total > 0 else 0.0
    
    return {
        "accuracy": accuracy,
        "correct": correct,
        "total": total,
        "results": results
    }


# ============================================================================
# Answer Format Detection
# ============================================================================

def detect_answer_format(text: str) -> str:
    """
    Detect the format of an answer.
    
    Returns one of: "boxed", "numeric", "fraction", "text", "unknown"
    """
    if "\\boxed{" in text:
        return "boxed"
    
    # Check for fraction
    if re.match(r"^-?\d+/\d+$", text.strip()):
        return "fraction"
    
    # Check for numeric
    try:
        float(text.strip().replace(",", ""))
        return "numeric"
    except ValueError:
        pass
    
    if text.strip():
        return "text"
    
    return "unknown"


def format_answer_for_display(answer: str, detected_format: str) -> str:
    """Format answer for display based on detected format."""
    if detected_format == "fraction":
        num = normalize_numeric_answer(answer)
        if num is not None:
            return f"{answer} ≈ {num:.6f}"
    
    if detected_format == "numeric":
        try:
            num = float(answer.replace(",", ""))
            return f"{num:g}"
        except ValueError:
            pass
    
    return answer