Initial commit
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
4ecf7b351d
143 files changed
+24156
No files matched your search
@@ -0,0 +1,133 @@
|
||||
"""
|
||||
Document sampling strategy for large files
|
||||
"""
|
||||
|
||||
from typing import List, Dict, Any
|
||||
from ..core.config import TestsetConfig
|
||||
from ..utils.helpers import calculate_file_size_mb
|
||||
from .document_loader import Document
|
||||
|
||||
|
||||
class SamplingStrategy:
|
||||
"""Document sampling strategy"""
|
||||
|
||||
def __init__(self, config: TestsetConfig):
|
||||
self.config = config
|
||||
self.small_threshold = config.sampling_strategy['small_file_threshold_mb']
|
||||
self.medium_threshold = config.sampling_strategy['medium_file_threshold_mb']
|
||||
self.small_rate = config.sampling_strategy['small_file_sampling_rate']
|
||||
self.medium_rate = config.sampling_strategy['medium_file_sampling_rate']
|
||||
self.large_rate = config.sampling_strategy['large_file_sampling_rate']
|
||||
|
||||
def get_sampling_rate(self, file_size_bytes: int) -> float:
|
||||
"""
|
||||
Determine sampling rate based on file size
|
||||
|
||||
Args:
|
||||
file_size_bytes: File size in bytes
|
||||
|
||||
Returns:
|
||||
Sampling rate (0.0 to 1.0)
|
||||
"""
|
||||
file_size_mb = calculate_file_size_mb(file_size_bytes)
|
||||
|
||||
if file_size_mb < self.small_threshold:
|
||||
return self.small_rate
|
||||
elif file_size_mb < self.medium_threshold:
|
||||
return self.medium_rate
|
||||
else:
|
||||
return self.large_rate
|
||||
|
||||
def sample_documents(self, documents: List[Document]) -> List[Document]:
|
||||
"""
|
||||
Sample documents based on file size
|
||||
|
||||
Args:
|
||||
documents: List of documents to sample
|
||||
|
||||
Returns:
|
||||
Sampled list of documents
|
||||
"""
|
||||
sampled_docs = []
|
||||
|
||||
for doc in documents:
|
||||
file_size = doc.metadata.get('file_size', 0)
|
||||
sampling_rate = self.get_sampling_rate(file_size)
|
||||
|
||||
# For now, simple implementation: include document if sampling rate >= 1.0
|
||||
# In production, implement more sophisticated sampling (e.g., stratified sampling)
|
||||
if sampling_rate >= 1.0:
|
||||
sampled_docs.append(doc)
|
||||
elif sampling_rate > 0:
|
||||
# TODO: Implement probabilistic or stratified sampling
|
||||
# For now, include all documents with sampling_rate > 0
|
||||
sampled_docs.append(doc)
|
||||
|
||||
return sampled_docs
|
||||
|
||||
|
||||
class DocumentChunker:
|
||||
"""Document chunking for large documents"""
|
||||
|
||||
def __init__(self, chunk_size: int = 1000, chunk_overlap: int = 200):
|
||||
"""
|
||||
Initialize document chunker
|
||||
|
||||
Args:
|
||||
chunk_size: Size of each chunk in characters
|
||||
chunk_overlap: Overlap between chunks in characters
|
||||
"""
|
||||
self.chunk_size = chunk_size
|
||||
self.chunk_overlap = chunk_overlap
|
||||
|
||||
def chunk_document(self, document: Document) -> List[Document]:
|
||||
"""
|
||||
Split document into chunks
|
||||
|
||||
Args:
|
||||
document: Document to chunk
|
||||
|
||||
Returns:
|
||||
List of chunked documents
|
||||
"""
|
||||
content = document.page_content
|
||||
chunks = []
|
||||
|
||||
start = 0
|
||||
chunk_index = 0
|
||||
|
||||
while start < len(content):
|
||||
end = start + self.chunk_size
|
||||
chunk_content = content[start:end]
|
||||
|
||||
# Create new document for chunk
|
||||
chunk_metadata = document.metadata.copy()
|
||||
chunk_metadata['chunk_index'] = chunk_index
|
||||
chunk_metadata['chunk_start'] = start
|
||||
chunk_metadata['chunk_end'] = end
|
||||
|
||||
chunks.append(Document(content=chunk_content, metadata=chunk_metadata))
|
||||
|
||||
# Move to next chunk with overlap
|
||||
start = end - self.chunk_overlap
|
||||
chunk_index += 1
|
||||
|
||||
return chunks
|
||||
|
||||
def chunk_documents(self, documents: List[Document]) -> List[Document]:
|
||||
"""
|
||||
Chunk multiple documents
|
||||
|
||||
Args:
|
||||
documents: List of documents to chunk
|
||||
|
||||
Returns:
|
||||
List of all chunks from all documents
|
||||
"""
|
||||
all_chunks = []
|
||||
|
||||
for doc in documents:
|
||||
chunks = self.chunk_document(doc)
|
||||
all_chunks.extend(chunks)
|
||||
|
||||
return all_chunks
|
||||
Reference in new issue
Block a user