j-js commited on
Commit
08229e6
·
verified ·
1 Parent(s): dd561c8

Create context_parser.py

Browse files
Files changed (1) hide show
  1. context_parser.py +160 -0
context_parser.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import re
4
+ from typing import List, Optional
5
+
6
+ from models import ParsedContext
7
+
8
+
9
+ def extract_section(text: str, start_label: str, end_labels: List[str]) -> str:
10
+ start_idx = text.find(start_label)
11
+ if start_idx == -1:
12
+ return ""
13
+
14
+ start_idx += len(start_label)
15
+ remaining = text[start_idx:]
16
+
17
+ end_positions = []
18
+ for label in end_labels:
19
+ idx = remaining.find(label)
20
+ if idx != -1:
21
+ end_positions.append(idx)
22
+
23
+ if end_positions:
24
+ end_idx = min(end_positions)
25
+ return remaining[:end_idx].strip()
26
+
27
+ return remaining.strip()
28
+
29
+
30
+ def parse_hidden_context(message: str) -> ParsedContext:
31
+ raw = message or ""
32
+
33
+ visible_user_text = ""
34
+ question_text = ""
35
+ options_text = ""
36
+ recent_conversation = ""
37
+
38
+ if "Latest player message:" in raw:
39
+ visible_user_text = extract_section(raw, "Latest player message:", [])
40
+ elif "Player message:" in raw:
41
+ visible_user_text = extract_section(raw, "Player message:", [])
42
+ else:
43
+ visible_user_text = raw.strip()
44
+
45
+ if "Question:" in raw:
46
+ question_text = extract_section(
47
+ raw,
48
+ "Question:",
49
+ ["\nOptions:", "\nPlayer balance:", "\nLast outcome:", "\nRecent conversation:", "\nLatest player message:", "\nPlayer message:"]
50
+ )
51
+
52
+ if "Options:" in raw:
53
+ options_text = extract_section(
54
+ raw,
55
+ "Options:",
56
+ ["\nPlayer balance:", "\nLast outcome:", "\nRecent conversation:", "\nLatest player message:", "\nPlayer message:"]
57
+ )
58
+
59
+ if "Recent conversation:" in raw:
60
+ recent_conversation = extract_section(
61
+ raw,
62
+ "Recent conversation:",
63
+ ["\nLatest player message:", "\nPlayer message:"]
64
+ )
65
+
66
+ combined_parts = []
67
+ if question_text:
68
+ combined_parts.append(question_text.strip())
69
+ if options_text:
70
+ combined_parts.append(options_text.strip())
71
+
72
+ combined_question_block = "\n".join([p for p in combined_parts if p]).strip()
73
+ if not combined_question_block:
74
+ combined_question_block = raw.strip()
75
+
76
+ return ParsedContext(
77
+ raw_message=raw,
78
+ visible_user_text=visible_user_text.strip(),
79
+ full_context_text=raw.strip(),
80
+ question_text=question_text.strip(),
81
+ options_text=options_text.strip(),
82
+ combined_question_block=combined_question_block.strip(),
83
+ recent_conversation=recent_conversation.strip(),
84
+ )
85
+
86
+
87
+ def detect_help_mode(user_text: str, supplied: Optional[str]) -> str:
88
+ lower = (user_text or "").lower().strip()
89
+
90
+ if supplied in {"hint", "walkthrough", "answer"}:
91
+ if lower not in {"hint", "walkthrough", "answer"}:
92
+ if "hint" in lower:
93
+ return "hint"
94
+ if "walkthrough" in lower or "step by step" in lower or "work through" in lower:
95
+ return "walkthrough"
96
+ if "explain" in lower and "why" in lower:
97
+ return "walkthrough"
98
+ return supplied
99
+ return supplied
100
+
101
+ if lower in {"hint", "just a hint", "hint please", "small hint"}:
102
+ return "hint"
103
+
104
+ if "hint" in lower:
105
+ return "hint"
106
+
107
+ if (
108
+ "walkthrough" in lower
109
+ or "step by step" in lower
110
+ or "work through" in lower
111
+ or "explain step by step" in lower
112
+ ):
113
+ return "walkthrough"
114
+
115
+ if "why" in lower or "explain" in lower:
116
+ return "walkthrough"
117
+
118
+ return "answer"
119
+
120
+
121
+ def user_is_referring_to_existing_question(user_text: str, question_text: str) -> bool:
122
+ lower = (user_text or "").lower().strip()
123
+
124
+ if not question_text:
125
+ return False
126
+
127
+ short_refs = {
128
+ "help", "hint", "why", "why?", "explain", "explain it", "explain this",
129
+ "answer", "what's the answer", "what is the answer", "is it a", "is it b",
130
+ "is it c", "is it d", "is it e", "which one", "which option",
131
+ "what do i do first", "what do i do", "how do i start", "are you sure",
132
+ "can you help", "help me", "i don't get it", "i dont get it",
133
+ "why is it c", "why is it b", "why is it a", "why is it d", "why is it e"
134
+ }
135
+
136
+ if lower in short_refs:
137
+ return True
138
+
139
+ if len(lower) <= 25 and any(phrase in lower for phrase in [
140
+ "is it ", "why", "hint", "explain", "answer", "help", "first step"
141
+ ]):
142
+ return True
143
+
144
+ return False
145
+
146
+
147
+ def mentions_choice_letter(user_text: str) -> Optional[str]:
148
+ lower = (user_text or "").lower().strip()
149
+ m = re.search(r"\b(?:is it|answer is|it's|its|option|choice)\s*([a-e])\b", lower)
150
+ if m:
151
+ return m.group(1).upper()
152
+
153
+ m = re.search(r"\bwhy is it\s*([a-e])\b", lower)
154
+ if m:
155
+ return m.group(1).upper()
156
+
157
+ if lower in {"a", "b", "c", "d", "e"}:
158
+ return lower.upper()
159
+
160
+ return None