Spaces:
Sleeping
Sleeping
Download backend/lib/chunker.py from orakzai-io/Rednote: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/orakzai-io/Rednote/resolve/main/backend/lib/chunker.py
- Command line
-
hf download hf://spaces/orakzai-io/Rednote/backend/lib/chunker.py
-
curl -L -o chunker.py https://huggingface.co/spaces/orakzai-io/Rednote/resolve/main/backend/lib/chunker.py
1.22 kB
| # FUNCTION — chunk_text(text, chunk_size, overlap) | |
| def chunk_text(text: str, chunk_size: int = 500, overlap: int = 50) -> list[str]: | |
| """ | |
| Splits a string into overlapping chunks of words in O(N) time complexity. | |
| Args: | |
| text (str): The raw text to chunk. | |
| chunk_size (int): Max number of words per chunk. | |
| overlap (int): Number of overlapping words between successive chunks. | |
| Returns: | |
| list[str]: List of text chunks. | |
| """ | |
| words: list[str] = text.split() | |
| if not words: | |
| return [] | |
| # Standardize parameters to prevent infinite loops | |
| if chunk_size <= 0: | |
| chunk_size = 500 | |
| if overlap >= chunk_size or overlap < 0: | |
| overlap = min(50, chunk_size - 1) if chunk_size > 1 else 0 | |
| chunks: list[str] = [] | |
| num_words = len(words) | |
| step = chunk_size - overlap | |
| i = 0 | |
| while i < num_words: | |
| chunk_words: list[str] = words[i : i + chunk_size] | |
| chunks.append(" ".join(chunk_words)) | |
| i += step | |
| # If the remaining words are fewer than the overlap, stop to avoid redundant tiny chunks | |
| if num_words - i <= overlap: | |
| break | |
| return chunks |