File size: 3,100 Bytes
aeab25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import logging
from typing import List
 
logger = logging.getLogger(__name__)
 
 
def load_text_file(filepath: str) -> str:
    """Load plain text file and return content."""
    with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
        return f.read()
 
 
def load_pdf_file(filepath: str) -> str:
    """Extract text from PDF using pypdf."""
    try:
        from pypdf import PdfReader
        reader = PdfReader(filepath)
        return "\n".join(page.extract_text() or "" for page in reader.pages)
    except Exception as e:
        logger.error(f"PDF extraction failed: {e}")
        raise RuntimeError(f"Could not read PDF: {e}")
 
 
def load_document(filepath: str) -> str:
    """Load a .txt or .pdf file and return raw text."""
    ext = os.path.splitext(filepath)[1].lower()
    if ext == ".txt":
        return load_text_file(filepath)
    elif ext == ".pdf":
        return load_pdf_file(filepath)
    else:
        raise ValueError(f"Unsupported file format: {ext}")
 
 
def clean_text(text: str) -> str:
    """Normalize whitespace and remove non-printable characters."""
    text = re.sub(r'\s+', ' ', text)
    text = re.sub(r'[^\x20-\x7E\n]', '', text)
    return text.strip()
 
 
def chunk_text(text: str, chunk_size: int, chunk_overlap: int, min_chunk_size: int) -> List[str]:
    """
    Split text into overlapping chunks.
    Tries to split on sentence boundaries first, falls back to hard split.
    """
    sentences = re.split(r'(?<=[.!?])\s+', text)
 
    chunks = []
    current_chunk = ""
 
    for sentence in sentences:
        if len(current_chunk) + len(sentence) + 1 <= chunk_size:
            current_chunk += (" " if current_chunk else "") + sentence
        else:
            if len(current_chunk.strip()) >= min_chunk_size:
                chunks.append(current_chunk.strip())
            overlap_text = current_chunk[-chunk_overlap:] if len(current_chunk) > chunk_overlap else current_chunk
            current_chunk = overlap_text + (" " if overlap_text else "") + sentence
 
    if len(current_chunk.strip()) >= min_chunk_size:
        chunks.append(current_chunk.strip())
 
    logger.info(f"Text split into {len(chunks)} chunks")
    return chunks
 
 
def format_context(chunks: List[str]) -> str:
    """Format retrieved chunks into a single context string for the LLM."""
    return "\n\n---\n\n".join(
        f"[Excerpt {i+1}]:\n{chunk}" for i, chunk in enumerate(chunks)
    )
 
 
def validate_file(filepath: str, max_size_mb: float) -> None:
    """Validate file exists, has a supported extension, and is within size limit."""
    from config import SUPPORTED_FORMATS
    if not os.path.exists(filepath):
        raise FileNotFoundError(f"File not found: {filepath}")
    ext = os.path.splitext(filepath)[1].lower()
    if ext not in SUPPORTED_FORMATS:
        raise ValueError(f"Unsupported format '{ext}'. Supported: {SUPPORTED_FORMATS}")
    size_mb = os.path.getsize(filepath) / (1024 * 1024)
    if size_mb > max_size_mb:
        raise ValueError(f"File too large ({size_mb:.1f} MB). Limit: {max_size_mb} MB")