-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunker.py
More file actions
50 lines (32 loc) · 1.37 KB
/
Copy pathchunker.py
File metadata and controls
50 lines (32 loc) · 1.37 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
class Chunker:
def __init__(self, chunk_size=250, overlap=40):
self.chunk_size = chunk_size
self.overlap = overlap
def chunker(self, text):
if self.overlap >= self.chunk_size:
raise ValueError("Overlap must be smaller than chunk size.")
chunks = []
start = 0
text_size = len(text)
while start < text_size:
end = start + self.chunk_size
if end < text_size:
last_space = text.rfind(' ', start, end)
if last_space != -1:
end = last_space
chunk = text[start:end].strip()
if chunk:
chunks.append(chunk)
start = end - self.overlap
return chunks
while start < text_size:
end = start + self.chunk_size
if end < text_size:
last_space = clean_text.rfind(' ', start, end)
if last_space != -1 and last_space > start:
end = last_space
chunk = clean_text[start:end].strip()
if chunk:
chunks.append(chunk)
start = end - self.overlap
return chunks