| from __future__ import annotations |
|
|
| import re |
| from typing import Any, Dict, List, Optional, Tuple |
|
|
|
|
| def split_unity_message(text: str) -> Tuple[str, str]: |
| """ |
| Splits a Unity-style message into: |
| - hidden/system/game context prefix |
| - actual user-facing message |
| |
| If no obvious split is found, returns ("", original_text). |
| """ |
| raw = (text or "").strip() |
| if not raw: |
| return "", "" |
|
|
| |
| |
| |
| m = re.search(r"(?is)^(.*?)(?:\buser\b|\bprompt\b|\bmessage\b)\s*:\s*(.+)$", raw) |
| if m: |
| hidden = (m.group(1) or "").strip() |
| user = (m.group(2) or "").strip() |
| return hidden, user |
|
|
| return "", raw |
|
|
|
|
| def _extract_options(text: str) -> List[str]: |
| if not text: |
| return [] |
|
|
| lines = [line.strip() for line in text.splitlines() if line.strip()] |
| options: List[str] = [] |
|
|
| for line in lines: |
| if re.match(r"^[A-E][\)\.\:]\s*", line, flags=re.I): |
| options.append(re.sub(r"^[A-E][\)\.\:]\s*", "", line, flags=re.I).strip()) |
|
|
| if options: |
| return options |
|
|
| |
| matches = re.findall(r"(?:^|\s)([A-E])[\)\.\:]\s*(.*?)(?=(?:\s+[A-E][\)\.\:])|$)", text, flags=re.I | re.S) |
| if matches: |
| return [m[1].strip() for m in matches if m[1].strip()] |
|
|
| return [] |
|
|
|
|
| def extract_game_context_fields(text: str) -> Dict[str, Any]: |
| """ |
| Extracts lightweight structured fields from hidden Unity/game context. |
| Always returns stable keys expected by app.py. |
| """ |
| raw = (text or "").strip() |
|
|
| result: Dict[str, Any] = { |
| "question": "", |
| "options": [], |
| "difficulty": None, |
| "category": None, |
| "money": None, |
| "has_choices": False, |
| "looks_like_quant": False, |
| } |
|
|
| if not raw: |
| return result |
|
|
| |
| q_match = re.search(r"\bquestion\s*[:=]\s*(.+?)(?=\n[A-Za-z_ ]+\s*[:=]|\Z)", raw, flags=re.I | re.S) |
| if q_match: |
| result["question"] = q_match.group(1).strip() |
|
|
| |
| opt_match = re.search(r"\b(?:options|choices|answers)\s*[:=]\s*(.+?)(?=\n[A-Za-z_ ]+\s*[:=]|\Z)", raw, flags=re.I | re.S) |
| if opt_match: |
| result["options"] = _extract_options(opt_match.group(1)) |
|
|
| |
| if not result["options"]: |
| result["options"] = _extract_options(raw) |
|
|
| result["has_choices"] = len(result["options"]) > 0 |
|
|
| |
| difficulty_match = re.search(r"\bdifficulty\s*[:=]\s*([A-Za-z0-9_\- ]+)", raw, flags=re.I) |
| if difficulty_match: |
| result["difficulty"] = difficulty_match.group(1).strip() |
|
|
| |
| category_match = re.search(r"\b(?:category|topic)\s*[:=]\s*([A-Za-z0-9_\- /]+)", raw, flags=re.I) |
| if category_match: |
| result["category"] = category_match.group(1).strip() |
|
|
| |
| money_match = re.search(r"\b(?:money|balance|bank)\s*[:=]\s*([\-]?\d+(?:\.\d+)?)", raw, flags=re.I) |
| if money_match: |
| try: |
| result["money"] = float(money_match.group(1)) |
| except Exception: |
| pass |
|
|
| lower = raw.lower() |
| result["looks_like_quant"] = any( |
| token in lower |
| for token in [ |
| "solve", |
| "equation", |
| "percent", |
| "%", |
| "ratio", |
| "probability", |
| "mean", |
| "median", |
| "algebra", |
| "integer", |
| "triangle", |
| "circle", |
| ] |
| ) |
|
|
| return result |
|
|
|
|
| def detect_intent(text: str, incoming_help_mode: Optional[str] = None) -> str: |
| """ |
| Returns one of: |
| answer, hint, instruction, walkthrough, explain, method, definition, concept |
| """ |
| forced = (incoming_help_mode or "").strip().lower() |
| if forced in { |
| "answer", |
| "hint", |
| "instruction", |
| "walkthrough", |
| "step_by_step", |
| "explain", |
| "method", |
| "definition", |
| "concept", |
| }: |
| return forced |
|
|
| t = (text or "").strip().lower() |
|
|
| if not t: |
| return "answer" |
|
|
| if ( |
| re.search(r"\bdefine\b", t) |
| or re.search(r"\bdefinition\b", t) |
| or re.search(r"\bwhat does\b", t) |
| or re.search(r"\bwhat is meant by\b", t) |
| ): |
| return "definition" |
|
|
| if re.search(r"\bhint\b", t) or re.search(r"\bclue\b", t) or re.search(r"\bnudge\b", t): |
| return "hint" |
|
|
| if ( |
| re.search(r"\bfirst step\b", t) |
| or re.search(r"\bnext step\b", t) |
| or re.search(r"\bwhat should i do first\b", t) |
| or re.search(r"\bgive me the first step\b", t) |
| or re.search(r"\bspecific step\b", t) |
| ): |
| return "instruction" |
|
|
| if ( |
| re.search(r"\bwalk ?through\b", t) |
| or re.search(r"\bstep by step\b", t) |
| or re.search(r"\bfull working\b", t) |
| or re.search(r"\bwork through\b", t) |
| ): |
| return "walkthrough" |
|
|
| if re.search(r"\bexplain\b", t) or re.search(r"\bwhy\b", t): |
| return "explain" |
|
|
| if ( |
| re.search(r"\bmethod\b", t) |
| or re.search(r"\bapproach\b", t) |
| or re.search(r"\bhow do i solve\b", t) |
| or re.search(r"\bhow to solve\b", t) |
| or re.search(r"\bhow do i do this\b", t) |
| ): |
| return "method" |
|
|
| if ( |
| re.search(r"\bconcept\b", t) |
| or re.search(r"\bprinciple\b", t) |
| or re.search(r"\brule\b", t) |
| or re.search(r"\bwhat is the idea\b", t) |
| ): |
| return "concept" |
|
|
| if ( |
| re.search(r"\bsolve\b", t) |
| or re.search(r"\bwhat is\b", t) |
| or re.search(r"\bfind\b", t) |
| or re.search(r"\bgive (?:me )?the answer\b", t) |
| or re.search(r"\bjust the answer\b", t) |
| or re.search(r"\banswer only\b", t) |
| or re.search(r"\bcalculate\b", t) |
| ): |
| return "answer" |
|
|
| return "answer" |
|
|
|
|
| def intent_to_help_mode(intent: str) -> str: |
| if intent in {"walkthrough", "step_by_step", "explain", "method", "concept"}: |
| return "walkthrough" |
| if intent == "hint": |
| return "hint" |
| if intent in {"definition", "instruction"}: |
| return intent |
| return "answer" |