Create explainer_algebra.py
Browse files
explainers/explainer_algebra.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# explainer_algebra.py
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from models import ExplainerResult
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def explain_algebra_question(text: str) -> ExplainerResult | None:
|
| 8 |
+
t = text.lower()
|
| 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 = []
|
| 22 |
+
relationships = []
|
| 23 |
+
constraints = []
|
| 24 |
+
trap_notes = []
|
| 25 |
+
|
| 26 |
+
if has_equation:
|
| 27 |
+
givens.append("An equation is given")
|
| 28 |
+
|
| 29 |
+
variables = sorted(set(re.findall(r"\b[a-z]\b", t)))
|
| 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(
|
| 67 |
+
understood=True,
|
| 68 |
+
topic="algebra",
|
| 69 |
+
asks_for=asks_for,
|
| 70 |
+
givens=givens,
|
| 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 |
+
)
|