j-js commited on
Commit
0d89b07
·
verified ·
1 Parent(s): 12af63a

Update formatting.py

Browse files
Files changed (1) hide show
  1. formatting.py +92 -26
formatting.py CHANGED
@@ -25,14 +25,55 @@ def _clean_lines(core: str) -> list[str]:
25
  return lines
26
 
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def _dedupe_lines(lines: list[str]) -> list[str]:
29
  seen = set()
30
  output = []
31
  for line in lines:
32
- key = line.strip().lower()
33
  if key and key not in seen:
34
  seen.add(key)
35
- output.append(line)
36
  return output
37
 
38
 
@@ -40,7 +81,6 @@ def _limit_lines_for_verbosity(lines: list[str], verbosity: float, help_mode: st
40
  if not lines:
41
  return lines
42
 
43
- # hint should always stay short
44
  if help_mode == "hint":
45
  return lines[:1]
46
 
@@ -107,16 +147,16 @@ def _why_line(topic: str, lines: list[str]) -> str:
107
 
108
 
109
  def _tone_adjust_line(line: str, tone: float) -> str:
 
 
 
 
110
  if tone < 0.25:
111
  line = re.sub(r"^let’s\s+", "", line, flags=re.IGNORECASE)
 
112
  line = re.sub(r"^here is the idea in context:\s*", "", line, flags=re.IGNORECASE)
113
  return line.strip()
114
 
115
- if tone > 0.8:
116
- if line.lower().startswith("first step:"):
117
- return line
118
- return line
119
-
120
  return line
121
 
122
 
@@ -128,10 +168,15 @@ def format_reply(core: str, tone: float, verbosity: float, transparency: float,
128
  return prefix or "Start with the structure of the problem."
129
 
130
  lines = _clean_lines(core)
131
- lines = _dedupe_lines(lines)
132
  lines = [_tone_adjust_line(line, tone) for line in lines]
 
 
133
  lines = _limit_lines_for_verbosity(lines, verbosity, help_mode)
134
 
 
 
 
135
  topic = _extract_topic_from_lines(lines)
136
 
137
  output: list[str] = []
@@ -186,7 +231,7 @@ def format_reply(core: str, tone: float, verbosity: float, transparency: float,
186
 
187
 
188
  # -------------------------------------------------
189
- # NEW: Explainer formatting
190
  # -------------------------------------------------
191
 
192
  def _explainer_header(tone: float) -> str:
@@ -199,20 +244,27 @@ def _explainer_header(tone: float) -> str:
199
  return "You’ve got this — here’s what the question is really asking:"
200
 
201
 
202
- def _append_section(lines: List[str], title: str, items: List[str], limit: int) -> None:
203
  cleaned = []
204
  seen = set()
205
 
206
- for item in items:
207
  text = (item or "").strip()
208
  if not text:
209
  continue
210
- key = text.lower()
 
 
211
  if key in seen:
212
  continue
213
  seen.add(key)
214
  cleaned.append(text)
215
 
 
 
 
 
 
216
  if not cleaned:
217
  return
218
 
@@ -238,18 +290,20 @@ def format_explainer_response(
238
  output.append(header)
239
  output.append("")
240
 
241
- if result.plain_english:
242
- output.append(result.plain_english.strip())
 
243
 
244
- if result.asks_for and transparency >= 0.3:
 
245
  output.append("")
246
- output.append(f"The question is asking for: {result.asks_for}")
247
 
248
  if verbosity >= 0.4:
249
  _append_section(
250
  output,
251
  "What the question gives you:",
252
- result.givens,
253
  5 if verbosity >= 0.7 else 3,
254
  )
255
 
@@ -257,7 +311,7 @@ def format_explainer_response(
257
  _append_section(
258
  output,
259
  "Constraints or conditions:",
260
- result.constraints,
261
  5,
262
  )
263
 
@@ -265,7 +319,7 @@ def format_explainer_response(
265
  _append_section(
266
  output,
267
  "Key relationship:",
268
- result.relationships,
269
  4,
270
  )
271
 
@@ -273,7 +327,7 @@ def format_explainer_response(
273
  _append_section(
274
  output,
275
  "Concepts you will probably need:",
276
- result.needed_concepts,
277
  4,
278
  )
279
 
@@ -281,16 +335,28 @@ def format_explainer_response(
281
  _append_section(
282
  output,
283
  "Watch out for:",
284
- result.trap_notes,
285
  4,
286
  )
287
 
288
- if result.strategy_hint and verbosity >= 0.35:
 
289
  output.append("")
290
- output.append(f"Best starting move: {result.strategy_hint}")
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
- output = [line for line in output if line is not None]
293
- text = "\n".join(output).strip()
294
 
295
  if not text:
296
  return "I can help explain what the question is asking, but I need the full wording of the question."
 
25
  return lines
26
 
27
 
28
+ def _normalize_key(text: str) -> str:
29
+ text = (text or "").strip().lower()
30
+ text = text.replace("’", "'")
31
+ text = re.sub(r"\s+", " ", text)
32
+ return text
33
+
34
+
35
+ def _is_wrapper_line(line: str) -> bool:
36
+ key = _normalize_key(line).rstrip(":")
37
+ wrapper_lines = {
38
+ "let's work through it.",
39
+ "lets work through it.",
40
+ "let's solve it efficiently.",
41
+ "lets solve it efficiently.",
42
+ "you've got this — let's solve it cleanly.",
43
+ "youve got this — lets solve it cleanly.",
44
+ "you’ve got this — let’s solve it cleanly.",
45
+ "walkthrough",
46
+ "hint",
47
+ "steps",
48
+ "method",
49
+ "explanation",
50
+ "key idea",
51
+ "question breakdown",
52
+ "here’s what the question is really asking",
53
+ "here's what the question is really asking",
54
+ "let’s break down what the question is asking",
55
+ "lets break down what the question is asking",
56
+ "you’ve got this — here’s what the question is really asking",
57
+ "you've got this — here's what the question is really asking",
58
+ }
59
+ return key in wrapper_lines
60
+
61
+
62
+ def _strip_leading_wrapper_lines(lines: list[str]) -> list[str]:
63
+ cleaned = list(lines)
64
+ while cleaned and _is_wrapper_line(cleaned[0]):
65
+ cleaned.pop(0)
66
+ return cleaned
67
+
68
+
69
  def _dedupe_lines(lines: list[str]) -> list[str]:
70
  seen = set()
71
  output = []
72
  for line in lines:
73
+ key = _normalize_key(line)
74
  if key and key not in seen:
75
  seen.add(key)
76
+ output.append(line.strip())
77
  return output
78
 
79
 
 
81
  if not lines:
82
  return lines
83
 
 
84
  if help_mode == "hint":
85
  return lines[:1]
86
 
 
147
 
148
 
149
  def _tone_adjust_line(line: str, tone: float) -> str:
150
+ line = (line or "").strip()
151
+ if not line:
152
+ return line
153
+
154
  if tone < 0.25:
155
  line = re.sub(r"^let’s\s+", "", line, flags=re.IGNORECASE)
156
+ line = re.sub(r"^let's\s+", "", line, flags=re.IGNORECASE)
157
  line = re.sub(r"^here is the idea in context:\s*", "", line, flags=re.IGNORECASE)
158
  return line.strip()
159
 
 
 
 
 
 
160
  return line
161
 
162
 
 
168
  return prefix or "Start with the structure of the problem."
169
 
170
  lines = _clean_lines(core)
171
+ lines = _strip_leading_wrapper_lines(lines)
172
  lines = [_tone_adjust_line(line, tone) for line in lines]
173
+ lines = [line for line in lines if line]
174
+ lines = _dedupe_lines(lines)
175
  lines = _limit_lines_for_verbosity(lines, verbosity, help_mode)
176
 
177
+ if not lines:
178
+ return prefix or "Start with the structure of the problem."
179
+
180
  topic = _extract_topic_from_lines(lines)
181
 
182
  output: list[str] = []
 
231
 
232
 
233
  # -------------------------------------------------
234
+ # Explainer formatting
235
  # -------------------------------------------------
236
 
237
  def _explainer_header(tone: float) -> str:
 
244
  return "You’ve got this — here’s what the question is really asking:"
245
 
246
 
247
+ def _clean_section_items(items: List[str]) -> List[str]:
248
  cleaned = []
249
  seen = set()
250
 
251
+ for item in items or []:
252
  text = (item or "").strip()
253
  if not text:
254
  continue
255
+ if _is_wrapper_line(text):
256
+ continue
257
+ key = _normalize_key(text)
258
  if key in seen:
259
  continue
260
  seen.add(key)
261
  cleaned.append(text)
262
 
263
+ return cleaned
264
+
265
+
266
+ def _append_section(lines: List[str], title: str, items: List[str], limit: int) -> None:
267
+ cleaned = _clean_section_items(items)
268
  if not cleaned:
269
  return
270
 
 
290
  output.append(header)
291
  output.append("")
292
 
293
+ plain = (getattr(result, "plain_english", "") or "").strip()
294
+ if plain and not _is_wrapper_line(plain):
295
+ output.append(plain)
296
 
297
+ asks_for = (getattr(result, "asks_for", "") or "").strip()
298
+ if asks_for and transparency >= 0.3:
299
  output.append("")
300
+ output.append(f"The question is asking for: {asks_for}")
301
 
302
  if verbosity >= 0.4:
303
  _append_section(
304
  output,
305
  "What the question gives you:",
306
+ getattr(result, "givens", []) or [],
307
  5 if verbosity >= 0.7 else 3,
308
  )
309
 
 
311
  _append_section(
312
  output,
313
  "Constraints or conditions:",
314
+ getattr(result, "constraints", []) or [],
315
  5,
316
  )
317
 
 
319
  _append_section(
320
  output,
321
  "Key relationship:",
322
+ getattr(result, "relationships", []) or [],
323
  4,
324
  )
325
 
 
327
  _append_section(
328
  output,
329
  "Concepts you will probably need:",
330
+ getattr(result, "needed_concepts", []) or [],
331
  4,
332
  )
333
 
 
335
  _append_section(
336
  output,
337
  "Watch out for:",
338
+ getattr(result, "trap_notes", []) or [],
339
  4,
340
  )
341
 
342
+ strategy_hint = (getattr(result, "strategy_hint", "") or "").strip()
343
+ if strategy_hint and verbosity >= 0.35 and not _is_wrapper_line(strategy_hint):
344
  output.append("")
345
+ output.append(f"Best starting move: {strategy_hint}")
346
+
347
+ final_lines = []
348
+ previous_key = None
349
+ for line in output:
350
+ if line is None:
351
+ continue
352
+ text = str(line).rstrip()
353
+ key = _normalize_key(text)
354
+ if key == previous_key and key:
355
+ continue
356
+ final_lines.append(text)
357
+ previous_key = key
358
 
359
+ text = "\n".join(final_lines).strip()
 
360
 
361
  if not text:
362
  return "I can help explain what the question is asking, but I need the full wording of the question."