Download explainers/explainer_algebra.py from j-js/GameAI: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/j-js/GameAI/resolve/4afc5be64481ffbcd209feeb6eddcfafd3ac54fc/explainers/explainer_algebra.py
- Command line
-
hf download hf://spaces/j-js/GameAI@4afc5be64481ffbcd209feeb6eddcfafd3ac54fc/explainers/explainer_algebra.py
-
curl -L -o explainer_algebra.py https://huggingface.co/spaces/j-js/GameAI/resolve/4afc5be64481ffbcd209feeb6eddcfafd3ac54fc/explainers/explainer_algebra.py
2.8 kB
| # explainer_algebra.py | |
| import re | |
| from models import ExplainerResult | |
| def explain_algebra_question(text: str) -> ExplainerResult | None: | |
| t = text.lower() | |
| has_equation = "=" in t | |
| has_variable = bool(re.search(r"\b[a-z]\b", t)) | |
| has_solve_word = "solve" in t or "value of x" in t or "value of y" in t | |
| if not has_equation and not has_variable and not has_solve_word: | |
| return None | |
| # avoid catching obvious non-algebra topics first | |
| if "%" in t or "percent" in t or "ratio" in t or re.search(r"\b\d+\s*:\s*\d+\b", t): | |
| return None | |
| givens = [] | |
| relationships = [] | |
| constraints = [] | |
| trap_notes = [] | |
| if has_equation: | |
| givens.append("An equation is given") | |
| variables = sorted(set(re.findall(r"\b[a-z]\b", t))) | |
| variables = [v for v in variables if v not in {"a", "i"}] | |
| if variables: | |
| givens.append(f"A variable appears in the question: {', '.join(variables[:3])}") | |
| numbers = re.findall(r"\b\d+(?:\.\d+)?\b", t) | |
| if numbers: | |
| givens.append(f"Numbers are included in the expression: {', '.join(numbers[:5])}") | |
| relationships.append("Both sides of the equation represent the same value") | |
| relationships.append("You need to isolate the variable while keeping the equation balanced") | |
| if "integer" in t: | |
| constraints.append("The variable may be restricted to integer values") | |
| if "positive" in t: | |
| constraints.append("The variable may need to be positive") | |
| if "negative" in t: | |
| constraints.append("The variable may be negative or restricted by sign") | |
| asks_for = None | |
| m = re.search(r"\bsolve for ([a-z])\b", t) | |
| if m: | |
| asks_for = f"the value of {m.group(1)}" | |
| else: | |
| m = re.search(r"\bwhat is the value of ([a-z])\b", t) | |
| if m: | |
| asks_for = f"the value of {m.group(1)}" | |
| elif "what is" in t or "find" in t: | |
| asks_for = "the value of the variable" | |
| trap_notes.extend([ | |
| "Do not perform an operation on only one side of the equation", | |
| "Watch for sign mistakes when moving terms", | |
| "Be careful with brackets, distribution, and inverse operations", | |
| ]) | |
| return ExplainerResult( | |
| understood=True, | |
| topic="algebra", | |
| asks_for=asks_for, | |
| givens=givens, | |
| constraints=constraints, | |
| relationships=relationships, | |
| needed_concepts=[ | |
| "equation balancing", | |
| "inverse operations", | |
| "isolating the variable", | |
| ], | |
| trap_notes=trap_notes, | |
| strategy_hint="Identify the variable, then think about what operations need to be undone to isolate it.", | |
| plain_english="The question is giving you an algebraic relationship and asking you to isolate the variable to find its value.", | |
| ) |