Update explainers/explainer_ratio.py
Browse files- explainers/explainer_ratio.py +120 -112
explainers/explainer_ratio.py
CHANGED
|
@@ -1,183 +1,191 @@
|
|
| 1 |
import re
|
| 2 |
-
from explainer_types import ExplainerResult, ExplainerScaffold
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
r"
|
| 7 |
-
r"\
|
| 8 |
-
r"\
|
| 9 |
-
r"\
|
| 10 |
-
r"
|
| 11 |
-
r"\
|
| 12 |
-
r"\
|
| 13 |
-
r"\
|
| 14 |
-
r"\bpart to whole\b",
|
| 15 |
]
|
| 16 |
|
| 17 |
|
| 18 |
-
def
|
| 19 |
low = (text or "").lower()
|
| 20 |
|
| 21 |
-
if re.search(r"\b\
|
| 22 |
return True
|
| 23 |
-
if
|
| 24 |
return True
|
| 25 |
-
if "ratio" in low or "proportion" in low:
|
| 26 |
-
return True
|
| 27 |
-
|
| 28 |
return False
|
| 29 |
|
| 30 |
|
| 31 |
-
def
|
| 32 |
low = (text or "").lower()
|
| 33 |
|
| 34 |
-
if any(k in low for k in ["
|
| 35 |
-
return "
|
| 36 |
-
if any(k in low for k in ["
|
| 37 |
-
return "
|
| 38 |
-
if any(k in low for k in ["
|
| 39 |
-
return "
|
| 40 |
-
if any(k in low for k in ["
|
| 41 |
-
return "
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
return None
|
| 48 |
|
| 49 |
-
subtype =
|
| 50 |
|
| 51 |
result = ExplainerResult(
|
| 52 |
understood=True,
|
| 53 |
-
topic="
|
| 54 |
-
summary="This is
|
| 55 |
)
|
| 56 |
|
| 57 |
scaffold = ExplainerScaffold(
|
| 58 |
-
concept="
|
| 59 |
-
ask="
|
| 60 |
-
target="
|
| 61 |
answer_hidden=True,
|
| 62 |
)
|
| 63 |
|
| 64 |
teaching_points = [
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
]
|
| 69 |
|
| 70 |
-
if subtype == "
|
| 71 |
scaffold.setup_actions = [
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
]
|
| 76 |
scaffold.intermediate_steps = [
|
| 77 |
-
"
|
| 78 |
-
"
|
| 79 |
-
"
|
| 80 |
]
|
| 81 |
-
scaffold.first_move = "Rewrite
|
| 82 |
-
scaffold.next_hint = "
|
| 83 |
scaffold.variables_to_define = [
|
| 84 |
-
"Let the
|
| 85 |
]
|
| 86 |
scaffold.equations_to_form = [
|
| 87 |
-
"
|
| 88 |
]
|
| 89 |
scaffold.common_traps = [
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
]
|
| 94 |
|
| 95 |
-
elif subtype == "
|
| 96 |
scaffold.setup_actions = [
|
| 97 |
-
"
|
| 98 |
-
"
|
| 99 |
-
"
|
| 100 |
]
|
| 101 |
scaffold.intermediate_steps = [
|
| 102 |
-
"
|
| 103 |
-
"
|
| 104 |
-
"Check whether the question asks for one
|
| 105 |
-
]
|
| 106 |
-
scaffold.first_move = "Turn the ratio into variable-based amounts and add them to get the total structure."
|
| 107 |
-
scaffold.next_hint = "Use the given total to solve for the shared multiplier."
|
| 108 |
-
scaffold.variables_to_define = [
|
| 109 |
-
"Let the common multiplier be k."
|
| 110 |
]
|
|
|
|
|
|
|
| 111 |
scaffold.equations_to_form = [
|
| 112 |
-
"
|
| 113 |
]
|
| 114 |
scaffold.common_traps = [
|
| 115 |
-
"
|
| 116 |
-
"
|
| 117 |
-
"
|
| 118 |
]
|
| 119 |
|
| 120 |
-
elif subtype == "
|
| 121 |
scaffold.setup_actions = [
|
| 122 |
-
"
|
| 123 |
-
"
|
| 124 |
-
"
|
| 125 |
]
|
| 126 |
scaffold.intermediate_steps = [
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
-
]
|
| 131 |
-
scaffold.first_move = "Match the corresponding quantities in the two ratios."
|
| 132 |
-
scaffold.next_hint = "Once the matching is correct, form the equation between the two ratios."
|
| 133 |
-
scaffold.equations_to_form = [
|
| 134 |
-
"first ratio = second ratio"
|
| 135 |
]
|
|
|
|
|
|
|
| 136 |
scaffold.common_traps = [
|
| 137 |
-
"
|
| 138 |
-
"
|
| 139 |
-
"
|
| 140 |
]
|
| 141 |
|
| 142 |
-
elif subtype == "
|
| 143 |
scaffold.setup_actions = [
|
| 144 |
-
"
|
| 145 |
-
"
|
| 146 |
-
"
|
| 147 |
]
|
| 148 |
scaffold.intermediate_steps = [
|
| 149 |
-
"
|
| 150 |
-
"
|
| 151 |
-
"
|
| 152 |
]
|
| 153 |
-
scaffold.first_move = "
|
| 154 |
-
scaffold.next_hint = "Then
|
| 155 |
-
scaffold.
|
| 156 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
]
|
|
|
|
|
|
|
| 158 |
scaffold.common_traps = [
|
| 159 |
-
"
|
| 160 |
-
"
|
| 161 |
-
"
|
| 162 |
]
|
| 163 |
|
| 164 |
else:
|
| 165 |
scaffold.setup_actions = [
|
| 166 |
-
"
|
| 167 |
-
"Translate the
|
| 168 |
-
"
|
| 169 |
]
|
| 170 |
scaffold.intermediate_steps = [
|
| 171 |
-
"
|
| 172 |
-
"
|
| 173 |
-
"Check
|
| 174 |
]
|
| 175 |
-
scaffold.first_move = "Start by
|
| 176 |
-
scaffold.next_hint = "Then
|
| 177 |
scaffold.common_traps = [
|
| 178 |
-
"
|
| 179 |
-
"
|
| 180 |
-
"
|
| 181 |
]
|
| 182 |
|
| 183 |
result.teaching_points = teaching_points
|
|
|
|
| 1 |
import re
|
| 2 |
+
from .explainer_types import ExplainerResult, ExplainerScaffold
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
_ALGEBRA_PATTERNS = [
|
| 6 |
+
r"=",
|
| 7 |
+
r"\bsolve\b",
|
| 8 |
+
r"\bequation\b",
|
| 9 |
+
r"\bexpression\b",
|
| 10 |
+
r"\bvalue of\b",
|
| 11 |
+
r"\bwhat is x\b",
|
| 12 |
+
r"\bwhat is y\b",
|
| 13 |
+
r"\bvariable\b",
|
|
|
|
| 14 |
]
|
| 15 |
|
| 16 |
|
| 17 |
+
def _looks_like_algebra_question(text: str) -> bool:
|
| 18 |
low = (text or "").lower()
|
| 19 |
|
| 20 |
+
if re.search(r"\b[xyzab]\b", low) and "=" in low:
|
| 21 |
return True
|
| 22 |
+
if any(re.search(p, low) for p in _ALGEBRA_PATTERNS):
|
| 23 |
return True
|
|
|
|
|
|
|
|
|
|
| 24 |
return False
|
| 25 |
|
| 26 |
|
| 27 |
+
def _infer_algebra_subtype(text: str) -> str:
|
| 28 |
low = (text or "").lower()
|
| 29 |
|
| 30 |
+
if any(k in low for k in ["system", "simultaneous", "x and y", "two equations"]):
|
| 31 |
+
return "system"
|
| 32 |
+
if any(k in low for k in ["inequality", "<", ">", "at least", "at most", "no more than"]):
|
| 33 |
+
return "inequality"
|
| 34 |
+
if any(k in low for k in ["quadratic", "squared", "^2", "x2", "root", "factor"]):
|
| 35 |
+
return "quadratic"
|
| 36 |
+
if any(k in low for k in ["expression", "value of 2x", "value of x +", "in terms of"]):
|
| 37 |
+
return "expression_evaluation"
|
| 38 |
+
if "=" in low:
|
| 39 |
+
return "linear_equation"
|
| 40 |
+
return "generic_algebra"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def explain_algebra_question(text: str):
|
| 44 |
+
if not _looks_like_algebra_question(text):
|
| 45 |
return None
|
| 46 |
|
| 47 |
+
subtype = _infer_algebra_subtype(text)
|
| 48 |
|
| 49 |
result = ExplainerResult(
|
| 50 |
understood=True,
|
| 51 |
+
topic="algebra",
|
| 52 |
+
summary="This is an algebra problem. The main goal is to translate the wording into a clean symbolic relationship and isolate the requested quantity step by step."
|
| 53 |
)
|
| 54 |
|
| 55 |
scaffold = ExplainerScaffold(
|
| 56 |
+
concept="Algebra represents unknown quantities symbolically, then uses valid transformations to isolate or compare them.",
|
| 57 |
+
ask="Identify the unknown, identify the governing relationship, and check whether the question wants the variable itself or an expression built from it.",
|
| 58 |
+
target="Set up the simplest correct equation or relation before manipulating it.",
|
| 59 |
answer_hidden=True,
|
| 60 |
)
|
| 61 |
|
| 62 |
teaching_points = [
|
| 63 |
+
"Most algebra errors happen before the solving starts: either the variable is misdefined, or the equation is set up incorrectly.",
|
| 64 |
+
"A clean equation makes the solving steps much easier.",
|
| 65 |
+
"You should always check whether the question asks for x itself or for something derived from x."
|
| 66 |
]
|
| 67 |
|
| 68 |
+
if subtype == "linear_equation":
|
| 69 |
scaffold.setup_actions = [
|
| 70 |
+
"Identify the unknown and write the equation cleanly.",
|
| 71 |
+
"Simplify each side if needed.",
|
| 72 |
+
"Undo operations in a logical order to isolate the variable."
|
| 73 |
]
|
| 74 |
scaffold.intermediate_steps = [
|
| 75 |
+
"Combine like terms first when possible.",
|
| 76 |
+
"Move variable terms and constant terms carefully.",
|
| 77 |
+
"Check whether the final result should be the variable or a substituted expression."
|
| 78 |
]
|
| 79 |
+
scaffold.first_move = "Rewrite the relationship as one clean equation if it is not already in that form."
|
| 80 |
+
scaffold.next_hint = "Simplify both sides before isolating the variable."
|
| 81 |
scaffold.variables_to_define = [
|
| 82 |
+
"Let the unknown quantity be x if the question has not already named it."
|
| 83 |
]
|
| 84 |
scaffold.equations_to_form = [
|
| 85 |
+
"Build one equation from the stated relationship."
|
| 86 |
]
|
| 87 |
scaffold.common_traps = [
|
| 88 |
+
"Moving terms across the equals sign incorrectly.",
|
| 89 |
+
"Trying to isolate the variable before simplifying.",
|
| 90 |
+
"Finding x and forgetting the question asks for something like 2x or x + 3."
|
| 91 |
]
|
| 92 |
|
| 93 |
+
elif subtype == "system":
|
| 94 |
scaffold.setup_actions = [
|
| 95 |
+
"Identify the separate equations and unknowns.",
|
| 96 |
+
"Decide whether substitution or elimination is the cleaner method.",
|
| 97 |
+
"Reduce the system to one variable before solving completely."
|
| 98 |
]
|
| 99 |
scaffold.intermediate_steps = [
|
| 100 |
+
"Make one variable easy to substitute, or align coefficients for elimination.",
|
| 101 |
+
"After finding one variable, substitute back carefully.",
|
| 102 |
+
"Check whether the question asks for one variable, both variables, or a combination of them."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
]
|
| 104 |
+
scaffold.first_move = "Choose one variable to eliminate or substitute."
|
| 105 |
+
scaffold.next_hint = "Turn the system into a single-variable equation before solving."
|
| 106 |
scaffold.equations_to_form = [
|
| 107 |
+
"Use the two given equations together to reduce to one unknown."
|
| 108 |
]
|
| 109 |
scaffold.common_traps = [
|
| 110 |
+
"Mixing substitution and elimination without a clear plan.",
|
| 111 |
+
"Arithmetic mistakes when substituting back.",
|
| 112 |
+
"Stopping after finding one variable when the question asks for something else."
|
| 113 |
]
|
| 114 |
|
| 115 |
+
elif subtype == "inequality":
|
| 116 |
scaffold.setup_actions = [
|
| 117 |
+
"Translate the condition into an inequality.",
|
| 118 |
+
"Manipulate it like an equation, but track the inequality direction carefully.",
|
| 119 |
+
"Reverse the sign only if multiplying or dividing by a negative number."
|
| 120 |
]
|
| 121 |
scaffold.intermediate_steps = [
|
| 122 |
+
"Simplify both sides first if possible.",
|
| 123 |
+
"Isolate the variable systematically.",
|
| 124 |
+
"Interpret the final solution set in the form the question wants."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
]
|
| 126 |
+
scaffold.first_move = "Set up the inequality carefully from the wording."
|
| 127 |
+
scaffold.next_hint = "Solve it step by step, watching for any operation that would reverse the sign."
|
| 128 |
scaffold.common_traps = [
|
| 129 |
+
"Forgetting to reverse the inequality when dividing or multiplying by a negative.",
|
| 130 |
+
"Treating phrase-based conditions like at least or no more than incorrectly.",
|
| 131 |
+
"Reporting a single number when the solution is actually a range."
|
| 132 |
]
|
| 133 |
|
| 134 |
+
elif subtype == "quadratic":
|
| 135 |
scaffold.setup_actions = [
|
| 136 |
+
"Rewrite the equation so one side is zero if needed.",
|
| 137 |
+
"Look for factoring, structure, or another simplifying method.",
|
| 138 |
+
"Treat each factor or case carefully once the equation is structured properly."
|
| 139 |
]
|
| 140 |
scaffold.intermediate_steps = [
|
| 141 |
+
"Factor if the form allows it.",
|
| 142 |
+
"Otherwise identify another clean solving route.",
|
| 143 |
+
"Check whether all resulting values are allowed in the original context."
|
| 144 |
]
|
| 145 |
+
scaffold.first_move = "Put the expression into a standard structured form before solving."
|
| 146 |
+
scaffold.next_hint = "Then look for a factorable pattern or another clean route."
|
| 147 |
+
scaffold.common_traps = [
|
| 148 |
+
"Trying to factor before the expression is fully simplified.",
|
| 149 |
+
"Dropping one valid case.",
|
| 150 |
+
"Giving roots when the question asks for a derived expression instead."
|
| 151 |
+
]
|
| 152 |
+
|
| 153 |
+
elif subtype == "expression_evaluation":
|
| 154 |
+
scaffold.setup_actions = [
|
| 155 |
+
"Find the variable or relationship first.",
|
| 156 |
+
"Only then substitute into the requested expression.",
|
| 157 |
+
"Simplify the final expression carefully."
|
| 158 |
+
]
|
| 159 |
+
scaffold.intermediate_steps = [
|
| 160 |
+
"Do not stop when you find the variable unless that is exactly what the question asks.",
|
| 161 |
+
"Preserve parentheses during substitution.",
|
| 162 |
+
"Check whether there is a shortcut using the given relationship directly."
|
| 163 |
]
|
| 164 |
+
scaffold.first_move = "Work out whether you need to solve for the variable first or can rewrite the target expression directly."
|
| 165 |
+
scaffold.next_hint = "Once the relationship is clear, substitute only into the exact expression the question asks for."
|
| 166 |
scaffold.common_traps = [
|
| 167 |
+
"Stopping at x when the question asks for something built from x.",
|
| 168 |
+
"Substituting incorrectly into expressions with multiple terms.",
|
| 169 |
+
"Ignoring an easier algebraic simplification path."
|
| 170 |
]
|
| 171 |
|
| 172 |
else:
|
| 173 |
scaffold.setup_actions = [
|
| 174 |
+
"Define the unknown clearly.",
|
| 175 |
+
"Translate the wording into a symbolic relationship.",
|
| 176 |
+
"Manipulate the relationship only after the setup is clean."
|
| 177 |
]
|
| 178 |
scaffold.intermediate_steps = [
|
| 179 |
+
"Simplify before isolating.",
|
| 180 |
+
"Keep track of what the question actually asks for.",
|
| 181 |
+
"Check the final quantity against the prompt."
|
| 182 |
]
|
| 183 |
+
scaffold.first_move = "Start by translating the words into one clean symbolic statement."
|
| 184 |
+
scaffold.next_hint = "Then simplify the structure before solving."
|
| 185 |
scaffold.common_traps = [
|
| 186 |
+
"Poor variable definition.",
|
| 187 |
+
"Messy setup before solving.",
|
| 188 |
+
"Answering the wrong final quantity."
|
| 189 |
]
|
| 190 |
|
| 191 |
result.teaching_points = teaching_points
|