Update context_parser.py
Browse files- context_parser.py +62 -45
context_parser.py
CHANGED
|
@@ -1,65 +1,82 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import re
|
| 4 |
-
from typing import Dict, Optional
|
| 5 |
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
return "", ""
|
| 10 |
-
marker = "USER_MESSAGE:"
|
| 11 |
-
idx = full_text.find(marker)
|
| 12 |
-
if idx == -1:
|
| 13 |
-
return "", full_text.strip()
|
| 14 |
-
hidden = full_text[:idx].strip()
|
| 15 |
-
user = full_text[idx + len(marker):].strip()
|
| 16 |
-
return hidden, user
|
| 17 |
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"question": r"Question:\s*(.+?)(?:\nOptions:|\nPlayer balance:|\nLast outcome:|$)",
|
| 28 |
-
"options": r"Options:\s*(.+?)(?:\nPlayer balance:|\nLast outcome:|$)",
|
| 29 |
-
}
|
| 30 |
-
for key, pattern in patterns.items():
|
| 31 |
-
m = re.search(pattern, hidden_context, re.DOTALL)
|
| 32 |
-
if m:
|
| 33 |
-
fields[key] = m.group(1).strip()
|
| 34 |
-
return fields
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return supplied
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
return "method"
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if
|
| 54 |
-
return "
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
return "answer"
|
|
|
|
| 57 |
return "answer"
|
| 58 |
|
| 59 |
|
| 60 |
def intent_to_help_mode(intent: str) -> str:
|
|
|
|
|
|
|
| 61 |
if intent == "hint":
|
| 62 |
return "hint"
|
| 63 |
-
if intent in {"
|
| 64 |
-
return
|
| 65 |
-
return "answer"
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import re
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
+
def detect_intent(text: str) -> str:
|
| 7 |
+
t = (text or "").strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
if not t:
|
| 10 |
+
return "answer"
|
| 11 |
|
| 12 |
+
# definition
|
| 13 |
+
if (
|
| 14 |
+
re.search(r"\bdefine\b", t)
|
| 15 |
+
or re.search(r"\bdefinition\b", t)
|
| 16 |
+
or re.search(r"\bwhat does\b", t)
|
| 17 |
+
or re.search(r"\bwhat is meant by\b", t)
|
| 18 |
+
):
|
| 19 |
+
return "definition"
|
| 20 |
|
| 21 |
+
# hint
|
| 22 |
+
if re.search(r"\bhint\b", t) or re.search(r"\bclue\b", t) or re.search(r"\bnudge\b", t):
|
| 23 |
+
return "hint"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# step/instruction
|
| 26 |
+
if (
|
| 27 |
+
re.search(r"\bfirst step\b", t)
|
| 28 |
+
or re.search(r"\bnext step\b", t)
|
| 29 |
+
or re.search(r"\bwhat should i do first\b", t)
|
| 30 |
+
or re.search(r"\bgive me the first step\b", t)
|
| 31 |
+
):
|
| 32 |
+
return "instruction"
|
| 33 |
|
| 34 |
+
# walkthrough / step-by-step
|
| 35 |
+
if (
|
| 36 |
+
re.search(r"\bwalk ?through\b", t)
|
| 37 |
+
or re.search(r"\bstep by step\b", t)
|
| 38 |
+
or re.search(r"\bfull working\b", t)
|
| 39 |
+
or re.search(r"\bwork through\b", t)
|
| 40 |
+
):
|
| 41 |
+
return "walkthrough"
|
| 42 |
|
| 43 |
+
# explain
|
| 44 |
+
if re.search(r"\bexplain\b", t) or re.search(r"\bwhy\b", t):
|
| 45 |
+
return "explain"
|
|
|
|
| 46 |
|
| 47 |
+
# method
|
| 48 |
+
if (
|
| 49 |
+
re.search(r"\bmethod\b", t)
|
| 50 |
+
or re.search(r"\bapproach\b", t)
|
| 51 |
+
or re.search(r"\bhow do i solve\b", t)
|
| 52 |
+
or re.search(r"\bhow to solve\b", t)
|
| 53 |
+
):
|
| 54 |
return "method"
|
| 55 |
+
|
| 56 |
+
# concept
|
| 57 |
+
if re.search(r"\bconcept\b", t) or re.search(r"\bprinciple\b", t) or re.search(r"\brule\b", t):
|
| 58 |
+
return "concept"
|
| 59 |
+
|
| 60 |
+
# plain answer / direct solve
|
| 61 |
+
if (
|
| 62 |
+
re.search(r"\bsolve\b", t)
|
| 63 |
+
or re.search(r"\bwhat is\b", t)
|
| 64 |
+
or re.search(r"\bfind\b", t)
|
| 65 |
+
or re.search(r"\bgive (?:me )?the answer\b", t)
|
| 66 |
+
or re.search(r"\bjust the answer\b", t)
|
| 67 |
+
or re.search(r"\banswer only\b", t)
|
| 68 |
+
or re.search(r"\bcalculate\b", t)
|
| 69 |
+
):
|
| 70 |
return "answer"
|
| 71 |
+
|
| 72 |
return "answer"
|
| 73 |
|
| 74 |
|
| 75 |
def intent_to_help_mode(intent: str) -> str:
|
| 76 |
+
if intent in {"walkthrough", "step_by_step", "explain", "method", "concept"}:
|
| 77 |
+
return "walkthrough"
|
| 78 |
if intent == "hint":
|
| 79 |
return "hint"
|
| 80 |
+
if intent in {"definition", "instruction"}:
|
| 81 |
+
return intent
|
| 82 |
+
return "answer"
|