Spaces:
Running
Running
Update context_parser.py
Browse files- context_parser.py +85 -21
context_parser.py
CHANGED
|
@@ -35,7 +35,9 @@ def parse_hidden_context(message: str) -> ParsedContext:
|
|
| 35 |
options_text = ""
|
| 36 |
recent_conversation = ""
|
| 37 |
|
| 38 |
-
if "
|
|
|
|
|
|
|
| 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:", [])
|
|
@@ -46,21 +48,40 @@ def parse_hidden_context(message: str) -> ParsedContext:
|
|
| 46 |
question_text = extract_section(
|
| 47 |
raw,
|
| 48 |
"Question:",
|
| 49 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
if "Options:" in raw:
|
| 53 |
options_text = extract_section(
|
| 54 |
raw,
|
| 55 |
"Options:",
|
| 56 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
if "Recent conversation:" in raw:
|
| 60 |
recent_conversation = extract_section(
|
| 61 |
raw,
|
| 62 |
"Recent conversation:",
|
| 63 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
combined_parts = []
|
|
@@ -88,14 +109,17 @@ 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
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
return supplied
|
| 100 |
|
| 101 |
if lower in {"hint", "just a hint", "hint please", "small hint"}:
|
|
@@ -125,20 +149,59 @@ def user_is_referring_to_existing_question(user_text: str, question_text: str) -
|
|
| 125 |
return False
|
| 126 |
|
| 127 |
short_refs = {
|
| 128 |
-
"help",
|
| 129 |
-
"
|
| 130 |
-
"
|
| 131 |
-
"
|
| 132 |
-
"
|
| 133 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
}
|
| 135 |
|
| 136 |
if lower in short_refs:
|
| 137 |
return True
|
| 138 |
|
| 139 |
-
if
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
return True
|
| 143 |
|
| 144 |
return False
|
|
@@ -146,6 +209,7 @@ def user_is_referring_to_existing_question(user_text: str, question_text: str) -
|
|
| 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()
|
|
|
|
| 35 |
options_text = ""
|
| 36 |
recent_conversation = ""
|
| 37 |
|
| 38 |
+
if "USER_MESSAGE:" in raw:
|
| 39 |
+
visible_user_text = extract_section(raw, "USER_MESSAGE:", [])
|
| 40 |
+
elif "Latest player message:" in raw:
|
| 41 |
visible_user_text = extract_section(raw, "Latest player message:", [])
|
| 42 |
elif "Player message:" in raw:
|
| 43 |
visible_user_text = extract_section(raw, "Player message:", [])
|
|
|
|
| 48 |
question_text = extract_section(
|
| 49 |
raw,
|
| 50 |
"Question:",
|
| 51 |
+
[
|
| 52 |
+
"\nOptions:",
|
| 53 |
+
"\nPlayer balance:",
|
| 54 |
+
"\nLast outcome:",
|
| 55 |
+
"\nRecent conversation:",
|
| 56 |
+
"\nLatest player message:",
|
| 57 |
+
"\nPlayer message:",
|
| 58 |
+
"\nUSER_MESSAGE:",
|
| 59 |
+
],
|
| 60 |
)
|
| 61 |
|
| 62 |
if "Options:" in raw:
|
| 63 |
options_text = extract_section(
|
| 64 |
raw,
|
| 65 |
"Options:",
|
| 66 |
+
[
|
| 67 |
+
"\nPlayer balance:",
|
| 68 |
+
"\nLast outcome:",
|
| 69 |
+
"\nRecent conversation:",
|
| 70 |
+
"\nLatest player message:",
|
| 71 |
+
"\nPlayer message:",
|
| 72 |
+
"\nUSER_MESSAGE:",
|
| 73 |
+
],
|
| 74 |
)
|
| 75 |
|
| 76 |
if "Recent conversation:" in raw:
|
| 77 |
recent_conversation = extract_section(
|
| 78 |
raw,
|
| 79 |
"Recent conversation:",
|
| 80 |
+
[
|
| 81 |
+
"\nLatest player message:",
|
| 82 |
+
"\nPlayer message:",
|
| 83 |
+
"\nUSER_MESSAGE:",
|
| 84 |
+
],
|
| 85 |
)
|
| 86 |
|
| 87 |
combined_parts = []
|
|
|
|
| 109 |
lower = (user_text or "").lower().strip()
|
| 110 |
|
| 111 |
if supplied in {"hint", "walkthrough", "answer"}:
|
| 112 |
+
if "hint" in lower:
|
| 113 |
+
return "hint"
|
| 114 |
+
if (
|
| 115 |
+
"walkthrough" in lower
|
| 116 |
+
or "step by step" in lower
|
| 117 |
+
or "work through" in lower
|
| 118 |
+
or "explain step by step" in lower
|
| 119 |
+
):
|
| 120 |
+
return "walkthrough"
|
| 121 |
+
if "why" in lower or "explain" in lower:
|
| 122 |
+
return "walkthrough"
|
| 123 |
return supplied
|
| 124 |
|
| 125 |
if lower in {"hint", "just a hint", "hint please", "small hint"}:
|
|
|
|
| 149 |
return False
|
| 150 |
|
| 151 |
short_refs = {
|
| 152 |
+
"help",
|
| 153 |
+
"hint",
|
| 154 |
+
"why",
|
| 155 |
+
"why?",
|
| 156 |
+
"explain",
|
| 157 |
+
"explain it",
|
| 158 |
+
"explain this",
|
| 159 |
+
"answer",
|
| 160 |
+
"what's the answer",
|
| 161 |
+
"what is the answer",
|
| 162 |
+
"is it a",
|
| 163 |
+
"is it b",
|
| 164 |
+
"is it c",
|
| 165 |
+
"is it d",
|
| 166 |
+
"is it e",
|
| 167 |
+
"which one",
|
| 168 |
+
"which option",
|
| 169 |
+
"what do i do first",
|
| 170 |
+
"what do i do",
|
| 171 |
+
"how do i start",
|
| 172 |
+
"are you sure",
|
| 173 |
+
"can you help",
|
| 174 |
+
"help me",
|
| 175 |
+
"i don't get it",
|
| 176 |
+
"i dont get it",
|
| 177 |
+
"why is it c",
|
| 178 |
+
"why is it b",
|
| 179 |
+
"why is it a",
|
| 180 |
+
"why is it d",
|
| 181 |
+
"why is it e",
|
| 182 |
+
"what is the question asking me to do",
|
| 183 |
+
"what is this asking",
|
| 184 |
+
"what do you need",
|
| 185 |
}
|
| 186 |
|
| 187 |
if lower in short_refs:
|
| 188 |
return True
|
| 189 |
|
| 190 |
+
if any(
|
| 191 |
+
phrase in lower
|
| 192 |
+
for phrase in [
|
| 193 |
+
"is it ",
|
| 194 |
+
"why",
|
| 195 |
+
"hint",
|
| 196 |
+
"explain",
|
| 197 |
+
"answer",
|
| 198 |
+
"help",
|
| 199 |
+
"first step",
|
| 200 |
+
"what is the question asking",
|
| 201 |
+
"what is this asking",
|
| 202 |
+
"what do you need",
|
| 203 |
+
]
|
| 204 |
+
):
|
| 205 |
return True
|
| 206 |
|
| 207 |
return False
|
|
|
|
| 209 |
|
| 210 |
def mentions_choice_letter(user_text: str) -> Optional[str]:
|
| 211 |
lower = (user_text or "").lower().strip()
|
| 212 |
+
|
| 213 |
m = re.search(r"\b(?:is it|answer is|it's|its|option|choice)\s*([a-e])\b", lower)
|
| 214 |
if m:
|
| 215 |
return m.group(1).upper()
|