Update explainers/explainer_percent.py
Browse files- explainers/explainer_percent.py +81 -48
explainers/explainer_percent.py
CHANGED
|
@@ -1,49 +1,82 @@
|
|
| 1 |
-
# explainer_percent
|
| 2 |
-
|
| 3 |
-
import re
|
| 4 |
-
from models import ExplainerResult
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
relationships
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
|
|
|
| 1 |
+
# explainer_percent.py
|
| 2 |
+
|
| 3 |
+
import re
|
| 4 |
+
from models import ExplainerResult
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def explain_percent_question(text: str) -> ExplainerResult | None:
|
| 8 |
+
t = text.lower()
|
| 9 |
+
|
| 10 |
+
has_percent = "%" in t or "percent" in t or "percentage" in t
|
| 11 |
+
if not has_percent:
|
| 12 |
+
return None
|
| 13 |
+
|
| 14 |
+
givens = []
|
| 15 |
+
relationships = []
|
| 16 |
+
constraints = []
|
| 17 |
+
trap_notes = []
|
| 18 |
+
|
| 19 |
+
percent_matches = re.findall(r"\b\d+(?:\.\d+)?%", t)
|
| 20 |
+
number_matches = re.findall(r"\b\d+(?:\.\d+)?\b", t)
|
| 21 |
+
|
| 22 |
+
if percent_matches:
|
| 23 |
+
givens.append(f"A percentage is given: {', '.join(percent_matches[:3])}")
|
| 24 |
+
else:
|
| 25 |
+
givens.append("A percent relationship is given")
|
| 26 |
+
|
| 27 |
+
if "of" in t:
|
| 28 |
+
givens.append("The wording uses an 'of' relationship")
|
| 29 |
+
|
| 30 |
+
if number_matches:
|
| 31 |
+
givens.append(f"Numbers mentioned: {', '.join(number_matches[:5])}")
|
| 32 |
+
|
| 33 |
+
relationships.append("This is a part-percent-whole relationship")
|
| 34 |
+
|
| 35 |
+
if "of" in t:
|
| 36 |
+
relationships.append("One quantity is being described as a percent of another quantity")
|
| 37 |
+
|
| 38 |
+
if "increase" in t:
|
| 39 |
+
relationships.append("The question may involve a percent increase from an original value")
|
| 40 |
+
|
| 41 |
+
if "decrease" in t:
|
| 42 |
+
relationships.append("The question may involve a percent decrease from an original value")
|
| 43 |
+
|
| 44 |
+
if "original" in t or "whole" in t or "base" in t:
|
| 45 |
+
constraints.append("Pay attention to which value is the original whole or base")
|
| 46 |
+
|
| 47 |
+
if "more than" in t or "less than" in t:
|
| 48 |
+
constraints.append("Watch the comparison wording carefully")
|
| 49 |
+
|
| 50 |
+
asks_for = None
|
| 51 |
+
if "original" in t or "whole" in t:
|
| 52 |
+
asks_for = "the original whole value"
|
| 53 |
+
elif "what percent" in t or "what percentage" in t:
|
| 54 |
+
asks_for = "the missing percent"
|
| 55 |
+
elif "what is" in t or "find" in t or "how much" in t:
|
| 56 |
+
asks_for = "the missing value in the percent relationship"
|
| 57 |
+
|
| 58 |
+
trap_notes.extend([
|
| 59 |
+
"Do not confuse the part with the whole",
|
| 60 |
+
"Check whether you are solving for the percent, the part, or the whole",
|
| 61 |
+
"Make sure the percent is attached to the correct base value",
|
| 62 |
+
])
|
| 63 |
+
|
| 64 |
+
if "increase" in t or "decrease" in t:
|
| 65 |
+
trap_notes.append("Do not mix up the original amount and the changed amount")
|
| 66 |
+
|
| 67 |
+
return ExplainerResult(
|
| 68 |
+
understood=True,
|
| 69 |
+
topic="percent",
|
| 70 |
+
asks_for=asks_for,
|
| 71 |
+
givens=givens,
|
| 72 |
+
constraints=constraints,
|
| 73 |
+
relationships=relationships,
|
| 74 |
+
needed_concepts=[
|
| 75 |
+
"percent equation",
|
| 76 |
+
"part-whole thinking",
|
| 77 |
+
"identifying the base value",
|
| 78 |
+
],
|
| 79 |
+
trap_notes=trap_notes,
|
| 80 |
+
strategy_hint="First decide which number is the whole, which is the part, and whether the question is asking you to work forward or backward.",
|
| 81 |
+
plain_english="The question is describing a percent relationship and asking you to identify the missing value in that setup.",
|
| 82 |
)
|