This commit is contained in:
Deniz Şafak
2025-04-29 07:12:08 +03:00
parent 4a7f1767b9
commit ed9036b7b7
5 changed files with 281 additions and 120 deletions
+3 -4
View File
@@ -73,15 +73,14 @@ def clean_text(text, *args, **kwargs):
# Load replace_single_newlines from config
cfg = load_config()
replace_single_newlines = cfg.get("replace_single_newlines", False)
# Trim spaces and tabs at the start and end of each line, preserving blank lines
text = "\n".join(line.strip() for line in text.splitlines())
# Collapse all whitespace (excluding newlines) into single spaces per line and trim edges
lines = [re.sub(r"[^\S\n]+", " ", line).strip() for line in text.splitlines()]
text = "\n".join(lines)
# Standardize paragraph breaks (multiple newlines become exactly two) and trim overall whitespace
text = re.sub(r"\n{3,}", "\n\n", text).strip()
# Optionally replace single newlines with spaces, but preserve double newlines
if replace_single_newlines:
text = re.sub(r"(?<!\n)\n(?!\n)", " ", text)
# Collapse multiple spaces and tabs into a single space
text = re.sub(r"[ \t]+", " ", text)
return text