j-js commited on
Commit
0e7b568
·
verified ·
1 Parent(s): 1503061

Update context_parser.py

Browse files
Files changed (1) hide show
  1. 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 split_unity_message(full_text: str) -> tuple[str, str]:
8
- if not full_text:
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
- def extract_game_context_fields(hidden_context: str) -> Dict[str, str]:
20
- fields = {"category": "", "difficulty": "", "question": "", "options": ""}
21
- if not hidden_context:
22
- return fields
 
 
 
 
23
 
24
- patterns = {
25
- "category": r"Category:\s*(.+)",
26
- "difficulty": r"Difficulty:\s*(.+)",
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
- def detect_intent(user_text: str, supplied: Optional[str] = None) -> str:
38
- lower = (user_text or "").strip().lower()
 
 
 
 
 
 
39
 
40
- if supplied:
41
- supplied = supplied.strip().lower()
42
- if supplied in {"hint", "method", "walkthrough", "step_by_step", "full_working", "answer"}:
43
- return supplied
44
 
45
- if any(p in lower for p in ["full working", "full working out", "show all the working", "complete working"]):
46
- return "full_working"
47
- if any(p in lower for p in ["step by step", "walkthrough", "work through", "explain step by step"]):
48
- return "step_by_step"
49
- if any(p in lower for p in ["how do i solve", "how to solve", "method", "what method", "approach"]):
 
 
50
  return "method"
51
- if any(p in lower for p in ["hint", "nudge", "first step", "how do i start", "what do i do first"]):
52
- return "hint"
53
- if any(p in lower for p in ["why", "explain", "breakdown"]):
54
- return "step_by_step"
55
- if any(p in lower for p in ["solve", "what is", "answer", "give me the answer"]):
 
 
 
 
 
 
 
 
 
 
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 {"method", "step_by_step", "full_working"}:
64
- return "walkthrough"
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"