Update explainers/explainer_algebra.py
Browse files- 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
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
return None
|
| 16 |
|
| 17 |
-
|
| 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
|
| 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
|
| 40 |
-
relationships.append("You need to isolate the variable
|
| 41 |
|
| 42 |
-
|
| 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 |
-
"
|
| 62 |
-
"Watch for sign
|
| 63 |
-
"Be careful with
|
| 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 |
-
"
|
|
|
|
| 77 |
],
|
| 78 |
trap_notes=trap_notes,
|
| 79 |
-
strategy_hint="
|
| 80 |
-
plain_english="The question
|
| 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 |
)
|