"""Plain-text normalisation shared by dump extraction and live API results, so the model sees the same style of text at training and at inference.""" import re _WS = re.compile(r"[ \t ]+") _EMPTY_PARENS = re.compile(r"\(\s*[,;]?\s*\)") _MULTI_NL = re.compile(r"\n{3,}") _SPACE_PUNCT = re.compile(r" +([,.;:!?])") def clean_plain(text: str) -> str: text = text.replace("\r", "") text = _EMPTY_PARENS.sub("", text) text = _WS.sub(" ", text) text = _SPACE_PUNCT.sub(r"\1", text) lines = [ln.strip() for ln in text.split("\n")] # drop list-only junk lines such as leftover thumbnail captions lines = [ln for ln in lines if ln and not ln.startswith(("thumb|", "|", "{|", "|}", "!"))] text = "\n".join(lines) text = _MULTI_NL.sub("\n\n", text) return text.strip() def first_sentences(text: str, n: int = 2, max_chars: int = 400) -> str: para = text.strip().split("\n")[0] parts = re.split(r"(?<=[.!?])\s+(?=[A-Z0-9\"'])", para) out = "" for p in parts[:n]: if out and len(out) + len(p) + 1 > max_chars: break out = (out + " " + p).strip() return out[:max_chars].strip()