Varshith dharmaj commited on
Commit
95594cb
·
verified ·
1 Parent(s): 6631b9c

Upload utils/error_corrector.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. utils/error_corrector.py +144 -0
utils/error_corrector.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Automatic Error Correction
3
+ Applies corrections to fixable errors in solution steps
4
+ Tracks correction success rates
5
+ """
6
+
7
+ import re
8
+ from typing import List, Dict, Any
9
+ from sympy import sympify, simplify, N
10
+
11
+
12
+ def correct_solution(steps: List[str], errors: List[Dict[str, Any]]) -> Dict[str, Any]:
13
+ """
14
+ Automatically correct fixable errors in solution steps.
15
+
16
+ Args:
17
+ steps: Original solution steps list
18
+ errors: List of error dictionaries
19
+
20
+ Returns:
21
+ Dictionary with corrected steps, correction log, and success rate
22
+ """
23
+ corrected_steps = steps.copy()
24
+ correction_log = []
25
+ manual_review_needed = []
26
+ fixed_count = 0
27
+
28
+ for error in errors:
29
+ step_number = error.get("step_number", 0) - 1 # Convert to 0-based index
30
+ if step_number < 0 or step_number >= len(corrected_steps):
31
+ continue
32
+
33
+ error_type = error.get("type", "")
34
+ fixable = error.get("fixable", False)
35
+
36
+ if not fixable:
37
+ manual_review_needed.append(error)
38
+ continue
39
+
40
+ # Attempt correction based on error type
41
+ if error_type == "calculation_error":
42
+ success = _correct_arithmetic_error(corrected_steps, step_number, error)
43
+ if success:
44
+ fixed_count += 1
45
+ correction_log.append({
46
+ "step": step_number + 1,
47
+ "type": "arithmetic",
48
+ "original": steps[step_number],
49
+ "corrected": corrected_steps[step_number],
50
+ "reason": "Arithmetic calculation corrected"
51
+ })
52
+ else:
53
+ manual_review_needed.append(error)
54
+
55
+ elif error_type == "operation_mismatch":
56
+ success = _correct_operation_mismatch(corrected_steps, step_number, error)
57
+ if success:
58
+ fixed_count += 1
59
+ correction_log.append({
60
+ "step": step_number + 1,
61
+ "type": "operation_mismatch",
62
+ "original": steps[step_number],
63
+ "corrected": corrected_steps[step_number],
64
+ "reason": "Operation mismatch corrected"
65
+ })
66
+ else:
67
+ manual_review_needed.append(error)
68
+
69
+ else:
70
+ # Other error types need manual review
71
+ manual_review_needed.append(error)
72
+
73
+ # Calculate success rate
74
+ total_fixable = len([e for e in errors if e.get("fixable", False)])
75
+ if total_fixable > 0:
76
+ success_rate = fixed_count / total_fixable
77
+ else:
78
+ success_rate = 0.0
79
+
80
+ return {
81
+ "corrected_steps": corrected_steps,
82
+ "correction_log": correction_log,
83
+ "success_rate": success_rate,
84
+ "manual_review_needed": manual_review_needed,
85
+ "fixed_count": fixed_count,
86
+ "total_fixable": total_fixable
87
+ }
88
+
89
+
90
+ def _correct_arithmetic_error(steps: List[str], step_index: int, error: Dict[str, Any]) -> bool:
91
+ """Correct arithmetic calculation error."""
92
+ try:
93
+ found = error.get("found", "")
94
+ correct = error.get("correct", "")
95
+
96
+ # Extract the incorrect result from found
97
+ found_nums = re.findall(r'\d+\.?\d*', found)
98
+ correct_nums = re.findall(r'\d+\.?\d*', correct)
99
+
100
+ if not found_nums or not correct_nums:
101
+ return False
102
+
103
+ incorrect_result = found_nums[-1]
104
+ correct_result = correct_nums[-1]
105
+
106
+ # Replace incorrect result with correct result in the step
107
+ step = steps[step_index]
108
+ # Replace the last occurrence of the incorrect result
109
+ corrected_step = step.replace(incorrect_result, correct_result, 1)
110
+
111
+ # If that didn't work, try more sophisticated replacement
112
+ if corrected_step == step:
113
+ # Try replacing the full expression
114
+ corrected_step = step.replace(found, correct)
115
+
116
+ steps[step_index] = corrected_step
117
+ return True
118
+ except Exception as e:
119
+ return False
120
+
121
+
122
+ def _correct_operation_mismatch(steps: List[str], step_index: int, error: Dict[str, Any]) -> bool:
123
+ """Correct operation mismatch error."""
124
+ try:
125
+ description = error.get("description", "")
126
+ step = steps[step_index]
127
+
128
+ # Extract operation from description
129
+ # This is simplified - in production would use more sophisticated NLP
130
+ if "subtract" in description.lower() and "+" in step:
131
+ # Replace + with -
132
+ corrected_step = step.replace("+", "-", 1)
133
+ steps[step_index] = corrected_step
134
+ return True
135
+ elif "add" in description.lower() and "-" in step:
136
+ # Replace - with +
137
+ corrected_step = step.replace("-", "+", 1)
138
+ steps[step_index] = corrected_step
139
+ return True
140
+
141
+ return False
142
+ except Exception as e:
143
+ return False
144
+