Spaces:
Runtime error

File size: 2,043 Bytes
18866f9
 
 
 
 
e99563f
 
 
 
 
 
 
 
 
 
06430dd
e99563f
06430dd
 
e99563f
 
 
06430dd
e99563f
 
 
06430dd
e99563f
06430dd
 
e99563f
 
 
 
 
 
e2818fe
e99563f
 
 
 
 
 
 
 
 
 
 
 
 
18866f9
e99563f
 
 
e2818fe
e99563f
 
 
06430dd
 
 
 
 
 
e99563f
06430dd
18866f9
e99563f
 
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
import re
import logging

logger = logging.getLogger("rag-pipeline.text_processing")

def chunk_text(text: str, chunk_size: int = 1000, overlap: int = 200) -> list[str]:
    """Split text into overlapping chunks"""
    if not text.strip():
        return []
    
    words = text.split()
    chunks = []
    start = 0
    
    while start < len(words):
        # Calculate end position
        end = start + chunk_size
        
        # Get chunk words
        chunk_words = words[start:end]
        chunk_text = " ".join(chunk_words)
        
        # Add chunk if it has content
        if chunk_text.strip():
            chunks.append(chunk_text)
        
        # Move start position (with overlap)
        start = end - overlap
        
        # Break if we've processed all words
        if end >= len(words):
            break
    
    return chunks

def extract_pdf_text(file_path: str) -> str:
    """Extract text from PDF file"""
    try:
        from pypdf import PdfReader
        
        reader = PdfReader(file_path)
        text_content = ""
        
        for page in reader.pages:
            page_text = page.extract_text()
            if page_text:
                text_content += page_text + "\n"
        
        return text_content.strip()
    except Exception as e:
        logger.error(f"Error extracting PDF text: {e}")
        return ""

def clean_text(text: str) -> str:
    """Rigorous text cleaning to resolve 's p a c e d' character issues from PDFs"""
    if not text:
        return ""
    
    # Use a more aggressive approach to heal separated characters.
    # While there's a space between two single characters, join them.
    # We do this up to 10 times to handle long words like "P R O F E S S I O N A L".
    for _ in range(10):
        # Replaces Space + Char + Space with Space + Char (essentially collapsing)
        text = re.sub(r'(\s|^)([a-zA-Z0-9])\s(?=[a-zA-Z0-9](\s|$))', r'\1\2', text)
    
    # Final cleanup of extra whitespace
    text = " ".join(text.split())
    
    return text