Create explainer_probability.py
Browse files
explainers/explainer_probability.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from models import ExplainerResult
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def explain_probability_question(text: str) -> ExplainerResult | None:
|
| 6 |
+
t = text.lower()
|
| 7 |
+
|
| 8 |
+
# ✅ STRONG detection
|
| 9 |
+
probability_signals = [
|
| 10 |
+
"probability",
|
| 11 |
+
"chance",
|
| 12 |
+
"odds",
|
| 13 |
+
"random",
|
| 14 |
+
"chosen at random",
|
| 15 |
+
"selected at random",
|
| 16 |
+
"picked at random",
|
| 17 |
+
"drawn",
|
| 18 |
+
"without replacement",
|
| 19 |
+
"with replacement",
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
has_probability = any(p in t for p in probability_signals)
|
| 23 |
+
|
| 24 |
+
if not has_probability:
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
givens = []
|
| 28 |
+
relationships = []
|
| 29 |
+
constraints = []
|
| 30 |
+
trap_notes = []
|
| 31 |
+
|
| 32 |
+
numbers = re.findall(r"\b\d+\b", t)
|
| 33 |
+
|
| 34 |
+
if numbers:
|
| 35 |
+
givens.append(f"Numbers mentioned: {', '.join(numbers[:5])}")
|
| 36 |
+
else:
|
| 37 |
+
givens.append("A situation involving possible outcomes is described")
|
| 38 |
+
|
| 39 |
+
if "without replacement" in t:
|
| 40 |
+
relationships.append("The probabilities change after each selection")
|
| 41 |
+
constraints.append("The total number of items decreases after each draw")
|
| 42 |
+
|
| 43 |
+
if "with replacement" in t:
|
| 44 |
+
relationships.append("The probabilities stay the same for each selection")
|
| 45 |
+
|
| 46 |
+
if "at least" in t:
|
| 47 |
+
relationships.append("This may require using the complement (1 - probability of the opposite event)")
|
| 48 |
+
|
| 49 |
+
if "both" in t or "and" in t:
|
| 50 |
+
relationships.append("You may need to combine probabilities of multiple events occurring together")
|
| 51 |
+
|
| 52 |
+
if "or" in t:
|
| 53 |
+
relationships.append("You may need to consider multiple possible favorable cases")
|
| 54 |
+
|
| 55 |
+
# default structure
|
| 56 |
+
relationships.append("Probability = favorable outcomes ÷ total possible outcomes")
|
| 57 |
+
|
| 58 |
+
asks_for = "the probability of a specific event occurring"
|
| 59 |
+
|
| 60 |
+
trap_notes.extend([
|
| 61 |
+
"Make sure you correctly identify the total number of possible outcomes",
|
| 62 |
+
"Check whether events are independent or dependent",
|
| 63 |
+
"Watch for 'at least' — often easier to use the complement",
|
| 64 |
+
"Be careful not to double count outcomes",
|
| 65 |
+
])
|
| 66 |
+
|
| 67 |
+
return ExplainerResult(
|
| 68 |
+
understood=True,
|
| 69 |
+
topic="probability",
|
| 70 |
+
asks_for=asks_for,
|
| 71 |
+
givens=givens,
|
| 72 |
+
constraints=constraints,
|
| 73 |
+
relationships=relationships,
|
| 74 |
+
needed_concepts=[
|
| 75 |
+
"favorable vs total outcomes",
|
| 76 |
+
"independent vs dependent events",
|
| 77 |
+
"complement rule",
|
| 78 |
+
],
|
| 79 |
+
trap_notes=trap_notes,
|
| 80 |
+
strategy_hint="Start by identifying all possible outcomes, then count how many satisfy the condition.",
|
| 81 |
+
plain_english="The question is asking you to work out how likely an event is by comparing favorable outcomes to all possible outcomes.",
|
| 82 |
+
)
|