File size: 1,340 Bytes
7e0eb7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"