-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_processor.py
More file actions
425 lines (354 loc) · 16.1 KB
/
Copy pathdocument_processor.py
File metadata and controls
425 lines (354 loc) · 16.1 KB
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
"""
Document Processor Module
Handles extraction and chunking of PDF, DOCX, and PPTX files.
"""
import hashlib
import os
import re
import logging
from typing import List, Tuple, Optional
from dataclasses import dataclass
from pathlib import Path
logger = logging.getLogger(__name__)
ABBREVIATIONS = frozenset({
'dr', 'mr', 'mrs', 'ms', 'prof', 'jr', 'sr', 'st', 'ave', 'blvd',
'dept', 'rev', 'vol', 'fig', 'ed', 'eds', 'repr', 'trans', 'pt',
'ch', 'sec', 'app', 'ex', 'cf', 'eg', 'ie', 'etc', 'approx',
'esp', 'viz', 'al', 'vs', 'inc', 'corp', 'ltd', 'govt', 'est',
'acct', 'tel', 'ref',
})
@dataclass
class DocumentChunk:
"""Represents a chunk of text from a document."""
text: str
source: str
page: Optional[int] = None
chunk_index: int = 0
doc_id: Optional[str] = None # Stable hash-based document identifier
source_path: Optional[str] = None # Full file path for deduplication
class DocumentProcessor:
"""Processes various document formats and extracts text."""
SUPPORTED_EXTENSIONS = {".pdf", ".docx", ".doc", ".pptx", ".ppt", ".txt", ".md", ".xlsx"}
def __init__(self, chunk_size: int = 256, chunk_overlap: int = 100):
if chunk_size <= 0:
raise ValueError(f"chunk_size must be positive, got {chunk_size}")
if chunk_overlap < 0:
raise ValueError(f"chunk_overlap must be non-negative, got {chunk_overlap}")
if chunk_overlap >= chunk_size:
raise ValueError(
f"chunk_overlap ({chunk_overlap}) must be less than chunk_size ({chunk_size})"
)
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
def extract_pdf(self, filepath: str) -> Tuple[str, List[Tuple[int, str]]]:
"""Extract text from PDF with page information."""
try:
import pdfplumber
pages_text = []
full_text = []
with pdfplumber.open(filepath) as pdf:
for i, page in enumerate(pdf.pages, 1):
text = page.extract_text() or ""
if text.strip():
pages_text.append((i, text))
full_text.append(text)
return "\n\n".join(full_text), pages_text
except ImportError:
logger.warning(
"pdfplumber not installed. Falling back to pypdf for PDF extraction. "
"Consider installing pdfplumber for better extraction quality: pip install pdfplumber"
)
from pypdf import PdfReader
reader = PdfReader(filepath)
pages_text = []
full_text = []
for i, page in enumerate(reader.pages, 1):
text = page.extract_text() or ""
if text.strip():
pages_text.append((i, text))
full_text.append(text)
return "\n\n".join(full_text), pages_text
def _extract_table_rows(self, table) -> List[str]:
"""Extract text from table rows."""
rows = []
for row in table.rows:
row_text = [cell.text.strip() for cell in row.cells if cell.text.strip()]
if row_text:
rows.append(" | ".join(row_text))
return rows
def extract_docx(self, filepath: str) -> str:
"""Extract text from DOCX file."""
from docx import Document
doc = Document(filepath)
paragraphs = []
for para in doc.paragraphs:
if para.text.strip():
paragraphs.append(para.text)
for table in doc.tables:
paragraphs.extend(self._extract_table_rows(table))
return "\n\n".join(paragraphs)
def extract_pptx(self, filepath: str) -> str:
"""Extract text from PPTX file."""
from pptx import Presentation
prs = Presentation(filepath)
slides_text = []
for slide_num, slide in enumerate(prs.slides, 1):
slide_content = [f"[Slide {slide_num}]"]
for shape in slide.shapes:
if hasattr(shape, "text") and shape.text.strip():
slide_content.append(shape.text)
if hasattr(shape, "has_table") and shape.has_table:
slide_content.extend(self._extract_table_rows(shape.table))
if len(slide_content) > 1:
slides_text.append("\n".join(slide_content))
return "\n\n".join(slides_text)
def extract_xlsx(self, filepath: str) -> str:
"""Extract text from Excel .xlsx files, preserving sheet and row structure."""
import openpyxl
wb = openpyxl.load_workbook(filepath, data_only=True)
sheets_text = []
for sheet in wb.worksheets:
rows = []
for row in sheet.iter_rows(values_only=True):
cells = [str(cell) for cell in row if cell is not None]
row_text = " | ".join(cells)
if row_text.strip():
rows.append(row_text)
if rows:
sheets_text.append(f"[Sheet: {sheet.title}]\n" + "\n".join(rows))
return "\n\n".join(sheets_text)
def extract_text_file(self, filepath: str) -> str:
"""Extract text from plain text or markdown files."""
encodings = ["utf-8", "utf-16", "latin-1", "cp1252"]
for encoding in encodings:
try:
with open(filepath, "r", encoding=encoding) as f:
return f.read()
except (UnicodeDecodeError, UnicodeError):
continue
with open(filepath, "r", encoding="utf-8", errors="replace") as f:
return f.read()
def extract_document(self, filepath: str) -> Tuple[str, List[Tuple[int, str]]]:
"""Extract text from any supported document type.
Returns (full_text, pages) where pages is [(page_num, page_text), ...].
For non-PDF formats, pages is empty.
"""
filepath = str(filepath)
ext = Path(filepath).suffix.lower()
if ext == ".pdf":
return self.extract_pdf(filepath)
elif ext in {".docx", ".doc"}:
return self.extract_docx(filepath), []
elif ext in {".pptx", ".ppt"}:
return self.extract_pptx(filepath), []
elif ext == ".xlsx":
return self.extract_xlsx(filepath), []
elif ext in {".txt", ".md"}:
return self.extract_text_file(filepath), []
else:
raise ValueError(f"Unsupported file format: {ext}")
def clean_text(self, text: str) -> str:
"""Clean and normalize text while preserving paragraph and list structure."""
text = str(text)
# Step 1: Normalize line endings to \n
text = text.replace("\r\n", "\n").replace("\r", "\n")
# Step 2: Collapse runs of 3+ blank lines to exactly 2 (paragraph break)
text = re.sub(r"\n{3,}", "\n\n", text)
# Step 3: Collapse horizontal whitespace (spaces/tabs) within each line,
# but DO NOT collapse \n characters
lines = text.split("\n")
lines = [re.sub(r"[ \t]+", " ", line).strip() for line in lines]
text = "\n".join(lines)
# Step 4: Remove empty lines that are not paragraph breaks
# (single blank lines between non-empty lines are preserved as \n\n)
text = re.sub(r"\n{2,}", "\n\n", text)
return text.strip()
def _split_sentences(self, paragraph: str) -> List[str]:
"""Split paragraph into sentences, respecting common abbreviations."""
protected = paragraph
for abbr in ABBREVIATIONS:
protected = re.sub(
rf'\b{abbr}\.',
f'{abbr}\x00',
protected,
flags=re.IGNORECASE,
)
def _protect_initial(m):
return m.group(1) + '\x00'
protected = re.sub(r'\b([A-Z])\.', _protect_initial, protected)
sentences = re.split(r'(?<=[.!?])\s+', protected)
return [s.replace('\x00', '.').strip() for s in sentences if s.strip()]
def _calculate_overlap(
self, sentences: List[str], overlap_size: int
) -> Tuple[List[str], int]:
"""Calculate sentences to keep for overlap."""
overlap_sentences = []
overlap_word_count = 0
for s in reversed(sentences):
s_word_count = len(s.split())
if overlap_word_count + s_word_count <= overlap_size:
overlap_sentences.insert(0, s)
overlap_word_count += s_word_count
else:
break
return overlap_sentences, overlap_word_count
def chunk_text(self, text: str, source: str, pages: Optional[List[Tuple[int, str]]] = None) -> List[DocumentChunk]:
"""Split text into overlapping chunks respecting paragraph and sentence boundaries."""
text = self.clean_text(text)
# Build page mapping from PDF pages
para_page_map: dict = {}
if pages:
for page_num, page_text in pages:
cleaned = re.sub(r"\s+", " ", page_text.strip())
for segment in cleaned.split("\n\n"):
seg = segment.strip()
if seg:
para_page_map[seg[:80]] = page_num
def _find_page(chunk_text: str) -> Optional[int]:
"""Find the page number for a chunk text using prefix matching."""
if not para_page_map:
return None
# Try longest prefix match first
for length in range(len(chunk_text), 0, -1):
prefix = chunk_text[:length].strip()
if prefix in para_page_map:
return para_page_map[prefix]
return None
paragraphs = [p.strip() for p in text.split("\n\n") if p.strip()]
chunks = []
chunk_index = 0
current_chunk_sentences = []
current_chunk_word_count = 0
for paragraph in paragraphs:
sentences = self._split_sentences(paragraph)
for sentence in sentences:
sentence = sentence.strip()
if not sentence:
continue
sentence_word_count = len(sentence.split())
# If sentence alone exceeds chunk_size and we're starting fresh
if (
sentence_word_count > self.chunk_size
and not current_chunk_sentences
):
# Split sentence into word chunks
words = sentence.split()
while words:
chunk_words = words[: self.chunk_size]
chunk_text = " ".join(chunk_words)
chunks.append(
DocumentChunk(
text=chunk_text, source=source, chunk_index=chunk_index,
page=_find_page(chunk_text),
)
)
chunk_index += 1
words = words[self.chunk_size :]
if words:
# Overlap handling for split sentence
overlap_words = (
chunk_words[-self.chunk_overlap // 2 :]
if self.chunk_overlap > 0
else []
)
words = overlap_words + words
continue # Move to next sentence
# If adding this sentence would exceed chunk_size, finalize current chunk
if current_chunk_sentences and (
current_chunk_word_count + sentence_word_count > self.chunk_size
):
chunk_text = " ".join(current_chunk_sentences)
chunks.append(
DocumentChunk(
text=chunk_text, source=source, chunk_index=chunk_index,
page=_find_page(chunk_text),
)
)
chunk_index += 1
# Handle overlap using helper method
overlap_sentences, overlap_word_count = self._calculate_overlap(
current_chunk_sentences, self.chunk_overlap
)
current_chunk_sentences = overlap_sentences
current_chunk_word_count = overlap_word_count
current_chunk_sentences.append(sentence)
current_chunk_word_count += sentence_word_count
# Don't forget the last chunk
if current_chunk_sentences:
chunk_text = " ".join(current_chunk_sentences)
chunks.append(
DocumentChunk(text=chunk_text, source=source, chunk_index=chunk_index,
page=_find_page(chunk_text))
)
return chunks
def process_file(
self, filepath: str, source_name: Optional[str] = None
) -> List[DocumentChunk]:
"""Process a single file and return chunks."""
filepath = str(filepath)
# Use provided source_name or fall back to filename from path
filename = source_name if source_name else Path(filepath).name
canonical_path = str(Path(filepath).resolve())
doc_id = hashlib.sha256(canonical_path.encode()).hexdigest()[:16]
try:
text, pages = self.extract_document(filepath)
if not text.strip():
logger.warning("Empty content: %s", filename)
return []
chunks = self.chunk_text(text, filename, pages=pages)
for chunk in chunks:
chunk.doc_id = doc_id
chunk.source_path = canonical_path
logger.info("Processed: %s (%d chunks)", filename, len(chunks))
return chunks
except ValueError as e:
# Known error (unsupported format, etc.)
logger.error("Failed to process %s: %s", filename, e)
return []
except Exception as e:
# Unexpected error - log full exception details
logger.exception("Unexpected error processing %s", filename)
return []
def process_directory(self, directory: str) -> List[DocumentChunk]:
"""Process all supported documents in a directory recursively."""
all_chunks = []
directory = Path(directory)
# Get max file size from environment variable (default 100MB)
try:
max_file_size_mb = int(os.environ.get("RAG_MAX_FILE_SIZE", "100"))
if max_file_size_mb <= 0:
max_file_size_mb = 100
except ValueError:
max_file_size_mb = 100
max_file_size_bytes = max_file_size_mb * 1024 * 1024
skipped_files = []
for root, _, files in os.walk(directory):
for file in files:
filepath = Path(root) / file
ext = filepath.suffix.lower()
if ext in self.SUPPORTED_EXTENSIONS:
# Check file size before processing
file_size = filepath.stat().st_size
if file_size > max_file_size_bytes:
size_mb = file_size / (1024 * 1024)
skipped_files.append((filepath.name, size_mb))
logger.info("Skipping %s: %.1fMB > %.0fMB limit", filepath.name, size_mb, max_file_size_mb)
continue
chunks = self.process_file(str(filepath))
all_chunks.extend(chunks)
if skipped_files:
logger.info("Skipped %d file(s) exceeding %.0fMB limit", len(skipped_files), max_file_size_mb)
logger.info("Total: %d chunks from %s", len(all_chunks), directory)
return all_chunks
if __name__ == "__main__":
processor = DocumentProcessor(chunk_size=512, chunk_overlap=50)
import sys
if len(sys.argv) > 1:
path = sys.argv[1]
if os.path.isdir(path):
chunks = processor.process_directory(path)
else:
chunks = processor.process_file(path)
logger.info("Extracted %d chunks", len(chunks))
if chunks:
logger.info("First chunk preview:\n%s...", chunks[0].text[:200])