-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunker.py
More file actions
608 lines (491 loc) · 21.7 KB
/
Copy pathchunker.py
File metadata and controls
608 lines (491 loc) · 21.7 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
"""Text chunking utilities for processing large documents."""
import re
import logging
from typing import List, Optional
from transformers import AutoTokenizer
logger = logging.getLogger(__name__)
class TextChunker:
"""Handles intelligent text chunking for large documents."""
def __init__(
self,
max_tokens: int = 3500,
tokenizer_name: str = "google/gemma-3-27b-it",
debug_mode: bool = False
):
"""
Initialize the text chunker.
Args:
max_tokens: Maximum tokens per chunk.
tokenizer_name: HuggingFace tokenizer model name (default: 'google/gemma-3-27b-it').
debug_mode: Enable verbose logging.
"""
self.max_tokens = max_tokens
self.debug_mode = debug_mode
self.tokenizer = self._load_tokenizer(tokenizer_name)
def _load_tokenizer(self, tokenizer_name: str) -> Optional[AutoTokenizer]:
"""Load HuggingFace tokenizer for token counting."""
try:
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
logger.info("Tokenizer loaded successfully")
return tokenizer
except Exception as e:
logger.warning(f"Failed to load tokenizer: {e}. Token counting disabled.")
return None
def count_tokens(self, text: str) -> int:
"""
Count tokens in text using the loaded tokenizer.
Args:
text: Text to count tokens in.
Returns:
Token count, or 0 if tokenizer unavailable.
"""
if not self.tokenizer or not text:
return 0
try:
return len(self.tokenizer.encode(text))
except Exception as e:
logger.error(f"Error counting tokens: {e}")
return 0
def chunk_by_headings_blackdesert(self, text: str) -> List[str]:
"""
Split text by H2/H3 headings (playblackdesert.com structure).
Looks for:
- Lines starting with '### ' (H3 headings)
- Lines followed by '---' (H2 underlined headings)
Args:
text: Markdown text to split.
Returns:
List of text chunks split by headings.
"""
# Pattern: H3 headings OR text followed by dashes
pattern = re.compile(r'(^### |^[A-Za-z][^\n]*\n-+$)', re.MULTILINE)
matches = list(pattern.finditer(text))
if not matches:
logger.info("No H2/H3 headings found. Processing as single chunk.")
return [text.strip()] if text.strip() else []
# Create split points
split_points = [0] + [m.start() for m in matches] + [len(text)]
split_points = sorted(set(split_points))
chunks = []
for i in range(len(split_points) - 1):
start = split_points[i]
end = split_points[i + 1]
chunk = text[start:end].strip()
if chunk:
chunks.append(chunk)
logger.info(f"Split into {len(chunks)} chunks by H2/H3 headings")
return chunks
def chunk_by_headings_foundry(self, text: str) -> List[str]:
"""
Split text by H2/H3/H4 headings (blackdesertfoundry.com structure).
Looks for:
- ATX-style: ##, ###, or #### headings
- Setext-style: text followed by '---'
Args:
text: Markdown text to split.
Returns:
List of text chunks split by headings.
"""
# ATX-style H2/H3/H4
atx_pattern = r'^(?P<atx>#{2,4})\s+[^\n]+?\s*$'
# Setext-style H2 (text line followed by dashes)
setext_pattern = r'^(?P<setext>[^\s#][^\n]*?)\n-{3,}\s*$'
combined = re.compile(
rf'(?:{atx_pattern})|(?:{setext_pattern})',
flags=re.MULTILINE
)
matches = list(combined.finditer(text))
if not matches:
return [text.strip()] if text.strip() else []
split_points = [0] + [m.start() for m in matches] + [len(text)]
split_points = sorted(set(split_points))
chunks = []
for i in range(len(split_points) - 1):
start = split_points[i]
end = split_points[i + 1]
chunk = text[start:end].strip()
if chunk:
chunks.append(chunk)
# Merge short preamble with first heading chunk
if chunks and not self._starts_with_heading(chunks[0]):
if len(chunks) > 1 and len(chunks[0]) < 120:
chunks[1] = (chunks[0].rstrip() + "\n\n" + chunks[1].lstrip()).strip()
chunks.pop(0)
logger.info(f"Split into {len(chunks)} chunks by H2/H3/H4 headings")
return chunks
def chunk_by_headings_markdown(
self,
text: str,
min_level: int = 2,
include_setext: bool = True,
include_numbered: bool = True,
numbered_requires_hr: bool = True,
merge_short_preamble: bool = True,
preamble_threshold_chars: int = 160
) -> List[str]:
"""
Generic markdown splitter:
- ATX headings H{min_level..6} (e.g. ## ... ######)
- Optional Setext-style headings (=== / ---)
- Numbered section headings like '1. Title' (optionally require a '***' divider)
- Merge short preamble with the first heading
For the Garmoth site example: include_numbered=True and numbered_requires_hr=True are ideal.
"""
if not text or not text.strip():
return []
# ---------- ATX ----------
# Example: if min_level=2 this matches ##..######
atx = rf'^(?P<atx>#{{{min_level},6}})\s+[^\n]+?\s*$'
atx_re = re.compile(atx, re.MULTILINE)
# ---------- Setext ----------
setext_parts = []
if include_setext:
if 1 >= min_level:
setext_parts.append(r'(?P<setext_h1>^[^\s#][^\n]*?)\n=+\s*$')
if 2 >= min_level:
setext_parts.append(r'(?P<setext_h2>^[^\s#][^\n]*?)\n-+\s*$')
setext_re = re.compile('(?:' + '|'.join(setext_parts) + ')', re.MULTILINE) if setext_parts else None
# ---------- Numbered ----------
# Line example: "2. What Is Throne Of Edana"
numbered_line_re = re.compile(r'^(?P<num>\d+)\.\s+(?P<title>.+?)\s*$', re.MULTILINE)
hr_re = re.compile(r'^\s*\*{3,}\s*$', re.MULTILINE) # '***' or longer
# Collect all matches → split positions
split_positions = set([0, len(text)])
# ATX
for m in atx_re.finditer(text):
split_positions.add(m.start())
# Setext
if setext_re:
for m in setext_re.finditer(text):
split_positions.add(m.start())
# Numbered
if include_numbered:
for m in numbered_line_re.finditer(text):
ok = True
if numbered_requires_hr:
# True if there is a '***' within 1-3 lines after this heading
# (Blank lines are skipped)
line_end = text.find('\n', m.end())
if line_end == -1:
line_end = len(text)
# Check the next 3 lines
tail = text[line_end+1: ]
lines = tail.splitlines()
found_hr = False
check_upto = min(3, len(lines))
for i in range(check_upto):
s = lines[i].strip()
if not s: # skip blank lines
continue
if hr_re.match(lines[i]):
found_hr = True
break
# If the first non-empty line isn't an hr, continue; allow 2-3 lines tolerance
ok = found_hr
if ok:
split_positions.add(m.start())
# Split
points = sorted(split_positions)
chunks = []
for i in range(len(points) - 1):
seg = text[points[i]:points[i+1]].strip()
if seg:
chunks.append(seg)
# Merge short preamble with the first heading
if merge_short_preamble and chunks and not self._starts_with_heading_any_or_numbered(chunks[0]):
if len(chunks) > 1 and len(chunks[0]) <= preamble_threshold_chars:
chunks[1] = (chunks[0].rstrip() + "\n\n" + chunks[1].lstrip()).strip()
chunks.pop(0)
return chunks
def _starts_with_heading_any_or_numbered(self, text: str) -> bool:
"""
Accepts lines starting with ATX H1–H6, Setext H1/H2, or numbered headings like '^\d+\. '.
"""
return bool(re.match(
r'^(#{1,6})\s+|^[^\n]+\n[-=]{3,}\s*$|^\s*\d+\.\s+[^\n]+$',
text,
re.MULTILINE
))
def recombine_by_token_limit(self, chunks: List[str]) -> List[str]:
"""
Recombine chunks to maximize token usage without exceeding limits.
Groups non-heading chunks with the previous heading to form sections.
Splits oversized chunks intelligently to preserve context.
Args:
chunks: List of raw text chunks.
Returns:
List of recombined chunks optimized for token limits.
"""
if not chunks:
return []
heading_pattern = re.compile(
r"^(#{1,6}\s+.+$|[^\s#].+\n[-=]{3,}\s*$)",
re.MULTILINE
)
recombined = []
current_section = ""
can_count = bool(self.tokenizer)
for idx, chunk in enumerate(chunks):
chunk = chunk.strip()
if not chunk:
continue
is_heading_start = bool(heading_pattern.match(chunk))
# If this chunk starts a new heading and we already have a section,
# flush the current section and start a new one with this chunk.
if is_heading_start and current_section:
recombined.append(current_section.strip())
current_section = chunk
else:
if current_section:
# If tokenizer is available, check tokens before merging.
if can_count:
combined = (current_section + "\n\n" + chunk).strip()
try:
if self.count_tokens(combined) <= self.max_tokens:
current_section = combined
else:
# Do NOT include a partial of `chunk` in the current
# section. Flush current and start a new section
# with the whole chunk instead.
recombined.append(current_section.strip())
current_section = chunk
except Exception:
# On any token-counting error, fall back to simple concat
current_section += "\n\n" + chunk
else:
# Tokenizer unavailable: preserve previous behavior
current_section += "\n\n" + chunk
else:
current_section = chunk
if self.debug_mode:
logger.debug(
f"Chunk {idx+1}/{len(chunks)} assigned to "
f"{'new' if is_heading_start else 'current'} section"
)
if current_section:
recombined.append(current_section.strip())
# Merge heading-only chunks with their content
final_chunks = self._merge_orphaned_headings(recombined)
# Split oversized chunks
final_chunks = self._split_oversized_chunks(final_chunks)
logger.info(
f"{len(chunks)} initial chunks → {len(recombined)} sections → "
f"{len(final_chunks)} final chunks"
)
return final_chunks
def _merge_orphaned_headings(self, chunks: List[str]) -> List[str]:
"""Merge very short heading-only chunks with their following content."""
heading_pattern = re.compile(
r"^(#{1,6}\s+.+$|[^\s#].+\n[-=]{3,}\s*$)",
re.MULTILINE
)
result = []
i = 0
while i < len(chunks):
current = chunks[i]
# Check if this is a very short chunk (< 15 words) that starts with a heading
if len(current.split()) < 15 and heading_pattern.match(current):
# Try to merge with next chunk
if i + 1 < len(chunks):
merged = current.rstrip() + "\n\n" + chunks[i + 1].lstrip()
result.append(merged.strip())
i += 2 # Skip both chunks
continue
result.append(current)
i += 1
return result
def _split_oversized_chunks(self, chunks: List[str]) -> List[str]:
"""
Split chunks exceeding token limit while preserving context.
Each split part includes the section heading and continuation markers.
Args:
chunks: List of chunks to check and split.
Returns:
List with oversized chunks intelligently split.
"""
if not self.tokenizer:
logger.warning("Tokenizer unavailable, skipping oversized chunk splitting")
return chunks
result = []
heading_pattern = re.compile(
r"^(#{1,6}\s+.+$|[^\s#].+\n[-=]{3,}\s*$)",
re.MULTILINE
)
for chunk_idx, chunk in enumerate(chunks):
token_count = self.count_tokens(chunk)
if token_count <= self.max_tokens:
result.append(chunk)
continue
logger.warning(
f"Oversized chunk detected: {token_count} tokens "
f"(limit: {self.max_tokens}). Splitting chunk {chunk_idx + 1}..."
)
# Extract main heading
lines = chunk.split('\n')
heading_line, main_heading, content_start = self._extract_heading(lines)
# Split content into blocks
content_lines = lines[content_start:]
blocks = self._split_into_blocks(content_lines, heading_pattern)
# Combine blocks into sub-chunks within token limit
sub_chunks = self._create_sub_chunks(
blocks,
heading_line,
main_heading
)
result.extend(sub_chunks)
logger.info(f"Split into {len(sub_chunks)} manageable sub-chunks")
return result
def _extract_heading(self, lines: List[str]) -> tuple:
"""Extract heading from lines. Returns (heading_line, heading_text, content_start_idx)."""
for i, line in enumerate(lines):
stripped = line.strip()
# ATX-style heading (# ## ###)
if stripped.startswith('#'):
return line, stripped, i + 1
# Setext-style heading (underlined with = or -)
if i + 1 < len(lines):
next_line = lines[i + 1].strip()
if re.match(r'^[-=]{3,}\s*$', next_line):
heading_line = line + '\n' + lines[i + 1]
return heading_line, stripped, i + 2
# No heading found
return "## Content Section", "## Content Section", 0
def _split_into_blocks(
self,
lines: List[str],
heading_pattern: re.Pattern
) -> List[str]:
"""Split content lines into logical blocks (paragraphs, lists, etc.)."""
blocks = []
current_block = []
for line in lines:
stripped = line.strip()
# Start new block on: heading, empty line after content, or bullet point
should_split = (
heading_pattern.match(stripped) or
(not stripped and current_block and current_block[-1].strip()) or
(stripped.startswith('*') and not line.startswith(' '))
)
if should_split and current_block:
blocks.append('\n'.join(current_block))
current_block = []
current_block.append(line)
if current_block:
blocks.append('\n'.join(current_block))
return blocks
def _create_sub_chunks(
self,
blocks: List[str],
heading_line: str,
main_heading: str
) -> List[str]:
"""Combine blocks into sub-chunks that fit within token limit."""
sub_chunks = []
current_sub = []
current_sub_tokens = 0
heading_tokens = self.count_tokens(main_heading)
reserve_tokens = heading_tokens + 100 # Extra for continuation text
effective_limit = self.max_tokens - reserve_tokens
for block in blocks:
block_tokens = self.count_tokens(block)
# If single block exceeds limit, split by sentences
if block_tokens > effective_limit:
if current_sub:
sub_chunks.append('\n\n'.join(current_sub))
current_sub = []
current_sub_tokens = 0
# Split block by sentences
sentences = re.split(r'(?<=[.!?])\s+', block)
sentence_group = []
sentence_tokens = 0
for sent in sentences:
sent_tokens = self.count_tokens(sent)
if sentence_tokens + sent_tokens > effective_limit and sentence_group:
sub_chunks.append('\n'.join(sentence_group))
sentence_group = [sent]
sentence_tokens = sent_tokens
else:
sentence_group.append(sent)
sentence_tokens += sent_tokens
if sentence_group:
current_sub.append('\n'.join(sentence_group))
current_sub_tokens = sentence_tokens
elif current_sub_tokens + block_tokens > effective_limit:
# Current block would exceed limit
if current_sub:
sub_chunks.append('\n\n'.join(current_sub))
current_sub = [block]
current_sub_tokens = block_tokens
else:
# Add block to current sub-chunk
current_sub.append(block)
current_sub_tokens += block_tokens
# Add remaining content
if current_sub:
sub_chunks.append('\n\n'.join(current_sub))
# Add headings and continuation markers
return self._format_sub_chunks(sub_chunks, heading_line)
def _format_sub_chunks(
self,
sub_chunks: List[str],
heading_line: str
) -> List[str]:
"""Add headings and continuation markers to sub-chunks."""
formatted = []
for idx, sub_content in enumerate(sub_chunks):
part_num = idx + 1
total_parts = len(sub_chunks)
if part_num == 1:
# First part: original heading
chunk = f"{heading_line}\n\n{sub_content}"
if total_parts > 1:
chunk += f"\n\n**[Continued in Part {part_num + 1}]**"
else:
# Continuation parts
chunk = (
f"{heading_line}\n\n"
f"**[Part {part_num} of {total_parts} - Continued]**\n\n"
f"{sub_content}"
)
formatted.append(chunk)
if self.debug_mode:
tokens = self.count_tokens(chunk)
logger.debug(f"Sub-chunk {part_num}/{total_parts}: {tokens} tokens")
return formatted
def extract_heading_hierarchy(self, chunk: str) -> str:
"""
Extract heading hierarchy from a chunk for context.
Args:
chunk: Markdown text chunk.
Returns:
String with hierarchical heading structure.
"""
lines = chunk.split('\n')
headings = []
for i, line in enumerate(lines):
line = line.strip()
# ATX-style headings
if line.startswith('#'):
level = len(line) - len(line.lstrip('#'))
title = line.lstrip('#').strip()
headings.append((level, title))
if len(headings) >= 3:
break
# Setext-style H1 (=== underline)
elif i + 1 < len(lines) and lines[i + 1].strip().startswith('==='):
headings.append((1, line))
if len(headings) >= 3:
break
# Setext-style H2 (--- underline)
elif i + 1 < len(lines) and re.match(r'^-{3,}\s*$', lines[i + 1].strip()):
headings.append((2, line))
if len(headings) >= 3:
break
if not headings:
return "Guide Section"
# Build hierarchy string
context_parts = []
for level, title in headings:
indent = " " * (level - 1)
context_parts.append(f"{indent}{'#' * level} {title}")
return "\n".join(context_parts)