Update explainers/explainer_ratio.py
Browse files- explainers/explainer_ratio.py +139 -63
explainers/explainer_ratio.py
CHANGED
|
@@ -3,29 +3,57 @@ from .explainer_types import ExplainerResult, ExplainerScaffold
|
|
| 3 |
|
| 4 |
|
| 5 |
def _looks_like_ratio_question(text: str) -> bool:
|
| 6 |
-
low = (text or "").lower()
|
| 7 |
|
| 8 |
if re.search(r"\b\d+\s*:\s*\d+\b", low):
|
| 9 |
return True
|
| 10 |
-
if "for every" in low:
|
| 11 |
-
return True
|
| 12 |
-
if "ratio" in low or "proportion" in low:
|
| 13 |
-
return True
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
def _infer_ratio_subtype(text: str) -> str:
|
| 19 |
low = (text or "").lower()
|
| 20 |
|
| 21 |
-
if any(k in low for k in ["
|
| 22 |
-
return "ratio_parts"
|
| 23 |
-
if any(k in low for k in ["total", "sum", "combined"]):
|
| 24 |
-
return "part_to_total"
|
| 25 |
-
if any(k in low for k in ["proportion", "directly proportional", "inversely proportional"]):
|
| 26 |
return "proportion"
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return "group_ratio"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return "generic_ratio"
|
| 30 |
|
| 31 |
|
|
@@ -38,136 +66,183 @@ def explain_ratio_question(text: str):
|
|
| 38 |
result = ExplainerResult(
|
| 39 |
understood=True,
|
| 40 |
topic="ratio",
|
| 41 |
-
summary=
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
scaffold = ExplainerScaffold(
|
| 45 |
concept="A ratio compares quantities by relative size, not by actual amount.",
|
| 46 |
-
ask=
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
answer_hidden=True,
|
| 49 |
)
|
| 50 |
|
| 51 |
-
teaching_points = [
|
| 52 |
"A ratio does not give actual quantities until a common scale factor is applied.",
|
| 53 |
-
"Most ratio questions become
|
| 54 |
-
"The order matters. Reversing the ratio changes the meaning of the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
]
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if subtype == "ratio_parts":
|
| 58 |
scaffold.setup_actions = [
|
| 59 |
-
"Write each ratio part using a common multiplier such as ak and bk.",
|
| 60 |
-
"Keep the
|
| 61 |
-
"Use the condition in the question to connect those expressions to actual values."
|
| 62 |
]
|
| 63 |
scaffold.intermediate_steps = [
|
| 64 |
-
"If one part is known,
|
| 65 |
"If a total is given, add the ratio expressions.",
|
| 66 |
-
"If a difference is given, subtract the relevant ratio expressions."
|
| 67 |
]
|
| 68 |
scaffold.first_move = "Rewrite each ratio term as a multiple of the same variable."
|
| 69 |
scaffold.next_hint = "Then connect those expressions to the value or condition given in the question."
|
| 70 |
scaffold.variables_to_define = [
|
| 71 |
-
"Let the common multiplier be k."
|
| 72 |
]
|
| 73 |
scaffold.equations_to_form = [
|
| 74 |
-
"
|
| 75 |
]
|
| 76 |
scaffold.common_traps = [
|
| 77 |
"Reversing the order of the ratio.",
|
| 78 |
-
"Treating the ratio
|
| 79 |
-
"
|
| 80 |
]
|
| 81 |
|
| 82 |
elif subtype == "part_to_total":
|
| 83 |
scaffold.setup_actions = [
|
| 84 |
-
"Represent each part
|
| 85 |
-
"Add the ratio parts to
|
| 86 |
-
"Match the part or total expression to the
|
| 87 |
]
|
| 88 |
scaffold.intermediate_steps = [
|
| 89 |
"Translate the whole ratio into algebraic amounts first.",
|
| 90 |
-
"Use the sum of all parts
|
| 91 |
-
"Check whether the
|
| 92 |
]
|
| 93 |
-
scaffold.first_move = "Turn the ratio into variable-based
|
| 94 |
scaffold.next_hint = "Use the given total to solve for the shared multiplier."
|
| 95 |
scaffold.variables_to_define = [
|
| 96 |
-
"Let the common multiplier be k."
|
| 97 |
]
|
| 98 |
scaffold.equations_to_form = [
|
| 99 |
-
"total = sum of
|
| 100 |
]
|
| 101 |
scaffold.common_traps = [
|
| 102 |
-
"Using only one part
|
| 103 |
-
"
|
| 104 |
-
"
|
| 105 |
]
|
| 106 |
|
| 107 |
elif subtype == "proportion":
|
| 108 |
scaffold.setup_actions = [
|
| 109 |
"Identify which two ratios or rates are being set equal.",
|
| 110 |
-
"
|
| 111 |
-
"
|
| 112 |
]
|
| 113 |
scaffold.intermediate_steps = [
|
| 114 |
-
"Line up like-with-like before
|
| 115 |
-
"Check
|
| 116 |
-
"Then simplify the resulting equation."
|
| 117 |
]
|
| 118 |
scaffold.first_move = "Match the corresponding quantities in the two ratios."
|
| 119 |
-
scaffold.next_hint = "Once the matching is correct,
|
| 120 |
scaffold.equations_to_form = [
|
| 121 |
-
"first ratio = second ratio"
|
| 122 |
]
|
| 123 |
scaffold.common_traps = [
|
| 124 |
"Matching the wrong terms across the two ratios.",
|
| 125 |
-
"
|
| 126 |
-
"Ignoring whether the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
]
|
| 128 |
|
| 129 |
elif subtype == "group_ratio":
|
| 130 |
scaffold.setup_actions = [
|
| 131 |
"Assign each group its ratio-based expression.",
|
| 132 |
-
"Use the stated total, difference, or known subgroup size to
|
| 133 |
-
"Solve for the common multiplier before finding the requested quantity."
|
| 134 |
]
|
| 135 |
scaffold.intermediate_steps = [
|
| 136 |
"Make sure each category is represented exactly once.",
|
| 137 |
-
"Check whether the condition
|
| 138 |
-
"Return to the requested
|
| 139 |
]
|
| 140 |
scaffold.first_move = "Represent each group using the same scaling variable."
|
| 141 |
-
scaffold.next_hint = "Then use the condition involving the total or one
|
| 142 |
scaffold.variables_to_define = [
|
| 143 |
-
"Let the common multiplier be k."
|
|
|
|
|
|
|
|
|
|
| 144 |
]
|
| 145 |
scaffold.common_traps = [
|
| 146 |
-
"Using separate multipliers for
|
| 147 |
-
"Answering with the multiplier instead of the
|
| 148 |
-
"Losing the original ratio order when translating categories."
|
| 149 |
]
|
| 150 |
|
| 151 |
else:
|
| 152 |
scaffold.setup_actions = [
|
| 153 |
-
"Identify what each
|
| 154 |
-
"Translate the ratio into algebraic quantities
|
| 155 |
-
"Use the stated condition to solve for
|
| 156 |
]
|
| 157 |
scaffold.intermediate_steps = [
|
| 158 |
-
"Use
|
| 159 |
"Use subtraction if a difference is involved.",
|
| 160 |
-
"Check which final quantity the question wants."
|
| 161 |
]
|
| 162 |
scaffold.first_move = "Start by assigning a shared multiplier to the ratio parts."
|
| 163 |
scaffold.next_hint = "Then use the given condition to turn the ratio setup into an equation."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
scaffold.common_traps = [
|
| 165 |
"Reversing the order of the ratio.",
|
| 166 |
"Not using one shared multiplier.",
|
| 167 |
-
"Stopping at the multiplier instead of the requested quantity."
|
| 168 |
]
|
| 169 |
|
| 170 |
-
result.teaching_points = teaching_points
|
| 171 |
result.scaffold = scaffold
|
| 172 |
result.meta = {
|
| 173 |
"intent": "explain_question",
|
|
@@ -175,4 +250,5 @@ def explain_ratio_question(text: str):
|
|
| 175 |
"hint_style": "step_ready",
|
| 176 |
"subtype": subtype,
|
| 177 |
}
|
|
|
|
| 178 |
return result
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def _looks_like_ratio_question(text: str) -> bool:
|
| 6 |
+
low = (text or "").lower().strip()
|
| 7 |
|
| 8 |
if re.search(r"\b\d+\s*:\s*\d+\b", low):
|
| 9 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
ratio_signals = [
|
| 12 |
+
"ratio",
|
| 13 |
+
"proportion",
|
| 14 |
+
"for every",
|
| 15 |
+
"respectively",
|
| 16 |
+
"boys to girls",
|
| 17 |
+
"girls to boys",
|
| 18 |
+
"men to women",
|
| 19 |
+
"women to men",
|
| 20 |
+
"red to blue",
|
| 21 |
+
"blue to red",
|
| 22 |
+
"part to whole",
|
| 23 |
+
"part-to-whole",
|
| 24 |
+
"out of",
|
| 25 |
+
]
|
| 26 |
+
return any(signal in low for signal in ratio_signals)
|
| 27 |
|
| 28 |
|
| 29 |
def _infer_ratio_subtype(text: str) -> str:
|
| 30 |
low = (text or "").lower()
|
| 31 |
|
| 32 |
+
if any(k in low for k in ["directly proportional", "inversely proportional", "proportion"]):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return "proportion"
|
| 34 |
+
|
| 35 |
+
if any(k in low for k in ["total", "sum", "combined", "altogether", "in all"]):
|
| 36 |
+
return "part_to_total"
|
| 37 |
+
|
| 38 |
+
if any(
|
| 39 |
+
k in low
|
| 40 |
+
for k in [
|
| 41 |
+
"boys and girls",
|
| 42 |
+
"girls and boys",
|
| 43 |
+
"men and women",
|
| 44 |
+
"women and men",
|
| 45 |
+
"red and blue",
|
| 46 |
+
"blue and red",
|
| 47 |
+
"apples and oranges",
|
| 48 |
+
"cats and dogs",
|
| 49 |
+
"students and teachers",
|
| 50 |
+
]
|
| 51 |
+
):
|
| 52 |
return "group_ratio"
|
| 53 |
+
|
| 54 |
+
if any(k in low for k in ["for every", "ratio of", "respectively"]) or re.search(r"\b\d+\s*:\s*\d+\b", low):
|
| 55 |
+
return "ratio_parts"
|
| 56 |
+
|
| 57 |
return "generic_ratio"
|
| 58 |
|
| 59 |
|
|
|
|
| 66 |
result = ExplainerResult(
|
| 67 |
understood=True,
|
| 68 |
topic="ratio",
|
| 69 |
+
summary=(
|
| 70 |
+
"This is a ratio problem. The main job is to preserve the order of the comparison "
|
| 71 |
+
"and translate ratio parts into usable quantities with one shared scale factor."
|
| 72 |
+
),
|
| 73 |
)
|
| 74 |
|
| 75 |
scaffold = ExplainerScaffold(
|
| 76 |
concept="A ratio compares quantities by relative size, not by actual amount.",
|
| 77 |
+
ask=(
|
| 78 |
+
"Identify what each side of the ratio represents, keep the order exact, and decide "
|
| 79 |
+
"whether the question wants a part, a total, a difference, or a scaled amount."
|
| 80 |
+
),
|
| 81 |
+
target="Translate the ratio into variable-based quantities that connect to the condition in the question.",
|
| 82 |
answer_hidden=True,
|
| 83 |
)
|
| 84 |
|
| 85 |
+
result.teaching_points = [
|
| 86 |
"A ratio does not give actual quantities until a common scale factor is applied.",
|
| 87 |
+
"Most ratio questions become straightforward once each part is written using the same multiplier.",
|
| 88 |
+
"The order matters. Reversing the ratio changes the meaning of the setup.",
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
result.givens = [
|
| 92 |
+
"A comparison between two or more quantities.",
|
| 93 |
+
"An order that must be preserved exactly.",
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
result.relationships = [
|
| 97 |
+
"actual quantity = ratio part × common multiplier",
|
| 98 |
]
|
| 99 |
|
| 100 |
+
result.needed_concepts = [
|
| 101 |
+
"ratio order",
|
| 102 |
+
"shared multiplier",
|
| 103 |
+
"part-to-part versus part-to-whole structure",
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
result.trap_notes = [
|
| 107 |
+
"Reversing the order of the ratio.",
|
| 108 |
+
"Treating ratio numbers as final quantities instead of scaled parts.",
|
| 109 |
+
"Finding the multiplier but not returning to the quantity actually asked for.",
|
| 110 |
+
]
|
| 111 |
+
|
| 112 |
+
result.strategy_hint = "Start by assigning the same multiplier to every part of the ratio."
|
| 113 |
+
|
| 114 |
if subtype == "ratio_parts":
|
| 115 |
scaffold.setup_actions = [
|
| 116 |
+
"Write each ratio part using a common multiplier, such as ak and bk.",
|
| 117 |
+
"Keep the parts in the same order as the original ratio statement.",
|
| 118 |
+
"Use the condition in the question to connect those expressions to actual values.",
|
| 119 |
]
|
| 120 |
scaffold.intermediate_steps = [
|
| 121 |
+
"If one part is known, use it to find the common multiplier.",
|
| 122 |
"If a total is given, add the ratio expressions.",
|
| 123 |
+
"If a difference is given, subtract the relevant ratio expressions.",
|
| 124 |
]
|
| 125 |
scaffold.first_move = "Rewrite each ratio term as a multiple of the same variable."
|
| 126 |
scaffold.next_hint = "Then connect those expressions to the value or condition given in the question."
|
| 127 |
scaffold.variables_to_define = [
|
| 128 |
+
"Let the common multiplier be k.",
|
| 129 |
]
|
| 130 |
scaffold.equations_to_form = [
|
| 131 |
+
"amount = ratio part × k",
|
| 132 |
]
|
| 133 |
scaffold.common_traps = [
|
| 134 |
"Reversing the order of the ratio.",
|
| 135 |
+
"Treating the ratio parts as actual amounts immediately.",
|
| 136 |
+
"Using different multipliers for parts of the same ratio.",
|
| 137 |
]
|
| 138 |
|
| 139 |
elif subtype == "part_to_total":
|
| 140 |
scaffold.setup_actions = [
|
| 141 |
+
"Represent each part using the same multiplier.",
|
| 142 |
+
"Add the ratio parts to build the total.",
|
| 143 |
+
"Match the part or total expression to the stated condition.",
|
| 144 |
]
|
| 145 |
scaffold.intermediate_steps = [
|
| 146 |
"Translate the whole ratio into algebraic amounts first.",
|
| 147 |
+
"Use the sum of all parts when the question gives a total.",
|
| 148 |
+
"Check whether the final answer should be one part or the whole amount.",
|
| 149 |
]
|
| 150 |
+
scaffold.first_move = "Turn the ratio into variable-based parts and combine them to form the total."
|
| 151 |
scaffold.next_hint = "Use the given total to solve for the shared multiplier."
|
| 152 |
scaffold.variables_to_define = [
|
| 153 |
+
"Let the common multiplier be k.",
|
| 154 |
]
|
| 155 |
scaffold.equations_to_form = [
|
| 156 |
+
"total = (sum of ratio parts) × k",
|
| 157 |
]
|
| 158 |
scaffold.common_traps = [
|
| 159 |
+
"Using only one part when the condition refers to the whole total.",
|
| 160 |
+
"Leaving out one category when building the total.",
|
| 161 |
+
"Stopping after finding the multiplier instead of the requested amount.",
|
| 162 |
]
|
| 163 |
|
| 164 |
elif subtype == "proportion":
|
| 165 |
scaffold.setup_actions = [
|
| 166 |
"Identify which two ratios or rates are being set equal.",
|
| 167 |
+
"Match corresponding positions carefully.",
|
| 168 |
+
"Only form the equation once the correspondence is correct.",
|
| 169 |
]
|
| 170 |
scaffold.intermediate_steps = [
|
| 171 |
+
"Line up like-with-like before writing the proportion.",
|
| 172 |
+
"Check whether the quantities and units match properly.",
|
| 173 |
+
"Then simplify the resulting equation step by step.",
|
| 174 |
]
|
| 175 |
scaffold.first_move = "Match the corresponding quantities in the two ratios."
|
| 176 |
+
scaffold.next_hint = "Once the matching is correct, write the equality between the two ratios."
|
| 177 |
scaffold.equations_to_form = [
|
| 178 |
+
"first ratio = second ratio",
|
| 179 |
]
|
| 180 |
scaffold.common_traps = [
|
| 181 |
"Matching the wrong terms across the two ratios.",
|
| 182 |
+
"Using cross-multiplication before the setup is correct.",
|
| 183 |
+
"Ignoring whether the wording implies direct or inverse proportion.",
|
| 184 |
+
]
|
| 185 |
+
|
| 186 |
+
result.relationships = [
|
| 187 |
+
"equivalent ratios represent the same multiplicative relationship",
|
| 188 |
+
"matching positions must stay consistent across the proportion",
|
| 189 |
+
]
|
| 190 |
+
result.needed_concepts = [
|
| 191 |
+
"equivalent ratios",
|
| 192 |
+
"corresponding terms",
|
| 193 |
+
"proportional structure",
|
| 194 |
]
|
| 195 |
|
| 196 |
elif subtype == "group_ratio":
|
| 197 |
scaffold.setup_actions = [
|
| 198 |
"Assign each group its ratio-based expression.",
|
| 199 |
+
"Use the stated total, difference, or known subgroup size to build an equation.",
|
| 200 |
+
"Solve for the common multiplier before finding the requested quantity.",
|
| 201 |
]
|
| 202 |
scaffold.intermediate_steps = [
|
| 203 |
"Make sure each category is represented exactly once.",
|
| 204 |
+
"Check whether the condition refers to one group or the whole set.",
|
| 205 |
+
"Return to the requested group after finding the multiplier.",
|
| 206 |
]
|
| 207 |
scaffold.first_move = "Represent each group using the same scaling variable."
|
| 208 |
+
scaffold.next_hint = "Then use the condition involving the total or one subgroup to solve for that variable."
|
| 209 |
scaffold.variables_to_define = [
|
| 210 |
+
"Let the common multiplier be k.",
|
| 211 |
+
]
|
| 212 |
+
scaffold.equations_to_form = [
|
| 213 |
+
"group amount = ratio part × k",
|
| 214 |
]
|
| 215 |
scaffold.common_traps = [
|
| 216 |
+
"Using separate multipliers for groups in the same ratio.",
|
| 217 |
+
"Answering with the multiplier instead of the group requested.",
|
| 218 |
+
"Losing track of the original ratio order when translating categories.",
|
| 219 |
]
|
| 220 |
|
| 221 |
else:
|
| 222 |
scaffold.setup_actions = [
|
| 223 |
+
"Identify what each part of the ratio refers to.",
|
| 224 |
+
"Translate the ratio into algebraic quantities using one shared scale factor.",
|
| 225 |
+
"Use the stated condition to solve for that scale factor.",
|
| 226 |
]
|
| 227 |
scaffold.intermediate_steps = [
|
| 228 |
+
"Use addition if a total is involved.",
|
| 229 |
"Use subtraction if a difference is involved.",
|
| 230 |
+
"Check carefully which final quantity the question wants.",
|
| 231 |
]
|
| 232 |
scaffold.first_move = "Start by assigning a shared multiplier to the ratio parts."
|
| 233 |
scaffold.next_hint = "Then use the given condition to turn the ratio setup into an equation."
|
| 234 |
+
scaffold.variables_to_define = [
|
| 235 |
+
"Let the common multiplier be k.",
|
| 236 |
+
]
|
| 237 |
+
scaffold.equations_to_form = [
|
| 238 |
+
"actual quantity = ratio part × k",
|
| 239 |
+
]
|
| 240 |
scaffold.common_traps = [
|
| 241 |
"Reversing the order of the ratio.",
|
| 242 |
"Not using one shared multiplier.",
|
| 243 |
+
"Stopping at the multiplier instead of the requested quantity.",
|
| 244 |
]
|
| 245 |
|
|
|
|
| 246 |
result.scaffold = scaffold
|
| 247 |
result.meta = {
|
| 248 |
"intent": "explain_question",
|
|
|
|
| 250 |
"hint_style": "step_ready",
|
| 251 |
"subtype": subtype,
|
| 252 |
}
|
| 253 |
+
|
| 254 |
return result
|