File size: 15,956 Bytes
a072c5c
 
 
d8a8338
a072c5c
 
 
 
 
 
 
d8a8338
 
a072c5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8a8338
 
a072c5c
 
d8a8338
a072c5c
 
d8a8338
a072c5c
 
 
 
 
 
 
d269513
 
 
 
 
a072c5c
 
d269513
 
a072c5c
 
d269513
 
 
 
a072c5c
d8a8338
 
 
a072c5c
d269513
d8a8338
 
 
d269513
 
d8a8338
 
d269513
d8a8338
 
 
a072c5c
 
 
 
d269513
 
a072c5c
 
d269513
a072c5c
 
 
d269513
 
 
a072c5c
 
d269513
a072c5c
 
 
d269513
 
 
 
 
 
 
a072c5c
d269513
 
a072c5c
d269513
a072c5c
 
d269513
a072c5c
 
d269513
 
 
a072c5c
 
d269513
 
a072c5c
 
 
d269513
a072c5c
d269513
a072c5c
d269513
a072c5c
 
d269513
 
 
a072c5c
 
 
d269513
a072c5c
 
 
 
d269513
 
 
a072c5c
 
d269513
a072c5c
d269513
a072c5c
 
d269513
a072c5c
 
d269513
 
a072c5c
 
 
 
d269513
a072c5c
 
 
d269513
a072c5c
 
d269513
 
 
a072c5c
 
 
 
 
d269513
 
a072c5c
 
d269513
 
 
 
a072c5c
 
 
 
d269513
a072c5c
d269513
a072c5c
 
d269513
 
 
a072c5c
d269513
a072c5c
 
d269513
a072c5c
d269513
 
 
a072c5c
 
d269513
a072c5c
 
d269513
 
 
a072c5c
 
96ffb5c
a072c5c
d269513
a072c5c
 
 
 
 
 
 
 
96ffb5c
d269513
 
a072c5c
 
 
 
 
 
d269513
 
 
a072c5c
 
d269513
 
a072c5c
 
 
d269513
a072c5c
d8a8338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d269513
a072c5c
 
d269513
 
 
a072c5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d269513
a072c5c
 
d269513
 
 
a072c5c
 
 
 
d269513
a072c5c
 
d269513
a072c5c
 
d269513
a072c5c
 
d269513
a072c5c
 
 
d269513
a072c5c
 
 
d269513
a072c5c
d269513
a072c5c
 
 
d269513
 
 
 
 
a072c5c
 
d269513
 
a072c5c
d269513
 
 
 
 
 
 
 
a072c5c
 
d269513
a072c5c
 
d269513
a072c5c
 
 
96ffb5c
 
 
a072c5c
d269513
 
 
 
 
 
 
 
a072c5c
 
d269513
96ffb5c
a072c5c
d269513
a072c5c
 
 
 
 
d269513
 
 
 
 
 
 
a072c5c
 
d269513
a072c5c
 
d269513
a072c5c
 
 
 
 
 
d269513
 
 
a072c5c
 
d269513
 
a072c5c
 
 
 
 
d269513
 
a072c5c
d269513
a072c5c
 
 
d269513
a072c5c
 
 
 
 
d269513
 
 
a072c5c
 
d269513
 
a072c5c
 
 
 
 
 
 
 
 
 
 
d269513
 
a072c5c
d269513
 
 
a072c5c
 
 
d269513
 
a072c5c
d269513
a072c5c
 
 
 
 
 
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
# DEPENDENCIES
import io
import re
import sys
import PyPDF2
from typing import Any
from typing import Dict
from typing import Union
from pathlib import Path
from docx import Document
from typing import Optional
from config.settings import settings


try:
    # For PyMuPDF
    import fitz  
    PYMUPDF_AVAILABLE = True

except ImportError:
    PYMUPDF_AVAILABLE = False
    print("[DocumentReader] PyMuPDF not available. Install with: pip install PyMuPDF")

# Encoding detection
try:
    import chardet
    CHARDET_AVAILABLE = True

except ImportError:
    CHARDET_AVAILABLE = False



