File size: 15,145 Bytes
11d9dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Document processing module for parsing and chunking various document formats.
"""

import re
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Any
import hashlib
import mimetypes

# Document parsing imports
import PyPDF2
from docx import Document as DocxDocument
from io import BytesIO

from .error_handler import DocumentProcessingError, validate_file_upload


class DocumentChunk:
    """Represents a chunk of processed document content."""
    
    def __init__(
        self,
        content: str,
        metadata: Dict[str, Any],
        chunk_id: str = None
    ):
        self.content = content.strip()
        self.metadata = metadata
        self.chunk_id = chunk_id or self._generate_chunk_id()
        
    def _generate_chunk_id(self) -> str:
        """Generate unique chunk ID based on content hash."""
        content_hash = hashlib.md5(self.content.encode()).hexdigest()[:8]
        source = self.metadata.get("source", "unknown")
        page = self.metadata.get("page", 0)
        return f"{Path(source).stem}_{page}_{content_hash}"
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert chunk to dictionary representation."""
        return {
            "chunk_id": self.chunk_id,
            "content": self.content,
            "metadata": self.metadata
        }


class DocumentProcessor:
    """Main document processing class supporting multiple formats."""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        self.processing_config = config.get("processing", {})
        self.chunk_size = self.processing_config.get("chunk_size", 512)
        self.chunk_overlap = self.processing_config.get("chunk_overlap", 50)
        self.min_chunk_size = self.processing_config.get("min_chunk_size", 100)
        self.max_chunks_per_doc = self.processing_config.get("max_chunks_per_doc", 1000)
        self.supported_formats = self.processing_config.get("supported_formats", ["pdf", "docx", "txt"])
        
    def process_document(
        self, 
        file_path: str, 
        filename: Optional[str] = None
    ) -> List[DocumentChunk]:
        """
        Process a document and return list of chunks.
        
        Args:
            file_path: Path to the document file
            filename: Optional original filename
            
        Returns:
            List of DocumentChunk objects
        """
        # Validate file
        max_size = self.config.get("app", {}).get("max_upload_size", 50) * 1024 * 1024
        allowed_extensions = [f".{fmt}" for fmt in self.supported_formats]
        validate_file_upload(file_path, max_size, allowed_extensions)
        
        file_path = Path(file_path)
        filename = filename or file_path.name
        
        # Detect file type and extract text
        try:
            text_content, metadata = self._extract_text(file_path, filename)
            
            if not text_content.strip():
                raise DocumentProcessingError("Document appears to be empty or contains no extractable text")
                
            # Create chunks
            chunks = self._create_chunks(text_content, metadata)
            
            if not chunks:
                raise DocumentProcessingError("Failed to create any valid chunks from document")
                
            if len(chunks) > self.max_chunks_per_doc:
                raise DocumentProcessingError(
                    f"Document too large. Generated {len(chunks)} chunks, "
                    f"maximum allowed is {self.max_chunks_per_doc}"
                )
                
            return chunks
            
        except Exception as e:
            if isinstance(e, DocumentProcessingError):
                raise
            else:
                raise DocumentProcessingError(f"Failed to process document: {str(e)}") from e
    
    def _extract_text(self, file_path: Path, filename: str) -> Tuple[str, Dict[str, Any]]:
        """Extract text from document based on file type."""
        file_extension = file_path.suffix.lower()
        
        # Base metadata
        metadata = {
            "source": str(file_path),
            "filename": filename,
            "file_type": file_extension,
            "file_size": file_path.stat().st_size
        }
        
        if file_extension == ".pdf":
            text, pdf_metadata = self._extract_pdf_text(file_path)
            metadata.update(pdf_metadata)
        elif file_extension == ".docx":
            text, docx_metadata = self._extract_docx_text(file_path)
            metadata.update(docx_metadata)
        elif file_extension == ".txt":
            text, txt_metadata = self._extract_txt_text(file_path)
            metadata.update(txt_metadata)
        else:
            raise DocumentProcessingError(f"Unsupported file format: {file_extension}")
        
        return text, metadata
    
    def _extract_pdf_text(self, file_path: Path) -> Tuple[str, Dict[str, Any]]:
        """Extract text from PDF file."""
        try:
            with open(file_path, "rb") as file:
                pdf_reader = PyPDF2.PdfReader(file)
                
                if len(pdf_reader.pages) == 0:
                    raise DocumentProcessingError("PDF file contains no pages")
                
                text_parts = []
                for page_num, page in enumerate(pdf_reader.pages):
                    try:
                        page_text = page.extract_text()
                        if page_text.strip():
                            text_parts.append(f"\n\n--- Page {page_num + 1} ---\n\n{page_text}")
                    except Exception as e:
                        # Log warning but continue with other pages
                        print(f"Warning: Could not extract text from page {page_num + 1}: {e}")
                
                if not text_parts:
                    raise DocumentProcessingError("Could not extract any text from PDF")
                
                # Extract metadata
                pdf_metadata = {
                    "page_count": len(pdf_reader.pages),
                    "pdf_metadata": {}
                }
                
                if pdf_reader.metadata:
                    pdf_metadata["pdf_metadata"] = {
                        "title": pdf_reader.metadata.get("/Title", ""),
                        "author": pdf_reader.metadata.get("/Author", ""),
                        "subject": pdf_reader.metadata.get("/Subject", ""),
                        "creator": pdf_reader.metadata.get("/Creator", "")
                    }
                
                return "\n".join(text_parts), pdf_metadata
                
        except Exception as e:
            if isinstance(e, DocumentProcessingError):
                raise
            else:
                raise DocumentProcessingError(f"Failed to read PDF file: {str(e)}") from e
    
    def _extract_docx_text(self, file_path: Path) -> Tuple[str, Dict[str, Any]]:
        """Extract text from DOCX file."""
        try:
            doc = DocxDocument(file_path)
            
            # Extract paragraphs
            paragraphs = []
            for paragraph in doc.paragraphs:
                text = paragraph.text.strip()
                if text:
                    paragraphs.append(text)
            
            # Extract tables
            table_texts = []
            for table in doc.tables:
                table_data = []
                for row in table.rows:
                    row_data = [cell.text.strip() for cell in row.cells if cell.text.strip()]
                    if row_data:
                        table_data.append(" | ".join(row_data))
                if table_data:
                    table_texts.append("Table:\n" + "\n".join(table_data))
            
            all_text = "\n\n".join(paragraphs + table_texts)
            
            if not all_text.strip():
                raise DocumentProcessingError("DOCX file contains no extractable text")
            
            # Metadata
            docx_metadata = {
                "paragraph_count": len(paragraphs),
                "table_count": len(table_texts)
            }
            
            # Core properties
            if hasattr(doc, "core_properties"):
                props = doc.core_properties
                docx_metadata["docx_metadata"] = {
                    "title": props.title or "",
                    "author": props.author or "",
                    "subject": props.subject or "",
                    "created": str(props.created) if props.created else ""
                }
            
            return all_text, docx_metadata
            
        except Exception as e:
            if isinstance(e, DocumentProcessingError):
                raise
            else:
                raise DocumentProcessingError(f"Failed to read DOCX file: {str(e)}") from e
    
    def _extract_txt_text(self, file_path: Path) -> Tuple[str, Dict[str, Any]]:
        """Extract text from TXT file."""
        try:
            # Try different encodings
            encodings = ["utf-8", "utf-8-sig", "latin1", "cp1252"]
            
            text = None
            encoding_used = None
            
            for encoding in encodings:
                try:
                    with open(file_path, "r", encoding=encoding) as file:
                        text = file.read()
                        encoding_used = encoding
                        break
                except UnicodeDecodeError:
                    continue
            
            if text is None:
                raise DocumentProcessingError("Could not decode text file with any supported encoding")
            
            if not text.strip():
                raise DocumentProcessingError("Text file is empty")
            
            # Basic text statistics
            lines = text.split("\n")
            txt_metadata = {
                "encoding": encoding_used,
                "line_count": len(lines),
                "char_count": len(text)
            }
            
            return text, txt_metadata
            
        except Exception as e:
            if isinstance(e, DocumentProcessingError):
                raise
            else:
                raise DocumentProcessingError(f"Failed to read text file: {str(e)}") from e
    
    def _create_chunks(self, text: str, base_metadata: Dict[str, Any]) -> List[DocumentChunk]:
        """Create overlapping chunks from text."""
        # Clean and normalize text
        text = self._clean_text(text)
        
        # Split into sentences for better chunk boundaries
        sentences = self._split_into_sentences(text)
        
        if not sentences:
            return []
        
        chunks = []
        current_chunk = []
        current_length = 0
        
        for sentence in sentences:
            sentence_length = len(sentence)
            
            # If adding this sentence would exceed chunk size
            if current_length + sentence_length > self.chunk_size and current_chunk:
                # Create chunk from current sentences
                chunk_text = " ".join(current_chunk)
                if len(chunk_text) >= self.min_chunk_size:
                    chunk_metadata = {
                        **base_metadata,
                        "chunk_index": len(chunks),
                        "char_count": len(chunk_text),
                        "sentence_count": len(current_chunk)
                    }
                    chunks.append(DocumentChunk(chunk_text, chunk_metadata))
                
                # Start new chunk with overlap
                if self.chunk_overlap > 0:
                    overlap_sentences = self._get_overlap_sentences(current_chunk)
                    current_chunk = overlap_sentences
                    current_length = sum(len(s) for s in overlap_sentences)
                else:
                    current_chunk = []
                    current_length = 0
            
            # Add current sentence
            current_chunk.append(sentence)
            current_length += sentence_length
        
        # Create final chunk
        if current_chunk:
            chunk_text = " ".join(current_chunk)
            if len(chunk_text) >= self.min_chunk_size:
                chunk_metadata = {
                    **base_metadata,
                    "chunk_index": len(chunks),
                    "char_count": len(chunk_text),
                    "sentence_count": len(current_chunk)
                }
                chunks.append(DocumentChunk(chunk_text, chunk_metadata))
        
        return chunks
    
    def _clean_text(self, text: str) -> str:
        """Clean and normalize text."""
        # Remove excessive whitespace
        text = re.sub(r'\s+', ' ', text)
        
        # Remove page markers (from PDF extraction)
        text = re.sub(r'\n--- Page \d+ ---\n', '\n', text)
        
        # Fix common OCR errors and formatting issues
        text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)  # Add space between camelCase
        text = re.sub(r'([.!?])([A-Z])', r'\1 \2', text)  # Add space after punctuation
        
        return text.strip()
    
    def _split_into_sentences(self, text: str) -> List[str]:
        """Split text into sentences using simple heuristics."""
        # Simple sentence splitting - can be enhanced with NLTK if needed
        sentences = re.split(r'[.!?]+', text)
        
        # Clean up sentences
        cleaned_sentences = []
        for sentence in sentences:
            sentence = sentence.strip()
            if len(sentence) >= 10:  # Minimum sentence length
                cleaned_sentences.append(sentence)
        
        return cleaned_sentences
    
    def _get_overlap_sentences(self, sentences: List[str]) -> List[str]:
        """Get sentences for overlap based on character count."""
        overlap_sentences = []
        overlap_length = 0
        
        # Take sentences from the end up to the overlap size
        for sentence in reversed(sentences):
            if overlap_length + len(sentence) <= self.chunk_overlap:
                overlap_sentences.insert(0, sentence)
                overlap_length += len(sentence)
            else:
                break
        
        return overlap_sentences
    
    def get_document_stats(self, chunks: List[DocumentChunk]) -> Dict[str, Any]:
        """Get statistics about processed document."""
        if not chunks:
            return {"chunk_count": 0, "total_chars": 0, "avg_chunk_size": 0}
        
        total_chars = sum(len(chunk.content) for chunk in chunks)
        
        return {
            "chunk_count": len(chunks),
            "total_chars": total_chars,
            "avg_chunk_size": total_chars / len(chunks),
            "min_chunk_size": min(len(chunk.content) for chunk in chunks),
            "max_chunk_size": max(len(chunk.content) for chunk in chunks),
            "source_file": chunks[0].metadata.get("filename", "unknown"),
            "file_type": chunks[0].metadata.get("file_type", "unknown")
        }