File size: 457 Bytes
481dfe6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 | import re
def clean_pdf_text(text: str) -> str:
"""Normalize text extracted from PDF chunks."""
# Collapse multiple whitespace into single space
text = re.sub(r"[ \t]+", " ", text)
# Collapse more than 2 consecutive newlines
text = re.sub(r"\n{3,}", "\n\n", text)
# Remove null bytes and control characters (except newlines/tabs)
text = re.sub(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]", "", text)
return text.strip()
|