Update formatting.py
Browse files- formatting.py +117 -1
formatting.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import re
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def style_prefix(tone: float) -> str:
|
|
@@ -179,4 +182,117 @@ def format_reply(core: str, tone: float, verbosity: float, transparency: float,
|
|
| 179 |
output.append("")
|
| 180 |
output.append(_why_line(topic, lines))
|
| 181 |
|
| 182 |
-
return "\n".join(output).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import re
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
from models import ExplainerResult
|
| 7 |
|
| 8 |
|
| 9 |
def style_prefix(tone: float) -> str:
|
|
|
|
| 182 |
output.append("")
|
| 183 |
output.append(_why_line(topic, lines))
|
| 184 |
|
| 185 |
+
return "\n".join(output).strip()
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
# -------------------------------------------------
|
| 189 |
+
# NEW: Explainer formatting
|
| 190 |
+
# -------------------------------------------------
|
| 191 |
+
|
| 192 |
+
def _explainer_header(tone: float) -> str:
|
| 193 |
+
if tone < 0.2:
|
| 194 |
+
return "Question breakdown:"
|
| 195 |
+
if tone < 0.45:
|
| 196 |
+
return "Here’s what the question is really asking:"
|
| 197 |
+
if tone < 0.75:
|
| 198 |
+
return "Let’s break down what the question is asking:"
|
| 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 |
+
|
| 219 |
+
lines.append("")
|
| 220 |
+
lines.append(title)
|
| 221 |
+
for item in cleaned[:limit]:
|
| 222 |
+
lines.append(f"- {item}")
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def format_explainer_response(
|
| 226 |
+
result: ExplainerResult,
|
| 227 |
+
tone: float,
|
| 228 |
+
verbosity: float,
|
| 229 |
+
transparency: float,
|
| 230 |
+
) -> str:
|
| 231 |
+
if not result or not result.understood:
|
| 232 |
+
return "I can help explain what the question is asking, but I need the full wording of the question."
|
| 233 |
+
|
| 234 |
+
output: List[str] = []
|
| 235 |
+
|
| 236 |
+
header = _explainer_header(tone)
|
| 237 |
+
if header:
|
| 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 |
+
|
| 256 |
+
if verbosity >= 0.65:
|
| 257 |
+
_append_section(
|
| 258 |
+
output,
|
| 259 |
+
"Constraints or conditions:",
|
| 260 |
+
result.constraints,
|
| 261 |
+
5,
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
if transparency >= 0.45:
|
| 265 |
+
_append_section(
|
| 266 |
+
output,
|
| 267 |
+
"Key relationship:",
|
| 268 |
+
result.relationships,
|
| 269 |
+
4,
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
if transparency >= 0.5:
|
| 273 |
+
_append_section(
|
| 274 |
+
output,
|
| 275 |
+
"Concepts you will probably need:",
|
| 276 |
+
result.needed_concepts,
|
| 277 |
+
4,
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
if transparency >= 0.55 or verbosity >= 0.7:
|
| 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."
|
| 297 |
+
|
| 298 |
+
return text
|