-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunking.py
More file actions
85 lines (70 loc) · 2.06 KB
/
Copy pathchunking.py
File metadata and controls
85 lines (70 loc) · 2.06 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Document chunking strategies."""
import re
from pathlib import Path
CHUNK_SIZE = 1000
CHUNK_OVERLAP = 200
# File extensions that should use code chunking
_CODE_EXTENSIONS = {".py", ".js", ".ts", ".jsx", ".tsx", ".go", ".rs", ".java", ".rb", ".php"}
# Binary file extensions to skip during indexing
_BINARY_SUFFIXES = {
".png",
".jpg",
".jpeg",
".gif",
".svg",
".ico",
".woff",
".woff2",
".ttf",
".eot",
".pyc",
".pyo",
".so",
".dll",
".exe",
".bin",
".whl",
".zip",
".tar",
".gz",
}
def chunk_text(text: str, chunk_size: int = CHUNK_SIZE, overlap: int = CHUNK_OVERLAP) -> list[str]:
if not text:
return []
if chunk_size <= 0:
return [text]
if len(text) <= chunk_size:
return [text]
# Clamp overlap to prevent infinite loop
overlap = min(overlap, chunk_size - 1)
chunks = []
start = 0
while start < len(text):
end = start + chunk_size
chunk = text[start:end]
if end < len(text):
last_period = chunk.rfind(". ")
last_newline = chunk.rfind("\n\n")
break_at = max(last_period, last_newline)
if break_at > chunk_size // 2:
chunk = text[start : start + break_at + 1]
end = start + break_at + 1
chunks.append(chunk.strip())
start = end - overlap
return [c for c in chunks if c]
def chunk_code(text: str) -> list[str]:
"""Chunk code files by function/class boundaries."""
if not text:
return []
# Split on function/class definitions or blank lines
chunks = re.split(r"\n(?=(?:def |class |async def |# ---|## ))", text)
return [c.strip() for c in chunks if c.strip()]
def auto_chunk(text: str, file_path: str | None = None) -> list[str]:
"""Auto-select chunking strategy based on file type."""
if file_path:
ext = Path(file_path).suffix.lower()
if ext in _CODE_EXTENSIONS:
chunks = chunk_code(text)
if chunks:
return chunks
return chunk_text(text)