import re def clean_text(t): """Exact cleaning rule from the project notebook (keep in sync).""" lines = str(t).split("\n") while len(lines) > 1: last = lines[-1].strip() if last == "" or (len(last.split()) <= 4 and not re.search(r"[.!?\"')]$", last)): lines.pop() else: break t = " ".join(lines) t = re.sub(r"={2,}", " ", t) t = re.sub(r"[*#`|_]", " ", t) return re.sub(r"\s+", " ", t).strip()