-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrd_processor.py
More file actions
1235 lines (1006 loc) · 46.2 KB
/
srd_processor.py
File metadata and controls
1235 lines (1006 loc) · 46.2 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
D&D 5e SRD PDF to Markdown Converter & RAG Chunker v2.0
This tool provides a complete, production-ready pipeline for converting D&D 5e System
Reference Document (SRD) PDFs into clean, well-formatted Markdown and further chunking
them into RAG-optimized pieces.
Main Features:
1. PDF text extraction with intelligent column detection
2. Basic text cleanup with regex-based formatting
3. AI-powered cleanup using OpenAI's GPT models
4. RAG-optimized chunking (200-500 words per chunk)
5. Table of Contents-driven section organization
6. Comprehensive health reporting on chunk quality
7. Smart workflow resume capability
8. Quality validation and assessment tools
New in v2.0:
- Web interface with FastAPI and real-time progress tracking
- Configuration profiles for different use cases (fast/quality/custom)
- Quality validation system with OCR confidence and D&D-specific scoring
- Comprehensive test suite with unit, integration, and performance tests
- Enhanced error handling and recovery mechanisms
- Parallel processing support for improved performance
- Smart caching to reduce API costs
Author: eddiefiggie
Last Updated: July 2025
License: MIT
"""
import pdfplumber
import re
from pathlib import Path
import os
import logging
from typing import Optional, Dict, List, Tuple
from openai import OpenAI
# Configuration handling - gracefully fallback if config.py doesn't exist
# New in v2.0: Enhanced configuration management with profiles and validation
try:
import config
except ImportError:
print("❌ Config file not found. Please copy config.example.py to config.py and set your API key.")
print(" Or set the OPENAI_API_KEY environment variable.")
print(" For enhanced features, consider using the new configuration profiles:")
print(" - from config_manager import ConfigManager, ProcessingConfig")
config = None
# ============================================================================
# CONFIGURATION & GLOBALS
# ============================================================================
# File configuration - use config file if available, otherwise use defaults
INPUT_FILE = config.INPUT_PDF_FILE if config else "SRD_CC_v5.2.1.pdf"
RAW_TEXT_FILE = config.RAW_TEXT_OUTPUT if config else "srd_raw_text.txt"
MARKDOWN_FILE = config.BASIC_MARKDOWN_OUTPUT if config else "srd_cleaned_output.md"
AI_CLEANED_FILE = config.AI_ENHANCED_OUTPUT if config else "srd_ai_cleaned.md"
# ============================================================================
# OPENAI CLIENT SETUP
# ============================================================================
def get_openai_client() -> OpenAI:
"""
Initialize OpenAI client with API key from config or environment variable.
Returns:
OpenAI: Configured OpenAI client instance
Raises:
ValueError: If no API key is found or client initialization fails
"""
try:
if config:
api_key = config.get_openai_api_key()
else:
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
raise ValueError("OPENAI_API_KEY environment variable not set")
# Validate API key format
if not api_key.startswith('sk-'):
raise ValueError("Invalid OpenAI API key format (should start with 'sk-')")
return OpenAI(api_key=api_key)
except ImportError as e:
raise ValueError(f"OpenAI library not installed: {e}") from e
except (AttributeError, TypeError) as e:
raise ValueError(f"Configuration error: {e}") from e
except Exception as e:
raise ValueError(f"Failed to initialize OpenAI client: {e}") from e
# ============================================================================
# PDF TEXT EXTRACTION
# ============================================================================
def extract_text_by_layout() -> bool:
"""
Extract text from the SRD PDF using intelligent layout detection.
This function handles different page layouts in the SRD:
- Page 1: Single-column title page
- Pages 2-4: Three-column table of contents (uses layout detection)
- Remaining pages: Two-column main content (split down the middle)
Output is saved to RAW_TEXT_FILE with page markers for later processing.
Returns:
bool: True if extraction succeeded, False otherwise
"""
try:
if not Path(INPUT_FILE).exists():
print(f"❌ PDF file not found: {INPUT_FILE}")
return False
with pdfplumber.open(INPUT_FILE) as pdf:
total_pages = len(pdf.pages)
print(f"📖 Processing {total_pages} pages...")
with open(RAW_TEXT_FILE, "w", encoding="utf-8") as out:
for i, page in enumerate(pdf.pages):
# Progress indicator
if i % 10 == 0 or i == total_pages - 1:
print(f" Page {i+1}/{total_pages}")
page_marker = f"<!-- Page {i+1} -->\n"
out.write(page_marker)
if i == 0:
# Single-column title page
text = page.extract_text()
elif 1 <= i <= 3:
# Table of contents – 3-column: use layout detection
text = page.extract_text(layout=True)
else:
# Two-column layout: split page down the middle
left = page.crop((0, 0, page.width / 2, page.height)).extract_text()
right = page.crop((page.width / 2, 0, page.width, page.height)).extract_text()
text = (left or "") + "\n" + (right or "")
out.write((text or "") + "\n\n")
print(f"✅ Text extraction complete: {RAW_TEXT_FILE}")
return True
except FileNotFoundError:
print(f"❌ PDF file not found: {INPUT_FILE}")
return False
except PermissionError:
print(f"❌ Permission denied accessing file: {INPUT_FILE}")
return False
except Exception as e:
print(f"❌ Error during PDF extraction: {e}")
return False
# ============================================================================
# PDF STRUCTURE ANALYSIS (for AI cleanup)
# ============================================================================
def analyze_page_structure_with_pdf(pdf, page_num: int) -> Optional[Dict]:
"""
Analyze the visual structure of a PDF page to understand formatting.
This function extracts font size information and identifies potential headers
by analyzing text positioning and font characteristics. The results help the
AI cleanup function understand document structure.
Args:
pdf: Open pdfplumber PDF object
page_num: Page number to analyze (0-based index)
Returns:
dict: Structure information containing:
- body_font_size: Most common font size (likely body text)
- header_candidates: List of potential headers with position info
- page_width/height: Page dimensions
None: If analysis fails
"""
try:
page = pdf.pages[page_num]
# Extract text with bounding boxes to understand structure
words = page.extract_words()
if not words:
return None
# Group words by similar font size (likely headers)
font_sizes = {}
for word in words:
size = word.get('size', 12)
if size not in font_sizes:
font_sizes[size] = []
font_sizes[size].append(word)
if not font_sizes:
return None
# Find the most common font size (body text)
body_size = max(font_sizes.keys(), key=lambda x: len(font_sizes[x]))
# Identify potential headers (larger than body text)
header_candidates = []
for size, words_list in font_sizes.items():
if size > body_size:
for word in words_list:
header_candidates.append({
'text': word['text'],
'size': size,
'y': word['top'],
'bold': word.get('fontname', '').lower().find('bold') != -1
})
# Sort headers by position (top to bottom)
header_candidates.sort(key=lambda x: -x['y']) # Negative for top-to-bottom
return {
'body_font_size': body_size,
'header_candidates': header_candidates,
'page_width': page.width,
'page_height': page.height
}
except (IndexError, KeyError, AttributeError) as e:
print(f"Warning: Could not analyze page {page_num + 1} structure: {e}")
return None
except Exception as e:
print(f"Warning: Unexpected error analyzing page {page_num + 1}: {e}")
return None
# ============================================================================
# AI-POWERED CLEANUP
# ============================================================================
def clean_page_with_ai(client: OpenAI, page_text: str, page_num: int, pdf_structure: Optional[Dict] = None) -> str:
"""
Use OpenAI to clean and format a single page of text.
This function sends raw OCR text to OpenAI's GPT model for intelligent cleanup,
including OCR error correction, proper Markdown formatting, and D&D-specific
content structuring.
Args:
client: OpenAI client instance
page_text: Raw OCR text from the page
page_num: Page number (0-based) for context
pdf_structure: Optional structure analysis from analyze_page_structure_with_pdf
Returns:
str: Clean, formatted Markdown text
"""
# Input validation
if not page_text or not page_text.strip():
return ""
# Truncate extremely long pages to avoid API limits
max_chars = 12000 # Conservative limit for API calls
if len(page_text) > max_chars:
page_text = page_text[:max_chars] + "\n[...truncated...]"
print(f" ⚠️ Page {page_num + 1} truncated to {max_chars} characters")
# Create context about the document structure
structure_context = ""
if pdf_structure and pdf_structure.get('header_candidates'):
headers = [h['text'] for h in pdf_structure['header_candidates'][:3]]
structure_context = f"\n\nDetected headers on this page: {', '.join(headers)}"
prompt = f"""You are helping to convert a D&D 5e SRD document from OCR text to clean Markdown.
Page {page_num + 1} content:
{page_text}{structure_context}
Please clean this text and apply proper Markdown formatting:
1. Fix OCR errors and typos
2. Apply proper header hierarchy (# ## ### etc.) based on content importance
3. Format spell/ability blocks with proper structure
4. Bold important keywords like "Casting Time:", "Range:", "Duration:", etc.
5. Create proper lists and tables where appropriate
6. Preserve D&D terminology exactly
7. Remove excessive whitespace but maintain paragraph structure
8. Ensure proper sentence flow and grammar
Return ONLY the cleaned Markdown text, no explanations."""
try:
model = config.OPENAI_MODEL if config else "gpt-4o-mini"
max_tokens = config.OPENAI_MAX_TOKENS if config else 4000
temperature = config.OPENAI_TEMPERATURE if config else 0.1
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert at cleaning OCR text and formatting D&D content in Markdown."},
{"role": "user", "content": prompt}
],
temperature=temperature,
max_tokens=max_tokens
)
result = response.choices[0].message.content
return result.strip() if result else page_text
except ImportError as e:
print(f"Warning: OpenAI library issue for page {page_num + 1}: {e}")
return page_text
except Exception as e:
print(f"Warning: AI cleanup failed for page {page_num + 1}: {e}")
return page_text # Return original text if AI fails
# ============================================================================
# BASIC TEXT CLEANUP (Non-AI)
# ============================================================================
def clean_text_to_markdown(raw_text):
"""
Apply basic regex-based text cleanup to convert raw OCR text to Markdown.
This function performs simple but effective cleanup operations that work
without AI, including:
- Fixing line breaks and spacing issues
- Converting ALL CAPS sections to headers
- Adding basic Markdown formatting for D&D keywords
Args:
raw_text: Raw OCR text with layout artifacts
Returns:
str: Basic cleaned Markdown text
"""
# Remove long blank gaps
text = re.sub(r"\n{3,}", "\n\n", raw_text)
# Remove broken words from hyphenated line endings
text = re.sub(r"-\n", "", text)
# Normalize line breaks in the middle of sentences
text = re.sub(r"(?<!\n)\n(?!\n)", " ", text)
# Normalize multiple spaces
text = re.sub(r" {2,}", " ", text)
# Convert SCREAMING TITLES into headers (crude heuristic)
text = re.sub(r"\n([A-Z][A-Z\s]{4,})\n", r"\n# \1\n", text)
# Bold common mechanical keywords for clarity
keywords = ["Casting Time", "Range", "Components", "Duration", "Level \\d+", "Saving Throw", "Hit Points"]
for kw in keywords:
text = re.sub(rf"(?<=\n)({kw}):", r"**\1**:", text)
return text.strip()
def ai_cleanup_by_pages(raw_text_file, output_file):
"""
Process each page individually with AI cleanup.
This function coordinates the AI-powered cleanup process by:
1. Reading the raw text file with page markers
2. Splitting into individual pages
3. Analyzing PDF structure for each page
4. Sending each page to OpenAI for cleanup
5. Combining results into a final cleaned file
Args:
raw_text_file: Path to raw text file with page markers
output_file: Path where AI-cleaned output will be saved
Returns:
bool: True if successful, False if failed
"""
print("🤖 Starting AI-powered cleanup...")
# Initialize OpenAI client
try:
client = get_openai_client()
except ValueError as e:
print(f"❌ {e}")
print("Please set your OPENAI_API_KEY environment variable")
return False
# Read the raw text
raw_text = Path(raw_text_file).read_text(encoding="utf-8")
# Split by page markers
page_pattern = r'<!-- Page (\d+) -->'
pages = re.split(page_pattern, raw_text)
# Open PDF for structure analysis
try:
with pdfplumber.open(INPUT_FILE) as pdf:
cleaned_pages = []
# Process pages (skip first empty element from split)
for i in range(1, len(pages), 2):
page_num = int(pages[i]) - 1 # Convert to 0-based index
page_text = pages[i + 1].strip()
if not page_text:
continue
print(f"🔄 Processing page {page_num + 1}...")
# Analyze PDF structure for this page
pdf_structure = analyze_page_structure_with_pdf(pdf, page_num)
# Clean with AI
cleaned_text = clean_page_with_ai(client, page_text, page_num, pdf_structure)
# Add page marker and cleaned content
cleaned_pages.append(f"<!-- Page {page_num + 1} -->\n\n{cleaned_text}")
# Combine all cleaned pages
final_text = "\n\n---\n\n".join(cleaned_pages)
# Write to output file
Path(output_file).write_text(final_text, encoding="utf-8")
print(f"✅ AI cleanup complete! Saved to {output_file}")
return True
except Exception as e:
print(f"❌ Error during AI cleanup: {e}")
return False
# ============================================================================
# RAG CHUNKING SYSTEM
# ============================================================================
def count_words(text):
"""
Count words in a text string, excluding Markdown formatting.
This function provides accurate word counts for chunking by:
- Removing Markdown syntax characters
- Normalizing whitespace
- Filtering out very short "words" (likely artifacts)
Args:
text: Text string to count words in
Returns:
int: Number of meaningful words in the text
"""
# Remove markdown artifacts and count actual words
clean_text = re.sub(r'[#\*\_\[\]\(\)]+', ' ', text) # Remove markdown chars
clean_text = re.sub(r'\s+', ' ', clean_text).strip() # Normalize whitespace
words = [word for word in clean_text.split() if word and len(word) > 1]
return len(words)
def split_large_content(content, target_min=200, target_max=500):
"""
Split large content into smaller chunks targeting 200-500 words each.
This function intelligently splits content by:
1. Using headers as natural break points when possible
2. Falling back to paragraph boundaries for oversized chunks
3. Preserving content structure and readability
The algorithm prioritizes keeping related content together while
staying within the target word count range for optimal RAG performance.
Args:
content: Text content to split
target_min: Minimum target words per chunk (default: 200)
target_max: Maximum target words per chunk (default: 500)
Returns:
list: List of content chunks as strings
"""
chunks = []
lines = content.split('\n')
current_chunk = []
current_word_count = 0
i = 0
while i < len(lines):
line = lines[i]
line_words = count_words(line)
# Check if this is a header (# ## ### etc)
is_header = line.strip().startswith('#') and len(line.strip()) > 1
# If we're at a header and have content, consider splitting
if is_header and current_chunk and current_word_count >= target_min:
# Save current chunk
chunk_content = '\n'.join(current_chunk).strip()
if chunk_content:
chunks.append(chunk_content)
current_chunk = []
current_word_count = 0
# Add line to current chunk
current_chunk.append(line)
current_word_count += line_words
# If chunk is getting too large, try to split at paragraph boundaries
if current_word_count >= target_max:
# Look for a good break point (empty line or next header)
break_point = None
for j in range(i + 1, min(i + 10, len(lines))): # Look ahead a bit
if not lines[j].strip(): # Empty line (paragraph break)
break_point = j
break
elif lines[j].strip().startswith('#'): # Header
break_point = j
break
if break_point:
# Split at the break point
chunk_content = '\n'.join(current_chunk).strip()
if chunk_content:
chunks.append(chunk_content)
current_chunk = []
current_word_count = 0
i = break_point - 1 # Will be incremented at end of loop
i += 1
# Add remaining content
if current_chunk:
chunk_content = '\n'.join(current_chunk).strip()
if chunk_content:
chunks.append(chunk_content)
return chunks
def chunk_file_for_rag(input_file, output_dir="export", target_min=200, target_max=500):
"""
Split the AI-cleaned markdown file into small, focused chunks suitable for RAG.
This is the main RAG optimization function that:
1. Uses the D&D 5e SRD Table of Contents as the authoritative structure guide
2. Keeps each ToC section as a single chunk when possible (preserves logical grouping)
3. Only splits sections that are much larger than target_max (>2x target_max)
4. Uses intelligent splitting at headers and paragraph boundaries
5. Adds YAML metadata to each chunk for RAG systems
6. Generates comprehensive quality reports
Chunking Strategy:
- Priority 1: Keep ToC sections intact (even if slightly over target_max)
- Priority 2: Split only very large sections using natural boundaries
- Priority 3: Maintain content coherence and context
Output Format:
- File naming: {seq}_{section_name}_SRD_5_2.md
- YAML frontmatter with metadata (title, source_section, word_count, chunk_id)
- Sequential numbering for easy organization
Args:
input_file: Path to AI-cleaned markdown file to chunk
output_dir: Directory where chunk files will be written (default: "export")
target_min: Minimum target words per chunk (default: 200)
target_max: Maximum target words per chunk (default: 500)
Returns:
bool: True if chunking succeeded, False if failed
"""
print(f"📁 Creating RAG-optimized chunks from {input_file}...")
print(f"🎯 Target: {target_min}-{target_max} words per chunk")
# Create export directory if it doesn't exist
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
print(f"📂 Export directory: {output_path.absolute()}")
# Read the input file
if not Path(input_file).exists():
print(f"❌ Input file {input_file} not found")
return False
content = Path(input_file).read_text(encoding="utf-8")
# Comprehensive Table of Contents sections based on the actual SRD structure
# Each entry represents a top-level category that should get its own chunk
toc_sections = [
("Legal Information", "# Legal Information"),
("Playing the Game", "# Playing the Game"),
("The Six Abilities", "# The Six Abilities"),
("D20 Tests", "# D20 Tests"),
("Proficiency", "# Proficiency"),
("Actions", "# Actions"),
("Social Interaction", "# Social Interaction"),
("Exploration", "# Exploration"),
("Combat", "# Combat"),
("Damage and Healing", "# Damage and Healing"),
("Character Creation", "# Character Creation"),
("Classes", "# Classes"),
("Character Backgrounds", "# Character Backgrounds"),
("Character Species", "# Character Species"),
("Feats", "# Feats"),
("Spells", "# Spells"),
("Fighter", "# Fighter"),
("Monk", "# Monk"),
("Paladin", "# Paladin"),
("Ranger", "# Ranger"),
("Rogue", "# Rogue"),
("Sorcerer", "# Sorcerer"),
("Warlock", "# Warlock"),
("Wizard", "# Wizard"),
("Character Origins", "# Character Origins"),
("Magic Items", "# Magic Items"),
("Magic Item Categories", "# Magic Item Categories"),
("Monsters", "# Monsters"),
("Monsters A–Z", "# Monsters A–Z"),
("Index of Stat Blocks", "# Index of Stat Blocks"),
("Magic Items A–Z", "# Magic Items A–Z"),
]
all_chunks = []
chunk_counter = 1
# Clear existing export directory
print(f"🧹 Clearing existing chunks in {output_dir}/")
for existing_file in output_path.glob("*.md"):
existing_file.unlink()
# Process each major section
for i, (section_name, section_header) in enumerate(toc_sections):
print(f"\n🔍 Processing section: {section_name}")
# Find section start
section_start = content.find(section_header)
if section_start == -1:
print(f"⚠️ Section '{section_name}' not found, skipping")
continue
# Find section end (next major section or end of document)
section_end = len(content)
for j in range(i + 1, len(toc_sections)):
next_header = toc_sections[j][1]
next_pos = content.find(next_header, section_start + 1)
if next_pos != -1:
section_end = next_pos
break
# Extract section content
section_content = content[section_start:section_end].strip()
section_word_count = count_words(section_content)
print(f" 📊 Section size: {section_word_count} words")
if section_word_count == 0:
print(" ⚠️ Empty section, skipping")
continue
# Strategy: Always try to keep ToC sections as single chunks if possible
# Only split if absolutely necessary (much larger than target_max)
if section_word_count <= target_max * 2: # Allow up to 2x target_max for single chunks
# Keep as single chunk - this preserves ToC structure
chunk_info = {
'number': chunk_counter,
'title': section_name,
'content': section_content,
'word_count': section_word_count,
'source_section': section_name
}
all_chunks.append(chunk_info)
status = "✅ Single chunk" if section_word_count <= target_max else "📏 Large single chunk"
print(f" {status}: {section_word_count} words")
chunk_counter += 1
else:
# Only split very large sections that are more than 2x target_max
print(f" 📝 Section too large ({section_word_count} words), splitting intelligently...")
# Try to split at natural boundaries (subsections first, then paragraphs)
sub_chunks = split_large_content(section_content, target_min, target_max)
print(f" 📝 Split into {len(sub_chunks)} sub-chunks")
for j, sub_chunk in enumerate(sub_chunks, 1):
sub_word_count = count_words(sub_chunk)
# Create title for sub-chunk
sub_title = f"{section_name} Part {j}" if len(sub_chunks) > 1 else section_name
chunk_info = {
'number': chunk_counter,
'title': sub_title,
'content': sub_chunk,
'word_count': sub_word_count,
'source_section': section_name
}
all_chunks.append(chunk_info)
print(f" - Part {j}: {sub_word_count} words")
chunk_counter += 1
# Write chunks to files and collect statistics
chunk_stats = {
'total_chunks': len(all_chunks),
'word_counts': [],
'ideal_range_count': 0,
'too_small_count': 0,
'too_large_count': 0,
'sections_covered': set()
}
print(f"\n📝 Writing {len(all_chunks)} chunks to files...")
for chunk_info in all_chunks:
# Clean title for filename
clean_title = re.sub(r'[^\w\s-]', '', chunk_info['title'])
clean_title = re.sub(r'\s+', '_', clean_title)
clean_title = clean_title.strip('_')
# Truncate if too long
if len(clean_title) > 40:
clean_title = clean_title[:40]
# Create filename
filename = f"{chunk_info['number']:03d}_{clean_title}_SRD_5_2.md"
filepath = output_path / filename
# Add metadata header to chunk
chunk_with_metadata = f"""---
title: "{chunk_info['title']}"
source_section: "{chunk_info['source_section']}"
word_count: {chunk_info['word_count']}
chunk_id: {chunk_info['number']:03d}
---
{chunk_info['content']}"""
# Write to file
try:
filepath.write_text(chunk_with_metadata, encoding="utf-8")
# Update statistics
word_count = chunk_info['word_count']
chunk_stats['word_counts'].append(word_count)
chunk_stats['sections_covered'].add(chunk_info['source_section'])
if target_min <= word_count <= target_max:
chunk_stats['ideal_range_count'] += 1
status = "✅"
elif word_count < target_min:
chunk_stats['too_small_count'] += 1
status = "🔸"
else:
chunk_stats['too_large_count'] += 1
status = "🔹"
print(f" {status} {filename} ({word_count} words)")
except OSError as e:
print(f"❌ Failed to create {filename}: {e}")
# Generate health report
generate_health_report(chunk_stats, target_min, target_max, len(toc_sections))
print(f"\n🎉 Successfully created {len(all_chunks)} RAG-optimized chunks in {output_dir}/")
return True
def generate_health_report(stats, target_min, target_max, total_sections):
"""Generate a comprehensive health report on the chunking results."""
print("\n" + "="*60)
print("📊 CHUNKING HEALTH REPORT")
print("="*60)
# Basic statistics
print(f"📦 Total chunks created: {stats['total_chunks']}")
print(f"📚 Sections covered: {len(stats['sections_covered'])}/{total_sections}")
if stats['word_counts']:
word_counts = stats['word_counts']
avg_words = sum(word_counts) / len(word_counts)
min_words = min(word_counts)
max_words = max(word_counts)
print("📝 Word count statistics:")
print(f" • Average: {avg_words:.0f} words")
print(f" • Range: {min_words} - {max_words} words")
print(f" • Target range: {target_min}-{target_max} words")
# Size distribution
print("\n📏 Size distribution:")
ideal_pct = (stats['ideal_range_count'] / stats['total_chunks']) * 100
small_pct = (stats['too_small_count'] / stats['total_chunks']) * 100
large_pct = (stats['too_large_count'] / stats['total_chunks']) * 100
print(f" ✅ Ideal size ({target_min}-{target_max} words): {stats['ideal_range_count']} chunks ({ideal_pct:.1f}%)")
print(f" 🔸 Too small (<{target_min} words): {stats['too_small_count']} chunks ({small_pct:.1f}%)")
print(f" 🔹 Too large (>{target_max} words): {stats['too_large_count']} chunks ({large_pct:.1f}%)")
# Quality assessment
print("\n🎯 Quality assessment:")
if ideal_pct >= 80:
print(" 🟢 EXCELLENT: >80% of chunks in ideal range")
elif ideal_pct >= 60:
print(" 🟡 GOOD: 60-80% of chunks in ideal range")
elif ideal_pct >= 40:
print(" 🟠 FAIR: 40-60% of chunks in ideal range")
else:
print(" 🔴 NEEDS IMPROVEMENT: <40% of chunks in ideal range")
# Recommendations
print("\n💡 Recommendations:")
if stats['too_large_count'] > stats['total_chunks'] * 0.2:
print(" • Consider splitting large chunks further at paragraph boundaries")
if stats['too_small_count'] > stats['total_chunks'] * 0.2:
print(" • Consider merging very small chunks with related content")
if len(stats['sections_covered']) < total_sections * 0.9:
print(" • Some sections may be missing - verify section detection logic")
print("="*60)
# ============================================================================
# LOGGING SETUP
# ============================================================================
def setup_logging(verbose: bool = True) -> logging.Logger:
"""
Set up logging with appropriate levels and formatting.
Args:
verbose: If True, show INFO level logs. If False, only show WARNING and above.
Returns:
Configured logger instance
"""
logger = logging.getLogger('srd_processor')
# Don't add handlers if already configured
if logger.handlers:
return logger
logger.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO if verbose else logging.WARNING)
# Formatter
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%H:%M:%S'
)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger
# Initialize logger
_logger = setup_logging(verbose=config.VERBOSE_LOGGING if config else True)
# ============================================================================
# CONFIGURATION VALIDATION
# ============================================================================
def validate_environment() -> Tuple[bool, List[str]]:
"""
Validate that the environment is properly set up for running the processor.
Returns:
Tuple of (is_valid, list_of_issues)
"""
issues = []
# Check if PDF file exists
if not Path(INPUT_FILE).exists():
issues.append(f"PDF file not found: {INPUT_FILE}")
# Check write permissions
try:
test_file = Path("test_write_permission.tmp")
test_file.write_text("test", encoding="utf-8")
test_file.unlink()
except OSError:
issues.append("No write permission in current directory")
# Check if config file exists when expected
if config is None:
issues.append("Config file not found - using defaults and environment variables")
# Check if required directories can be created
try:
Path("export").mkdir(exist_ok=True)
except Exception as e:
issues.append(f"Cannot create export directory: {e}")
return len(issues) == 0, issues
def print_environment_status():
"""Print environment validation results."""
is_valid, issues = validate_environment()
if is_valid:
print("✅ Environment validation passed")
else:
print("⚠️ Environment issues detected:")
for issue in issues:
print(f" • {issue}")
print()
# ============================================================================
# MAIN ORCHESTRATION
# ============================================================================
def detect_workflow_state():
"""
Detect what files already exist to determine where to resume the workflow.
Returns:
dict: Workflow state information containing:
- has_raw_text: bool - Raw text file exists
- has_basic_markdown: bool - Basic cleaned markdown exists
- has_ai_cleaned: bool - AI-enhanced markdown exists
- has_chunks: bool - RAG chunks directory exists with files
- suggested_start: str - Recommended starting point
"""
state = {
'has_raw_text': Path(RAW_TEXT_FILE).exists(),
'has_basic_markdown': Path(MARKDOWN_FILE).exists(),
'has_ai_cleaned': Path(AI_CLEANED_FILE).exists(),
'has_chunks': False,
'suggested_start': 'pdf_extraction'
}
# Check for existing chunks
export_dir = Path("export")
if export_dir.exists():
chunk_files = list(export_dir.glob("*.md"))
state['has_chunks'] = len(chunk_files) > 0
# Determine suggested starting point
if state['has_chunks']:
state['suggested_start'] = 'complete'
elif state['has_ai_cleaned']:
state['suggested_start'] = 'chunking'
elif state['has_basic_markdown']:
state['suggested_start'] = 'ai_cleanup'
elif state['has_raw_text']:
state['suggested_start'] = 'basic_cleanup'
else:
state['suggested_start'] = 'pdf_extraction'
return state
def print_workflow_status(state):
"""
Print a user-friendly summary of the current workflow state.
Args:
state: Dictionary from detect_workflow_state()
"""
print("📋 Current Workflow Status:")
print(f" {'✅' if state['has_raw_text'] else '❌'} Raw text extraction (srd_raw_text.txt)")
print(f" {'✅' if state['has_basic_markdown'] else '❌'} Basic cleanup (srd_cleaned_output.md)")
print(f" {'✅' if state['has_ai_cleaned'] else '❌'} AI cleanup (srd_ai_cleaned.md)")
print(f" {'✅' if state['has_chunks'] else '❌'} RAG chunking (export/ directory)")
print()
def ask_user_workflow_choice(state):
"""
Ask the user what they want to do based on the current workflow state.
Args:
state: Dictionary from detect_workflow_state()
Returns:
str: User's choice ('start_fresh', 'resume', 'skip_to', 'chunks_only')
"""
if state['suggested_start'] == 'complete':
print("🎉 All workflow steps appear to be complete!")
choice = input("Would you like to:\n"
" 1. Start fresh (overwrite existing files)\n"
" 2. Re-run chunking only\n"
" 3. Exit\n"
"Choice (1/2/3): ").strip()
if choice == '1':
return 'start_fresh'
elif choice == '2':
return 'chunks_only'
else:
return 'exit'
elif state['suggested_start'] == 'chunking':
print("📄 AI-cleaned file exists but no chunks found.")
choice = input("Would you like to:\n"
" 1. Start fresh (re-extract and re-process everything)\n"
" 2. Just create RAG chunks from existing AI-cleaned file\n"
" 3. Exit\n"
"Choice (1/2/3): ").strip()
if choice == '1':
return 'start_fresh'
elif choice == '2':
return 'chunks_only'
else:
return 'exit'
elif state['suggested_start'] == 'ai_cleanup':
print("📝 Basic cleaned file exists but no AI cleanup found.")