-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdocument_processor.py
More file actions
75 lines (60 loc) · 2.34 KB
/
document_processor.py
File metadata and controls
75 lines (60 loc) · 2.34 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
import os
from typing import List, cast
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.schema import Document
class DocumentProcessor:
"""Handles document loading and chunking."""
def __init__(self, chunk_size: int = 1000, chunk_overlap: int = 200):
"""
Initialize the document processor.
Args:
chunk_size: Size of chunks in characters
chunk_overlap: Overlap between chunks in characters
"""
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=self.chunk_size,
chunk_overlap=self.chunk_overlap,
length_function=len,
)
def load_pdf(self, file_path: str) -> List[Document]:
"""
Load a PDF document and split it into chunks.
Args:
file_path: Path to the PDF file
Returns:
List of document chunks with metadata
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
loader = PyPDFLoader(file_path)
try:
documents = loader.load()
# Add filename to metadata
filename = os.path.basename(file_path)
for doc in documents:
doc.metadata["source"] = filename
return documents
except Exception as e:
raise Exception(f"Error loading PDF: {str(e)}")
def chunk_documents(self, documents: List[Document]) -> List[Document]:
"""
Split documents into smaller chunks.
Args:
documents: List of documents to chunk
Returns:
List of chunked documents
"""
return self.text_splitter.split_documents(documents)
def process_document(self, file_path: str) -> List[Document]:
"""
Process a document: load and chunk it.
Args:
file_path: Path to the document
Returns:
List of processed document chunks
"""
documents = self.load_pdf(file_path)
return self.chunk_documents(documents)