Spaces:
Running on Zero
Running on Zero
File size: 914 Bytes
d10de1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | """Clean raw document text before chunking."""
import re
def clean_text(text: str) -> str:
"""Remove excessive whitespace, headers noise, and non-printable characters."""
# Remove non-printable characters except newlines and tabs
text = re.sub(r"[^\x09\x0A\x0D\x20-\x7E\u00A0-\uFFFF]", " ", text)
# Collapse multiple blank lines into one
text = re.sub(r"\n{3,}", "\n\n", text)
# Collapse multiple spaces into one
text = re.sub(r" {2,}", " ", text)
# Strip leading/trailing whitespace
text = text.strip()
return text
def preprocess_documents(documents: list[dict]) -> list[dict]:
"""Apply clean_text to every document."""
cleaned = []
for doc in documents:
cleaned.append({
"source": doc["source"],
"text": clean_text(doc["text"]),
})
print(f"[preprocessing] Cleaned {len(cleaned)} documents.")
return cleaned
|