Spaces:
Running
Running
Download src/utils/text_chunking.py from Leon4gr45/brain: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/Leon4gr45/brain/resolve/main/src/utils/text_chunking.py
- Command line
-
hf download hf://spaces/Leon4gr45/brain/src/utils/text_chunking.py
-
curl -L -o text_chunking.py https://huggingface.co/spaces/Leon4gr45/brain/resolve/main/src/utils/text_chunking.py
1.31 kB
| def chunk_text(text: str, max_chars: int = 6000) -> list[str]: | |
| cleaned = (text or "").strip() | |
| if not cleaned or len(cleaned) <= max_chars: | |
| return [cleaned] if cleaned else [""] | |
| # Prefer blank-line paragraphs, then single newlines (dialogue turns), | |
| # else hard character slices. | |
| if "\n\n" in cleaned: | |
| parts = [p.strip() for p in cleaned.split("\n\n") if p.strip()] | |
| joiner = "\n\n" | |
| elif "\n" in cleaned: | |
| parts = [p.strip() for p in cleaned.split("\n") if p.strip()] | |
| joiner = "\n" | |
| else: | |
| parts = [cleaned] | |
| joiner = "\n" | |
| if not parts: | |
| parts = [cleaned] | |
| chunks: list[str] = [] | |
| current = "" | |
| for part in parts: | |
| candidate = f"{current}{joiner}{part}".strip() if current else part | |
| if len(candidate) <= max_chars: | |
| current = candidate | |
| continue | |
| if current: | |
| chunks.append(current) | |
| current = "" | |
| if len(part) <= max_chars: | |
| current = part | |
| continue | |
| for i in range(0, len(part), max_chars): | |
| piece = part[i : i + max_chars].strip() | |
| if piece: | |
| chunks.append(piece) | |
| current = "" | |
| if current: | |
| chunks.append(current) | |
| return chunks or [cleaned] | |