j-js commited on
Commit
df998e6
·
verified ·
1 Parent(s): 9e26223

Update explainers/explainer_algebra.py

Browse files
Files changed (1) hide show
  1. explainers/explainer_algebra.py +15 -37
explainers/explainer_algebra.py CHANGED
@@ -1,5 +1,3 @@
1
- # explainer_algebra.py
2
-
3
  import re
4
  from models import ExplainerResult
5
 
@@ -9,13 +7,13 @@ def explain_algebra_question(text: str) -> ExplainerResult | None:
9
 
10
  has_equation = "=" in t
11
  has_variable = bool(re.search(r"\b[a-z]\b", t))
12
- has_solve_word = "solve" in t or "value of x" in t or "value of y" in t
13
 
14
- if not has_equation and not has_variable and not has_solve_word:
 
15
  return None
16
 
17
- # avoid catching obvious non-algebra topics first
18
- if "%" in t or "percent" in t or "ratio" in t or re.search(r"\b\d+\s*:\s*\d+\b", t):
19
  return None
20
 
21
  givens = []
@@ -30,37 +28,17 @@ def explain_algebra_question(text: str) -> ExplainerResult | None:
30
  variables = [v for v in variables if v not in {"a", "i"}]
31
 
32
  if variables:
33
- givens.append(f"A variable appears in the question: {', '.join(variables[:3])}")
34
-
35
- numbers = re.findall(r"\b\d+(?:\.\d+)?\b", t)
36
- if numbers:
37
- givens.append(f"Numbers are included in the expression: {', '.join(numbers[:5])}")
38
 
39
- relationships.append("Both sides of the equation represent the same value")
40
- relationships.append("You need to isolate the variable while keeping the equation balanced")
41
 
42
- if "integer" in t:
43
- constraints.append("The variable may be restricted to integer values")
44
- if "positive" in t:
45
- constraints.append("The variable may need to be positive")
46
- if "negative" in t:
47
- constraints.append("The variable may be negative or restricted by sign")
48
-
49
- asks_for = None
50
- m = re.search(r"\bsolve for ([a-z])\b", t)
51
- if m:
52
- asks_for = f"the value of {m.group(1)}"
53
- else:
54
- m = re.search(r"\bwhat is the value of ([a-z])\b", t)
55
- if m:
56
- asks_for = f"the value of {m.group(1)}"
57
- elif "what is" in t or "find" in t:
58
- asks_for = "the value of the variable"
59
 
60
  trap_notes.extend([
61
- "Do not perform an operation on only one side of the equation",
62
- "Watch for sign mistakes when moving terms",
63
- "Be careful with brackets, distribution, and inverse operations",
64
  ])
65
 
66
  return ExplainerResult(
@@ -71,11 +49,11 @@ def explain_algebra_question(text: str) -> ExplainerResult | None:
71
  constraints=constraints,
72
  relationships=relationships,
73
  needed_concepts=[
74
- "equation balancing",
75
  "inverse operations",
76
- "isolating the variable",
 
77
  ],
78
  trap_notes=trap_notes,
79
- strategy_hint="Identify the variable, then think about what operations need to be undone to isolate it.",
80
- plain_english="The question is giving you an algebraic relationship and asking you to isolate the variable to find its value.",
81
  )
 
 
 
1
  import re
2
  from models import ExplainerResult
3
 
 
7
 
8
  has_equation = "=" in t
9
  has_variable = bool(re.search(r"\b[a-z]\b", t))
10
+ has_solve_word = "solve" in t or "value of" in t
11
 
12
+ # Avoid stealing other topics
13
+ if "%" in t or "percent" in t or "ratio" in t or re.search(r"\b\d+\s*:\s*\d+\b", t):
14
  return None
15
 
16
+ if not (has_equation or has_variable or has_solve_word):
 
17
  return None
18
 
19
  givens = []
 
28
  variables = [v for v in variables if v not in {"a", "i"}]
29
 
30
  if variables:
31
+ givens.append(f"A variable appears: {', '.join(variables[:3])}")
 
 
 
 
32
 
33
+ relationships.append("Both sides of the equation must remain equal")
34
+ relationships.append("You need to isolate the variable")
35
 
36
+ asks_for = "the value of the variable"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  trap_notes.extend([
39
+ "Always perform operations on both sides of the equation",
40
+ "Watch for sign errors",
41
+ "Be careful with distributing brackets",
42
  ])
43
 
44
  return ExplainerResult(
 
49
  constraints=constraints,
50
  relationships=relationships,
51
  needed_concepts=[
 
52
  "inverse operations",
53
+ "equation balancing",
54
+ "isolating variables",
55
  ],
56
  trap_notes=trap_notes,
57
+ strategy_hint="Undo operations step by step in reverse order to isolate the variable.",
58
+ plain_english="The question gives an equation and asks you to isolate the variable to find its value.",
59
  )