File size: 25,515 Bytes
575f1c7
 
 
 
 
 
 
 
 
 
63279a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575f1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63279a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Text chunking strategies for RAG document processing."""

import re
from typing import List, Dict, Any, Tuple
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document
from src.core.logging_config import get_logger

logger = get_logger(__name__)

class LaTeXAwareChunker:
    """Handles LaTeX-aware document chunking that preserves LaTeX structures."""
    
    def __init__(self, chunk_size: int = 1200, chunk_overlap: int = 150):
        """
        Initialize the LaTeX-aware document chunker.
        
        Args:
            chunk_size: Maximum size of each chunk in characters
            chunk_overlap: Number of characters to overlap between chunks
        """
        self.chunk_size = chunk_size
        self.chunk_overlap = chunk_overlap
        
        # Initialize the text splitter with LaTeX-aware settings
        self.text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=chunk_size,
            chunk_overlap=chunk_overlap,
            length_function=len,
            separators=[
                "\n\\section{",      # Section headers
                "\n\\subsection{",  # Subsection headers
                "\n\\subsubsection{", # Subsubsection headers
                "\n\\title{",       # Title commands
                "\n\\begin{",       # Begin environments
                "\n\\end{",         # End environments
                "\n\n",             # Paragraph breaks
                "\n",               # Line breaks
                ". ",               # Sentence breaks
                " ",                # Word breaks
                ""                  # Character breaks
            ],
            keep_separator=True,
            add_start_index=True
        )
        
        # Regex patterns for LaTeX structures
        self.latex_table_pattern = re.compile(
            r'\\begin\{tabular\}.*?\\end\{tabular\}',
            re.DOTALL | re.MULTILINE
        )
        
        self.latex_title_pattern = re.compile(
            r'\\title\{[^}]*\}',
            re.MULTILINE
        )
        
        self.latex_section_pattern = re.compile(
            r'\\(?:sub)*section\*?\{[^}]*\}',
            re.MULTILINE
        )
        
        self.latex_environment_pattern = re.compile(
            r'\\begin\{[^}]+\}.*?\\end\{[^}]+\}',
            re.DOTALL | re.MULTILINE
        )
        
        logger.info(f"LaTeX-aware chunker initialized with chunk_size={chunk_size}, overlap={chunk_overlap}")
    
    def extract_latex_structures(self, content: str) -> Tuple[List[Tuple[int, int, str]], str]:
        """
        Extract LaTeX tables and environments, replacing them with placeholders.
        
        Args:
            content: Original LaTeX content
            
        Returns:
            Tuple of (structures_list, content_with_placeholders)
        """
        structures = []
        
        # Find all tabular environments (highest priority)
        for match in self.latex_table_pattern.finditer(content):
            structures.append((
                match.start(),
                match.end(),
                "latex_table",
                match.group()
            ))
        
        # Find other LaTeX environments (avoid overlapping with tables)
        for match in self.latex_environment_pattern.finditer(content):
            # Check if this environment overlaps with any table
            overlaps_with_table = any(
                table_start <= match.start() < table_end or 
                table_start < match.end() <= table_end
                for table_start, table_end, struct_type, _ in structures 
                if struct_type == "latex_table"
            )
            
            if not overlaps_with_table and "tabular" not in match.group():
                structures.append((
                    match.start(),
                    match.end(),
                    "latex_environment",
                    match.group()
                ))
        
        # Find titles and sections
        for match in self.latex_title_pattern.finditer(content):
            structures.append((
                match.start(),
                match.end(),
                "latex_title",
                match.group()
            ))
        
        for match in self.latex_section_pattern.finditer(content):
            structures.append((
                match.start(),
                match.end(),
                "latex_section",
                match.group()
            ))
        
        # Sort by start position
        structures.sort(key=lambda x: x[0])
        
        # Replace structures with placeholders
        content_with_placeholders = content
        offset = 0
        
        for i, (start, end, struct_type, struct_content) in enumerate(structures):
            placeholder = f"\n\n__LATEX_STRUCTURE_{i}_{struct_type.upper()}__\n\n"
            
            # Adjust positions based on previous replacements
            adjusted_start = start - offset
            adjusted_end = end - offset
            
            content_with_placeholders = (
                content_with_placeholders[:adjusted_start] +
                placeholder +
                content_with_placeholders[adjusted_end:]
            )
            
            # Update offset for next replacement
            offset += (end - start) - len(placeholder)
        
        return structures, content_with_placeholders
    
    def restore_latex_structures(self, chunks: List[str], structures: List[Tuple[int, int, str, str]]) -> List[str]:
        """
        Restore LaTeX structures in chunks, keeping tables and environments intact.
        
        Args:
            chunks: List of text chunks with placeholders
            structures: List of original structures
            
        Returns:
            List of chunks with restored structures
        """
        restored_chunks = []
        
        for chunk in chunks:
            restored_chunk = chunk
            
            # Find placeholders in this chunk
            placeholder_pattern = re.compile(r'__LATEX_STRUCTURE_(\d+)_(\w+)__')
            
            for match in placeholder_pattern.finditer(chunk):
                structure_index = int(match.group(1))
                
                if structure_index < len(structures):
                    original_structure = structures[structure_index][3]
                    restored_chunk = restored_chunk.replace(match.group(), original_structure)
            
            restored_chunks.append(restored_chunk)
        
        return restored_chunks
    
    def chunk_document(self, content: str, source_metadata: Dict[str, Any]) -> List[Document]:
        """
        Chunk a LaTeX document while preserving LaTeX structures.
        
        Args:
            content: The LaTeX content to chunk
            source_metadata: Metadata about the source document
            
        Returns:
            List of Document objects with chunked content and enhanced metadata
        """
        try:
            # Extract LaTeX structures and replace with placeholders
            structures, content_with_placeholders = self.extract_latex_structures(content)
            
            # Create a document object with placeholders
            doc = Document(
                page_content=content_with_placeholders,
                metadata=source_metadata
            )
            
            # Split the document into chunks
            chunks = self.text_splitter.split_documents([doc])
            
            # Restore LaTeX structures in chunks
            chunk_contents = [chunk.page_content for chunk in chunks]
            restored_contents = self.restore_latex_structures(chunk_contents, structures)
            
            # Create enhanced chunks with restored content
            enhanced_chunks = []
            for i, (chunk, restored_content) in enumerate(zip(chunks, restored_contents)):
                # Add chunk-specific metadata
                chunk.metadata.update({
                    "chunk_index": i,
                    "total_chunks": len(chunks),
                    "chunk_size": len(restored_content),
                    "chunk_id": f"{source_metadata.get('source_id', 'unknown')}_{i}",
                    "has_latex_table": "\\begin{tabular}" in restored_content,
                    "has_latex_environment": "\\begin{" in restored_content and "\\end{" in restored_content,
                    "has_latex_math": "\\(" in restored_content or "$" in restored_content,
                    "content_type": "latex"
                })
                
                # Update the chunk content with restored structures
                chunk.page_content = restored_content
                enhanced_chunks.append(chunk)
            
            logger.info(f"LaTeX document chunked into {len(enhanced_chunks)} structure-aware pieces")
            return enhanced_chunks
            
        except Exception as e:
            logger.error(f"Error chunking LaTeX document: {e}")
            # Fallback to regular chunking if LaTeX processing fails
            return self._fallback_chunk(content, source_metadata)
    
    def _fallback_chunk(self, content: str, source_metadata: Dict[str, Any]) -> List[Document]:
        """Fallback chunking method if LaTeX-aware chunking fails."""
        try:
            doc = Document(page_content=content, metadata=source_metadata)
            chunks = self.text_splitter.split_documents([doc])
            
            for i, chunk in enumerate(chunks):
                chunk.metadata.update({
                    "chunk_index": i,
                    "total_chunks": len(chunks),
                    "chunk_size": len(chunk.page_content),
                    "chunk_id": f"{source_metadata.get('source_id', 'unknown')}_{i}",
                    "content_type": "latex"
                })
            
            logger.warning(f"Used fallback chunking for LaTeX content: {len(chunks)} pieces")
            return chunks
            
        except Exception as e:
            logger.error(f"Error in LaTeX fallback chunking: {e}")
            raise

class MarkdownAwareChunker:
    """Handles markdown-aware document chunking that preserves tables and structures."""
    
    def __init__(self, chunk_size: int = 1000, chunk_overlap: int = 200):
        """
        Initialize the markdown-aware document chunker.
        
        Args:
            chunk_size: Maximum size of each chunk in characters
            chunk_overlap: Number of characters to overlap between chunks
        """
        self.chunk_size = chunk_size
        self.chunk_overlap = chunk_overlap
        
        # Initialize the text splitter with markdown-aware settings
        self.text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=chunk_size,
            chunk_overlap=chunk_overlap,
            length_function=len,
            separators=[
                "\n\n",  # Paragraphs and sections
                "\n# ",  # H1 headers
                "\n## ", # H2 headers
                "\n### ", # H3 headers
                "\n\n---\n\n",  # Horizontal rules
                "\n",    # Lines
                " ",     # Words
                ".",     # Sentences
                ",",     # Clauses
                ""       # Characters
            ],
            keep_separator=True,
            add_start_index=True
        )
        
        # Regex patterns for markdown structures
        self.table_pattern = re.compile(
            r'(\|.*\|.*\n)+(\|[-\s|:]+\|.*\n)(\|.*\|.*\n)*',
            re.MULTILINE
        )
        
        self.code_block_pattern = re.compile(
            r'```[\s\S]*?```',
            re.MULTILINE
        )
        
        logger.info(f"Markdown-aware chunker initialized with chunk_size={chunk_size}, overlap={chunk_overlap}")
    
    def extract_markdown_structures(self, content: str) -> Tuple[List[Tuple[int, int, str]], str]:
        """
        Extract markdown tables and code blocks, replacing them with placeholders.
        
        Args:
            content: Original markdown content
            
        Returns:
            Tuple of (structures_list, content_with_placeholders)
            where structures_list contains (start, end, type, content) tuples
        """
        structures = []
        
        # Find all tables
        for match in self.table_pattern.finditer(content):
            structures.append((
                match.start(),
                match.end(),
                "table",
                match.group()
            ))
        
        # Find all code blocks
        for match in self.code_block_pattern.finditer(content):
            structures.append((
                match.start(),
                match.end(),
                "code_block",
                match.group()
            ))
        
        # Sort by start position
        structures.sort(key=lambda x: x[0])
        
        # Replace structures with placeholders
        content_with_placeholders = content
        offset = 0
        
        for i, (start, end, struct_type, struct_content) in enumerate(structures):
            placeholder = f"\n\n__STRUCTURE_{i}_{struct_type.upper()}__\n\n"
            
            # Adjust positions based on previous replacements
            adjusted_start = start - offset
            adjusted_end = end - offset
            
            content_with_placeholders = (
                content_with_placeholders[:adjusted_start] +
                placeholder +
                content_with_placeholders[adjusted_end:]
            )
            
            # Update offset for next replacement
            offset += (end - start) - len(placeholder)
        
        return structures, content_with_placeholders
    
    def restore_structures(self, chunks: List[str], structures: List[Tuple[int, int, str, str]]) -> List[str]:
        """
        Restore markdown structures in chunks, keeping tables and code blocks intact.
        
        Args:
            chunks: List of text chunks with placeholders
            structures: List of original structures
            
        Returns:
            List of chunks with restored structures
        """
        restored_chunks = []
        
        for chunk in chunks:
            restored_chunk = chunk
            
            # Find placeholders in this chunk
            placeholder_pattern = re.compile(r'__STRUCTURE_(\d+)_(\w+)__')
            
            for match in placeholder_pattern.finditer(chunk):
                structure_index = int(match.group(1))
                
                if structure_index < len(structures):
                    original_structure = structures[structure_index][3]
                    restored_chunk = restored_chunk.replace(match.group(), original_structure)
            
            restored_chunks.append(restored_chunk)
        
        return restored_chunks
    
    def chunk_document(self, content: str, source_metadata: Dict[str, Any]) -> List[Document]:
        """
        Chunk a markdown document while preserving tables and code blocks.
        
        Args:
            content: The markdown content to chunk
            source_metadata: Metadata about the source document
            
        Returns:
            List of Document objects with chunked content and enhanced metadata
        """
        try:
            # Extract markdown structures (tables, code blocks) and replace with placeholders
            structures, content_with_placeholders = self.extract_markdown_structures(content)
            
            # Create a document object with placeholders
            doc = Document(
                page_content=content_with_placeholders,
                metadata=source_metadata
            )
            
            # Split the document into chunks
            chunks = self.text_splitter.split_documents([doc])
            
            # Restore markdown structures in chunks
            chunk_contents = [chunk.page_content for chunk in chunks]
            restored_contents = self.restore_structures(chunk_contents, structures)
            
            # Create enhanced chunks with restored content
            enhanced_chunks = []
            for i, (chunk, restored_content) in enumerate(zip(chunks, restored_contents)):
                # Add chunk-specific metadata
                chunk.metadata.update({
                    "chunk_index": i,
                    "total_chunks": len(chunks),
                    "chunk_size": len(restored_content),
                    "chunk_id": f"{source_metadata.get('source_id', 'unknown')}_{i}",
                    "has_table": "table" in restored_content.lower() and "|" in restored_content,
                    "has_code": "```" in restored_content
                })
                
                # Update the chunk content with restored structures
                chunk.page_content = restored_content
                enhanced_chunks.append(chunk)
            
            logger.info(f"Document chunked into {len(enhanced_chunks)} markdown-aware pieces")
            return enhanced_chunks
            
        except Exception as e:
            logger.error(f"Error chunking markdown document: {e}")
            # Fallback to regular chunking if markdown processing fails
            return self._fallback_chunk(content, source_metadata)
    
    def _fallback_chunk(self, content: str, source_metadata: Dict[str, Any]) -> List[Document]:
        """Fallback chunking method if markdown-aware chunking fails."""
        try:
            doc = Document(page_content=content, metadata=source_metadata)
            chunks = self.text_splitter.split_documents([doc])
            
            for i, chunk in enumerate(chunks):
                chunk.metadata.update({
                    "chunk_index": i,
                    "total_chunks": len(chunks),
                    "chunk_size": len(chunk.page_content),
                    "chunk_id": f"{source_metadata.get('source_id', 'unknown')}_{i}"
                })
            
            logger.warning(f"Used fallback chunking for {len(chunks)} pieces")
            return chunks
            
        except Exception as e:
            logger.error(f"Error in fallback chunking: {e}")
            raise
    
    def chunk_multiple_documents(self, documents: List[Dict[str, Any]]) -> List[Document]:
        """
        Chunk multiple documents for batch processing.
        
        Args:
            documents: List of dictionaries with 'content' and 'metadata' keys
            
        Returns:
            List of chunked Document objects
        """
        all_chunks = []
        
        for doc_data in documents:
            content = doc_data.get('content', '')
            metadata = doc_data.get('metadata', {})
            
            if content.strip():  # Only process non-empty content
                chunks = self.chunk_document(content, metadata)
                all_chunks.extend(chunks)
        
        logger.info(f"Chunked {len(documents)} documents into {len(all_chunks)} total chunks")
        return all_chunks
    
    def get_chunk_preview(self, chunks: List[Document], max_chunks: int = 5) -> str:
        """
        Generate a preview of chunks for debugging/logging.
        
        Args:
            chunks: List of Document chunks
            max_chunks: Maximum number of chunks to include in preview
            
        Returns:
            String preview of chunks
        """
        preview = f"Document Chunks Preview ({len(chunks)} total chunks):\n"
        preview += "=" * 50 + "\n"
        
        for i, chunk in enumerate(chunks[:max_chunks]):
            has_table = chunk.metadata.get('has_table', False)
            has_code = chunk.metadata.get('has_code', False)
            
            preview += f"Chunk {i + 1}:\n"
            preview += f"  Length: {len(chunk.page_content)} characters\n"
            preview += f"  Has Table: {has_table}, Has Code: {has_code}\n"
            preview += f"  Metadata: {chunk.metadata}\n"
            preview += f"  Content preview: {chunk.page_content[:100]}...\n"
            preview += "-" * 30 + "\n"
        
        if len(chunks) > max_chunks:
            preview += f"... and {len(chunks) - max_chunks} more chunks\n"
        
        return preview

class UnifiedDocumentChunker:
    """Unified chunker that handles both Markdown and LaTeX content types."""
    
    def __init__(self):
        """Initialize the unified chunker with both markdown and LaTeX chunkers."""
        self.markdown_chunker = MarkdownAwareChunker(chunk_size=1000, chunk_overlap=200)
        self.latex_chunker = LaTeXAwareChunker(chunk_size=1200, chunk_overlap=150)
        logger.info("Unified document chunker initialized with both Markdown and LaTeX support")
    
    def chunk_document(self, content: str, source_metadata: Dict[str, Any]) -> List[Document]:
        """
        Chunk a document using the appropriate chunker based on content type.
        
        Args:
            content: The document content to chunk
            source_metadata: Metadata about the source document
            
        Returns:
            List of Document objects with chunked content and enhanced metadata
        """
        # Determine content type from metadata or content analysis
        content_type = source_metadata.get('doc_type', 'markdown').lower()
        
        # Override content type detection for GOT-OCR results
        if source_metadata.get('conversion_method', '').startswith('GOT-OCR'):
            content_type = 'latex'
        
        # Auto-detect content type if not specified
        if content_type not in ['markdown', 'latex']:
            if self._is_latex_content(content):
                content_type = 'latex'
            else:
                content_type = 'markdown'
        
        # Use appropriate chunker
        if content_type == 'latex':
            logger.info("Using LaTeX-aware chunker for document")
            return self.latex_chunker.chunk_document(content, source_metadata)
        else:
            logger.info("Using Markdown-aware chunker for document")
            return self.markdown_chunker.chunk_document(content, source_metadata)
    
    def _is_latex_content(self, content: str) -> bool:
        """
        Auto-detect if content is LaTeX based on common LaTeX commands.
        
        Args:
            content: Content to analyze
            
        Returns:
            True if content appears to be LaTeX, False otherwise
        """
        latex_indicators = [
            r'\\begin\{',
            r'\\end\{',
            r'\\title\{',
            r'\\section',
            r'\\subsection',
            r'\\hline',
            r'\\multirow',
            r'\\multicolumn'
        ]
        
        # Count LaTeX indicators
        latex_count = sum(1 for indicator in latex_indicators if re.search(indicator, content))
        
        # If we find multiple LaTeX indicators, treat as LaTeX
        return latex_count >= 2
    
    def chunk_multiple_documents(self, documents: List[Dict[str, Any]]) -> List[Document]:
        """
        Chunk multiple documents using appropriate chunkers.
        
        Args:
            documents: List of dictionaries with 'content' and 'metadata' keys
            
        Returns:
            List of chunked Document objects
        """
        all_chunks = []
        
        for doc_data in documents:
            content = doc_data.get('content', '')
            metadata = doc_data.get('metadata', {})
            
            if content.strip():  # Only process non-empty content
                chunks = self.chunk_document(content, metadata)
                all_chunks.extend(chunks)
        
        logger.info(f"Chunked {len(documents)} documents into {len(all_chunks)} total chunks")
        return all_chunks
    
    def get_chunk_preview(self, chunks: List[Document], max_chunks: int = 5) -> str:
        """
        Generate a preview of chunks for debugging/logging.
        
        Args:
            chunks: List of Document chunks
            max_chunks: Maximum number of chunks to include in preview
            
        Returns:
            String preview of chunks
        """
        preview = f"Document Chunks Preview ({len(chunks)} total chunks):\n"
        preview += "=" * 50 + "\n"
        
        for i, chunk in enumerate(chunks[:max_chunks]):
            content_type = chunk.metadata.get('content_type', 'unknown')
            has_table = chunk.metadata.get('has_table', False) or chunk.metadata.get('has_latex_table', False)
            has_code = chunk.metadata.get('has_code', False) or chunk.metadata.get('has_latex_environment', False)
            
            preview += f"Chunk {i + 1} ({content_type}):\n"
            preview += f"  Length: {len(chunk.page_content)} characters\n"
            preview += f"  Has Table: {has_table}, Has Code/Environment: {has_code}\n"
            preview += f"  Metadata: {chunk.metadata}\n"
            preview += f"  Content preview: {chunk.page_content[:100]}...\n"
            preview += "-" * 30 + "\n"
        
        if len(chunks) > max_chunks:
            preview += f"... and {len(chunks) - max_chunks} more chunks\n"
        
        return preview

# Global unified chunker instance that supports both Markdown and LaTeX
document_chunker = UnifiedDocumentChunker()