Varshith dharmaj commited on
Commit
6631b9c
·
verified ·
1 Parent(s): 9b28b2a

Upload utils/error_classifier.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. utils/error_classifier.py +156 -0
utils/error_classifier.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Error Classification & Taxonomy
3
+ Classifies errors into 10+ error types with severity and fixability assessment
4
+ """
5
+
6
+ import re
7
+ from typing import Dict, Any, List
8
+
9
+
10
+ def classify_error(error: Dict[str, Any]) -> Dict[str, Any]:
11
+ """
12
+ Classify error into taxonomy with severity and fixability.
13
+
14
+ Args:
15
+ error: Error dictionary with type, found, correct, etc.
16
+
17
+ Returns:
18
+ Enhanced error dictionary with classification details
19
+ """
20
+ error_type = error.get("type", "unknown")
21
+ found = error.get("found", "")
22
+ correct = error.get("correct", "")
23
+ operation = error.get("operation", "")
24
+
25
+ # Classification mapping
26
+ classification_map = {
27
+ "calculation_error": {
28
+ "category": "Arithmetic Error",
29
+ "description": "Calculation mistakes in arithmetic operations",
30
+ "severity": "HIGH",
31
+ "fixability": 0.92, # 92% fixable
32
+ "fixable": True
33
+ },
34
+ "logical_error": {
35
+ "category": "Logical Error",
36
+ "description": "Contradictions, circular reasoning, or logical inconsistencies",
37
+ "severity": "MEDIUM",
38
+ "fixability": 0.60, # 60% fixable
39
+ "fixable": False
40
+ },
41
+ "operation_mismatch": {
42
+ "category": "Operation Mismatch",
43
+ "description": "Text describes one operation but math uses another",
44
+ "severity": "HIGH",
45
+ "fixability": 0.68, # 68% fixable
46
+ "fixable": True
47
+ },
48
+ "semantic_error": {
49
+ "category": "Semantic Error",
50
+ "description": "Meaning doesn't match the mathematical expression",
51
+ "severity": "MEDIUM",
52
+ "fixability": 0.45, # 45% fixable
53
+ "fixable": False
54
+ }
55
+ }
56
+
57
+ # Determine classification based on error type
58
+ if error_type in classification_map:
59
+ classification = classification_map[error_type]
60
+ else:
61
+ # Try to infer from content
62
+ classification = _infer_classification(error, found, correct, operation)
63
+
64
+ # Additional error type detection from content
65
+ additional_types = _detect_additional_error_types(found, correct, operation)
66
+
67
+ # Enhance error dictionary
68
+ enhanced_error = error.copy()
69
+ enhanced_error.update({
70
+ "category": classification["category"],
71
+ "error_description": classification["description"],
72
+ "severity": classification["severity"],
73
+ "fixability_score": classification["fixability"],
74
+ "fixable": classification["fixable"],
75
+ "additional_types": additional_types
76
+ })
77
+
78
+ return enhanced_error
79
+
80
+
81
+ def _infer_classification(error: Dict[str, Any], found: str, correct: str, operation: str) -> Dict[str, Any]:
82
+ """Infer error classification from content when type is unknown."""
83
+ found_lower = found.lower()
84
+ correct_lower = correct.lower()
85
+
86
+ # Check for algebraic errors (variables)
87
+ if re.search(r'[a-zA-Z]', found):
88
+ return {
89
+ "category": "Algebraic Error",
90
+ "description": "Wrong operations on variables or algebraic expressions",
91
+ "severity": "HIGH",
92
+ "fixability": 0.75,
93
+ "fixable": True
94
+ }
95
+
96
+ # Check for unit errors
97
+ if re.search(r'\b(kg|g|m|cm|km|lb|oz|ft|in)\b', found_lower):
98
+ return {
99
+ "category": "Unit Error",
100
+ "description": "Wrong units or unit conversions",
101
+ "severity": "MEDIUM",
102
+ "fixability": 0.70,
103
+ "fixable": True
104
+ }
105
+
106
+ # Check for sign errors
107
+ if operation in ['+', '-']:
108
+ # Check if result has wrong sign
109
+ found_nums = re.findall(r'-?\d+\.?\d*', found)
110
+ correct_nums = re.findall(r'-?\d+\.?\d*', correct)
111
+ if found_nums and correct_nums:
112
+ try:
113
+ found_result = float(found_nums[-1])
114
+ correct_result = float(correct_nums[-1])
115
+ if abs(found_result) == abs(correct_result) and found_result != correct_result:
116
+ return {
117
+ "category": "Sign Error",
118
+ "description": "Wrong positive/negative sign",
119
+ "severity": "HIGH",
120
+ "fixability": 0.90,
121
+ "fixable": True
122
+ }
123
+ except:
124
+ pass
125
+
126
+ # Default to arithmetic error
127
+ return {
128
+ "category": "Arithmetic Error",
129
+ "description": "Calculation mistakes",
130
+ "severity": "HIGH",
131
+ "fixability": 0.85,
132
+ "fixable": True
133
+ }
134
+
135
+
136
+ def _detect_additional_error_types(found: str, correct: str, operation: str) -> List[str]:
137
+ """Detect additional error types from content."""
138
+ additional_types = []
139
+ found_lower = found.lower()
140
+
141
+ # Check for notation errors
142
+ if re.search(r'[≈~≈]', found) or re.search(r'\b(about|around|approximately)\b', found_lower):
143
+ additional_types.append("Notation Error")
144
+
145
+ # Check for order of operations issues
146
+ if re.search(r'\d+\s*[+\-*/]\s*\d+\s*[+\-*/]\s*\d+', found):
147
+ # Multiple operations without parentheses might indicate order issue
148
+ if '(' not in found and '(' in correct:
149
+ additional_types.append("Order of Operations")
150
+
151
+ # Check for conceptual errors (complex patterns)
152
+ if len(found.split()) > 10: # Very long expressions might indicate conceptual issues
153
+ additional_types.append("Conceptual Error")
154
+
155
+ return additional_types
156
+