Spaces:
Sleeping
Sleeping
| import re | |
| def detect_common_error(text: str) -> str: | |
| """ | |
| Detect common student errors in STEM questions. | |
| Returns a short description or 'None detected'. | |
| """ | |
| # Literal divide by zero | |
| if re.search(r"/\s*0\b", text): | |
| return "Possible division by zero" | |
| # Double negative or sign confusion | |
| if re.search(r"--|\+\s*-(?!\d*\.)|-\s*\+", text): | |
| return "Possible sign error" | |
| # Number directly next to variable and another digit (e.g. "2x3") | |
| if re.search(r"\d[a-zA-Z]\d", text): | |
| return "Possible notation ambiguity" | |
| # Ambiguous fraction: e.g. 1/2x (is it x/2 or 1/(2x)?) | |
| if re.search(r"\d/\d+[a-zA-Z]", text): | |
| return "Possible ambiguous fraction notation" | |
| # Physics quantity with number but no units | |
| physics_terms = [ | |
| "force", "velocity", "acceleration", "mass", | |
| "energy", "distance", "speed", "weight", | |
| ] | |
| unit_pattern = r"\b(kg|g|m|s|n|j|w|pa|km|cm|mm|ms|mph|kph|hz|°c|°f|k|mol|newton|joule|watt|meter|second)\b" | |
| text_lower = text.lower() | |
| has_physics = any(t in text_lower for t in physics_terms) | |
| has_number = bool(re.search(r"\d", text)) | |
| has_unit = bool(re.search(unit_pattern, text_lower)) | |
| if has_physics and has_number and not has_unit: | |
| return "Possible missing units" | |
| return "None detected" | |