class DocumentReader:
    """
    Document reader supporting PDF and DOCX : Uses PyMuPDF for better PDF extraction when available
    """
    # File Size Constraint 
    MAX_FILE_SIZE   = settings.MAX_UPLOAD_SIZE

    # File Type Constraint
    ALLOWED_TYPES   = settings.ALLOWED_EXTENSIONS

    # Minimum extracted text length
    MIN_TEXT_LENGTH = settings.MIN_CONTRACT_LENGTH  
    

    @staticmethod
    def read_file(file_path_or_bytes: Union[str, Path, io.BytesIO], file_type: str = "pdf") -> str:
        """
        Read document and extract text with validation
        
        Arguments:
        ----------
            file_path_or_bytes { str / Path / BytesIO } : File path (str/Path) or bytes object (io.BytesIO)
            
            file_type                   { str }         : "pdf" or "docx"
        
        Returns:
        --------
                            { str }                     : Extracted and cleaned text
            
        Raises:
        -------
            ValueError                                  : If file type unsupported or validation fails

            Exception                                   : If extraction fails
        """
        # Normalize file_type by removing any dots and converting to lowercase
        normalized_file_type = file_type.lower().replace('.', '')

        # Validate file size
        DocumentReader._validate_file_size(file_path_or_bytes = file_path_or_bytes)

        # Use normalized file type for routing
        if (normalized_file_type == "pdf"):
            text = DocumentReader._read_pdf(file_or_bytes = file_path_or_bytes)

        elif (normalized_file_type in ["docx", "doc"]):
            text = DocumentReader._read_docx(file_or_bytes = file_path_or_bytes) 
        
        elif (normalized_file_type == "txt"):
            text = DocumentReader._read_txt(file_or_bytes = file_path_or_bytes)

        else:
            raise ValueError(f"Unsupported file type: {file_type}")
        
        # Validate extracted text
        if (len(text.strip()) < DocumentReader.MIN_TEXT_LENGTH):
            raise ValueError(f"Extracted text too short ({len(text)} chars). Minimum: {DocumentReader.MIN_TEXT_LENGTH} chars. File may be corrupted or empty.")
        
        return text

    
    @staticmethod
    def _validate_file_size(file_path_or_bytes: Union[str, Path, io.BytesIO]) -> None:
        """
        Validate file size is within limits
        """
        if isinstance(file_path_or_bytes, (str, Path)):
            size = Path(file_path_or_bytes).stat().st_size
        
        else:
            # It's a file-like object
            current_pos = file_path_or_bytes.tell()
            # Seek to end
            file_path_or_bytes.seek(0, 2)  
            
            size        = file_path_or_bytes.tell()

            # Reset to original position
            file_path_or_bytes.seek(current_pos)  
        
        if (size > DocumentReader.MAX_FILE_SIZE):
            raise ValueError(f"File too large: {size / (1024 * 1024):.1f}MB. Maximum allowed: {DocumentReader.MAX_FILE_SIZE / (1024 * 1024):.1f}MB")
        
        if (size == 0):
            raise ValueError("File is empty (0 bytes)")
    

    @staticmethod
    def _read_pdf(file_or_bytes: Union[str, Path, io.BytesIO]) -> str:
        """
        Read PDF with PyMuPDF (preferred) or PyPDF2 (fallback)
        """
        if PYMUPDF_AVAILABLE:
            try:
                return DocumentReader._read_pdf_pymupdf(file_or_bytes = file_or_bytes)
            
            except Exception as e:
                print(f"[DocumentReader] PyMuPDF failed: {e}, falling back to PyPDF2")
                return DocumentReader._read_pdf_pypdf2(file_or_bytes)
        
        else:
            return DocumentReader._read_pdf_pypdf2(file_or_bytes = file_or_bytes)
    

    @staticmethod
    def _read_pdf_pymupdf(file_or_bytes: Union[str, Path, io.BytesIO]) -> str:
        """
        Read PDF using PyMuPDF (superior text extraction)
        """
        # Handle both file paths and bytes
        if isinstance(file_or_bytes, (str, Path)):
            doc = fitz.open(file_or_bytes)
        
        else:
            # It's a file-like object
            file_or_bytes.seek(0)
            file_content = file_or_bytes.read()
            doc          = fitz.open(stream   = file_content, 
                                     filetype = "pdf",
                                    )
        
        text = ""

        for page_num in range(doc.page_count):
            page      = doc[page_num]
            
            # Extract text with layout preservation
            page_text = page.get_text("text", sort = True)
            
            # Clean the text
            page_text = DocumentReader._clean_extracted_text(text = page_text)
            text     += page_text + "\n\n"
        
        doc.close()
        
        # Post-process entire document
        text = DocumentReader._post_process_text(text = text)
        
        return text
    

    @staticmethod
    def _read_pdf_pypdf2(file_or_bytes: Union[str, Path, io.BytesIO]) -> str:
        """
        Read PDF using PyPDF2 (fallback)
        """
        try:
            # Handle both file paths and bytes
            if isinstance(file_or_bytes, (str, Path)):
                with open(file_or_bytes, 'rb') as f:
                    pdf_reader = PyPDF2.PdfReader(f)
                    text       = DocumentReader._extract_from_pypdf2(pdf_reader = pdf_reader)
            
            else:
                file_or_bytes.seek(0)
                pdf_reader = PyPDF2.PdfReader(stream = file_or_bytes)
                text       = DocumentReader._extract_from_pypdf2(pdf_reader = pdf_reader)
            
            text = DocumentReader._post_process_text(text = text)
            
            return text
            
        except Exception as e:
            raise Exception(f"PDF extraction failed: {repr(e)}")
    

    @staticmethod
    def _extract_from_pypdf2(pdf_reader: PyPDF2.PdfReader) -> str:
        """
        Extract text from PyPDF2 reader
        """
        text = ""

        for page in pdf_reader.pages:
            page_text = page.extract_text()
            
            if page_text:
                page_text = DocumentReader._clean_extracted_text(text = page_text)
                text     += page_text + "\n\n"

        return text
    

    @staticmethod
    def _read_docx(file_or_bytes: Union[str, Path, io.BytesIO]) -> str:
        """
        Read DOCX file
        """
        try:
            # Handle both file paths and bytes
            if (isinstance(file_or_bytes, (str, Path))):
                doc = Document(file_or_bytes)
            
            else:
                file_or_bytes.seek(0)
                doc = Document(file_or_bytes)
            
            text = ""
            
            # Extract paragraphs
            for paragraph in doc.paragraphs:
                if (paragraph.text.strip()):
                    clean_text = DocumentReader._clean_extracted_text(text = paragraph.text)
                    text      += clean_text + "\n"
            
            # Extract tables
            for table in doc.tables:
                for row in table.rows:
                    for cell in row.cells:
                        if cell.text.strip():
                            clean_text = DocumentReader._clean_extracted_text(text = cell.text)
                            text      += clean_text + " "

                    text += "\n"
            
            text = DocumentReader._post_process_text(text = text)
            
            return text
            
        except Exception as e:
            raise Exception(f"DOCX extraction failed: {repr(e)}")
    
    
    @staticmethod
    def _read_txt(file_or_bytes: Union[str, Path, io.BytesIO]) -> str:
        """
        Read text file with encoding detection
        """
        try:
            if isinstance(file_or_bytes, (str, Path)):
                with open(file_or_bytes, 'rb') as f:
                    content = f.read()
            
            else:
                file_or_bytes.seek(0)
                content = file_or_bytes.read()
            
            # Detect encoding
            encoding = DocumentReader.detect_encoding(content)
            
            # Decode with detected encoding, fallback to utf-8
            try:
                text = content.decode(encoding)
            
            except UnicodeDecodeError:
                text = content.decode('utf-8', errors = 'replace')
            
            return DocumentReader._post_process_text(text=text)
        
        except Exception as e:
            raise Exception(f"TXT extraction failed: {repr(e)}")


    @staticmethod
    def _clean_extracted_text(text: str) -> str:
        """
        Clean and normalize extracted text
        """
        if not text:
            return ""
        
        # Replace multiple newlines with single newline
        text = re.sub(r'\n\s*\n', '\n\n', text)
        
        # Fix hyphenated words split across lines
        text = re.sub(r'(\w+)-\s*\n\s*(\w+)', r'\1\2', text)
        
        # Normalize whitespace (but preserve single newlines)
        text = re.sub(r'[ \t]+', ' ', text)
        
        # Remove page numbers on separate lines
        text = re.sub(r'\n\s*\d+\s*\n', '\n', text)
        
        # Remove lines with just numbers
        text = re.sub(r'^\d+\s*$', '', text, flags=re.MULTILINE)
        
        return text.strip()
    

    @staticmethod
    def _post_process_text(text: str) -> str:
        """
        Post-process entire extracted text
        """
        if not text:
            return ""
        
        # Remove excessive empty lines
        text  = re.sub(r'\n{3,}', '\n\n', text)
        
        # Fix mid-sentence line breaks (lowercase to lowercase)
        text  = re.sub(r'(?<=[a-z,])\n(?=[a-z])', ' ', text)
        
        # Ensure proper spacing around section numbers
        text  = re.sub(r'(\d+\.\d+)([A-Za-z])', r'\1 \2', text)
        
        # Remove excessive spaces
        text  = re.sub(r' {2,}', ' ', text)
        
        # Remove whitespace at line starts/ends
        lines = [line.strip() for line in text.split('\n')]
        text  = '\n'.join(lines)
        
        return text.strip()
    

    @staticmethod
    def extract_metadata(file_path_or_bytes: Union[str, Path, io.BytesIO], file_type: str = "pdf") -> Dict[str, Any]:
        """
        Extract document metadata (pages, author, creation date, etc.)
        
        Arguments:
        ----------
            file_path_or_bytes { str / Path / BytesIO } : File path or bytes object
            
            file_type                  { str }          : "pdf" or "docx"
        
        Returns:
        --------
                        { dict }                        : Dictionary containing metadata
        """
        metadata = {"pages"     : 0,
                    "title"     : "",
                    "author"    : "",
                    "creator"   : "",
                    "created"   : "",
                    "modified"  : "",
                    "file_type" : file_type,
                   }
        
        try:
            if ((file_type == "pdf") and PYMUPDF_AVAILABLE):
                if isinstance(file_path_or_bytes, (str, Path)):
                    doc = fitz.open(file_path_or_bytes)
                
                else:
                    file_path_or_bytes.seek(0)
                    file_content = file_path_or_bytes.read()
                    doc          = fitz.open(stream   = file_content, 
                                             filetype = "pdf",
                                            )
                
                metadata.update({"pages"    : doc.page_count,
                                 "title"    : doc.metadata.get("title", ""),
                                 "author"   : doc.metadata.get("author", ""),
                                 "creator"  : doc.metadata.get("creator", ""),
                                 "created"  : doc.metadata.get("creationDate", ""),
                                 "modified" : doc.metadata.get("modDate", ""),
                               })

                doc.close()
            
            elif (file_type in ["docx", "doc"]):
                if (isinstance(file_path_or_bytes, (str, Path))):
                    doc = Document(file_path_or_bytes)
                
                else:
                    file_path_or_bytes.seek(0)
                    doc = Document(file_path_or_bytes)
                
                core_props = doc.core_properties
                metadata.update({"pages"    : len(doc.sections),
                                 "title"    : core_props.title or "",
                                 "author"   : core_props.author or "",
                                 "creator"  : core_props.author or "",
                                 "created"  : str(core_props.created) if core_props.created else "",
                                 "modified" : str(core_props.modified) if core_props.modified else "",
                               })
        
        except Exception as e:
            print(f"[DocumentReader] Metadata extraction failed: {repr(e)}")
        
        return metadata

    
    @staticmethod
    def detect_encoding(file_bytes: bytes) -> str:
        """
        Detect text encoding for better extraction
        
        Arguments:
        ----------
            file_bytes { bytes } : Raw file bytes
        
        Returns:
        --------
                { str }          : Detected encoding (e.g., 'utf-8', 'latin-1')
        """
        if not CHARDET_AVAILABLE:
            return 'utf-8'
        
        try:
            # Check first 10KB
            result = chardet.detect(file_bytes[:10000])  
            return result['encoding'] or 'utf-8'
        
        except Exception:
            return 'utf-8'
    

    @staticmethod
    def validate_file_integrity(file_path: Union[str, Path]) -> tuple[bool, str]:
        """
        Validate file isn't corrupted and is readable
        
        Arguments:
        ----------
            file_path { str } : Path to file
        
        Returns:
        --------
               { tuple }      : (is_valid, message) tuple
        """
        try:
            file_path = Path(file_path)
            
            # Check file exists
            if not file_path.exists():
                return False, "File does not exist"
            
            # Check file size
            file_size = file_path.stat().st_size
            
            if (file_size == 0):
                return (False, "File is empty (0 bytes)")
            
            # Less than 1KB
            if (file_size < 1024):  
                return (False, f"File suspiciously small ({file_size} bytes)")
            
            # Check file is readable
            with open(file_path, 'rb') as f:
                # Try reading first KB
                f.read(1024)  
            
            return (True, "File integrity OK")
            
        except PermissionError:
            return (False, "Permission denied - cannot read file")
        
        except Exception as e:
            return (False, f"File integrity check failed: {repr(e)}")