Spaces:
Sleeping
Sleeping
File size: 10,881 Bytes
9222df3 | 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 | #!/usr/bin/env python3
"""
Text Chunker for Scikit-learn Documentation
This module processes the scraped Scikit-learn documentation and chunks it
into smaller, manageable pieces for use in a RAG application.
Author: AI Assistant
Date: September 2025
"""
import json
import logging
from typing import Dict, List, Any
from pathlib import Path
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class DocumentChunker:
"""
A class for chunking scraped documentation into smaller pieces.
This class handles the process of splitting long documents into
manageable chunks while preserving metadata and context.
"""
def __init__(
self,
chunk_size: int = 1000,
chunk_overlap: int = 150,
separators: List[str] = None
):
"""
Initialize the DocumentChunker.
Args:
chunk_size (int): Target size for each chunk in characters
chunk_overlap (int): Number of characters to overlap between chunks
separators (List[str]): Custom separators for text splitting
"""
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
# Use custom separators or defaults optimized for documentation
if separators is None:
separators = [
"\n\n", # Double newlines (paragraphs)
"\n", # Single newlines
". ", # Sentences
"! ", # Exclamations
"? ", # Questions
"; ", # Semicolons
", ", # Commas
" ", # Spaces
"" # Characters (last resort)
]
# Initialize the text splitter
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
separators=separators,
length_function=len,
)
logger.info(f"Initialized chunker with size={chunk_size}, overlap={chunk_overlap}")
def load_scraped_content(self, filepath: str) -> List[Dict[str, str]]:
"""
Load scraped content from JSON file.
Args:
filepath (str): Path to the scraped content JSON file
Returns:
List[Dict[str, str]]: List of documents with 'url' and 'text' keys
Raises:
FileNotFoundError: If the file doesn't exist
json.JSONDecodeError: If the file is not valid JSON
"""
filepath = Path(filepath)
if not filepath.exists():
raise FileNotFoundError(f"Scraped content file not found: {filepath}")
logger.info(f"Loading scraped content from {filepath}")
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = json.load(f)
logger.info(f"Loaded {len(content)} documents from {filepath}")
return content
except json.JSONDecodeError as e:
logger.error(f"Invalid JSON in {filepath}: {e}")
raise
except Exception as e:
logger.error(f"Error loading {filepath}: {e}")
raise
def create_chunk_metadata(self, original_url: str, chunk_index: int) -> Dict[str, Any]:
"""
Create metadata for a chunk.
Args:
original_url (str): URL of the original document
chunk_index (int): Index of this chunk within the document
Returns:
Dict[str, Any]: Metadata dictionary
"""
return {
"url": original_url,
"chunk_index": chunk_index,
"source": "scikit-learn-docs"
}
def chunk_document(self, document: Dict[str, str]) -> List[Dict[str, Any]]:
"""
Chunk a single document into smaller pieces.
Args:
document (Dict[str, str]): Document with 'url' and 'text' keys
Returns:
List[Dict[str, Any]]: List of chunks with 'page_content' and 'metadata' keys
"""
url = document['url']
text = document['text']
# Skip documents with minimal content
if len(text.strip()) < 100:
logger.warning(f"Skipping document with minimal content: {url}")
return []
logger.info(f"Chunking document: {url} ({len(text)} characters)")
# Split the text into chunks
text_chunks = self.text_splitter.split_text(text)
# Create chunk objects with metadata
chunks = []
for i, chunk_text in enumerate(text_chunks):
# Skip very small chunks
if len(chunk_text.strip()) < 50:
continue
chunk = {
"page_content": chunk_text.strip(),
"metadata": self.create_chunk_metadata(url, i)
}
chunks.append(chunk)
logger.info(f"Created {len(chunks)} chunks from {url}")
return chunks
def chunk_all_documents(self, documents: List[Dict[str, str]]) -> List[Dict[str, Any]]:
"""
Chunk all documents in the collection.
Args:
documents (List[Dict[str, str]]): List of documents to chunk
Returns:
List[Dict[str, Any]]: List of all chunks from all documents
"""
logger.info(f"Starting to chunk {len(documents)} documents")
all_chunks = []
total_chars_processed = 0
for doc_index, document in enumerate(documents, 1):
try:
doc_chunks = self.chunk_document(document)
all_chunks.extend(doc_chunks)
# Track progress
total_chars_processed += len(document['text'])
if doc_index % 10 == 0:
logger.info(f"Processed {doc_index}/{len(documents)} documents")
except Exception as e:
logger.error(f"Error chunking document {document.get('url', 'unknown')}: {e}")
continue
logger.info(f"Chunking completed:")
logger.info(f" - Documents processed: {len(documents)}")
logger.info(f" - Total chunks created: {len(all_chunks)}")
logger.info(f" - Total characters processed: {total_chars_processed:,}")
logger.info(f" - Average chunks per document: {len(all_chunks) / len(documents):.1f}")
return all_chunks
def save_chunks(self, chunks: List[Dict[str, Any]], filepath: str):
"""
Save chunks to a JSON file.
Args:
chunks (List[Dict[str, Any]]): List of chunks to save
filepath (str): Output file path
"""
filepath = Path(filepath)
logger.info(f"Saving {len(chunks)} chunks to {filepath}")
try:
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(chunks, f, indent=2, ensure_ascii=False)
# Calculate file size
file_size = filepath.stat().st_size / (1024 * 1024) # MB
logger.info(f"Chunks saved successfully ({file_size:.2f} MB)")
except Exception as e:
logger.error(f"Error saving chunks to {filepath}: {e}")
raise
def get_chunk_statistics(self, chunks: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Calculate statistics about the chunks.
Args:
chunks (List[Dict[str, Any]]): List of chunks
Returns:
Dict[str, Any]: Statistics dictionary
"""
if not chunks:
return {}
chunk_lengths = [len(chunk['page_content']) for chunk in chunks]
unique_urls = set(chunk['metadata']['url'] for chunk in chunks)
stats = {
"total_chunks": len(chunks),
"unique_documents": len(unique_urls),
"avg_chunk_length": sum(chunk_lengths) / len(chunk_lengths),
"min_chunk_length": min(chunk_lengths),
"max_chunk_length": max(chunk_lengths),
"total_characters": sum(chunk_lengths)
}
return stats
def process_and_save(
self,
input_filepath: str,
output_filepath: str = "chunks.json"
) -> Dict[str, Any]:
"""
Complete processing pipeline: load, chunk, and save.
Args:
input_filepath (str): Path to scraped content JSON
output_filepath (str): Path to save chunks JSON
Returns:
Dict[str, Any]: Processing statistics
"""
logger.info("Starting document chunking pipeline")
# Load scraped content
documents = self.load_scraped_content(input_filepath)
# Chunk all documents
chunks = self.chunk_all_documents(documents)
# Save chunks
self.save_chunks(chunks, output_filepath)
# Calculate and return statistics
stats = self.get_chunk_statistics(chunks)
logger.info("Chunking pipeline completed successfully")
return stats
def main():
"""
Main function to run the chunking process.
"""
print("Scikit-learn Documentation Chunker")
print("=" * 50)
# Configuration
input_file = "scraped_content.json"
output_file = "chunks.json"
# Initialize chunker with optimal settings for documentation
chunker = DocumentChunker(
chunk_size=1000,
chunk_overlap=150
)
try:
# Process documents
stats = chunker.process_and_save(input_file, output_file)
# Display results
print(f"\nProcessing Results:")
print(f" π Total chunks created: {stats['total_chunks']:,}")
print(f" π Unique documents: {stats['unique_documents']}")
print(f" π Average chunk length: {stats['avg_chunk_length']:.0f} characters")
print(f" π Min/Max chunk length: {stats['min_chunk_length']}/{stats['max_chunk_length']}")
print(f" πΎ Total characters: {stats['total_characters']:,}")
print(f" β
Chunks saved to: {output_file}")
except Exception as e:
logger.error(f"Pipeline failed: {e}")
print(f"\nβ Error: {e}")
return 1
return 0
if __name__ == "__main__":
exit(main()) |