File size: 11,792 Bytes
4a0fab5 9145e48 |
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 |
# chunker.py
import logging
from typing import List, Dict, Any, Optional
import re
from .models import Chunk
from .text_preprocessor import TextPreprocessor
import config
logger = logging.getLogger(__name__)
class TextChunker:
def __init__(self):
self.config = config.config
self.preprocessor = TextPreprocessor()
self.chunk_size = self.config.CHUNK_SIZE
self.chunk_overlap = self.config.CHUNK_OVERLAP
def chunk_document(self, document_id: str, content: str, method: str = "recursive") -> List[Chunk]:
"""Chunk a document using the specified method"""
if not content:
return []
try:
if method == "recursive":
return self._recursive_chunk(document_id, content)
elif method == "sentence":
return self._sentence_chunk(document_id, content)
elif method == "paragraph":
return self._paragraph_chunk(document_id, content)
elif method == "fixed":
return self._fixed_chunk(document_id, content)
else:
logger.warning(f"Unknown chunking method: {method}, using recursive")
return self._recursive_chunk(document_id, content)
except Exception as e:
logger.error(f"Error chunking document: {str(e)}")
# Fallback to simple fixed chunking
return self._fixed_chunk(document_id, content)
def _recursive_chunk(self, document_id: str, content: str) -> List[Chunk]:
"""Recursively split text by different separators"""
chunks = []
# Define separators in order of preference
separators = [
"\n\n", # Paragraphs
"\n", # Lines
". ", # Sentences
", ", # Clauses
" " # Words
]
def split_text(text: str, separators: List[str], chunk_size: int) -> List[str]:
if len(text) <= chunk_size:
return [text] if text.strip() else []
if not separators:
# If no separators left, split by character
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
separator = separators[0]
remaining_separators = separators[1:]
splits = text.split(separator)
result = []
current_chunk = ""
for split in splits:
if len(current_chunk) + len(split) + len(separator) <= chunk_size:
if current_chunk:
current_chunk += separator + split
else:
current_chunk = split
else:
if current_chunk:
result.append(current_chunk)
if len(split) > chunk_size:
# Split is too big, need to split further
result.extend(split_text(split, remaining_separators, chunk_size))
current_chunk = ""
else:
current_chunk = split
if current_chunk:
result.append(current_chunk)
return result
text_chunks = split_text(content, separators, self.chunk_size)
# Create chunk objects with overlap
for i, chunk_text in enumerate(text_chunks):
if not chunk_text.strip():
continue
# Calculate positions
start_pos = content.find(chunk_text)
if start_pos == -1:
start_pos = i * self.chunk_size
end_pos = start_pos + len(chunk_text)
# Add overlap from previous chunk if not the first chunk
if i > 0 and self.chunk_overlap > 0:
prev_chunk = text_chunks[i-1]
overlap_text = prev_chunk[-self.chunk_overlap:] if len(prev_chunk) > self.chunk_overlap else prev_chunk
chunk_text = overlap_text + " " + chunk_text
chunk = Chunk(
id=self._generate_chunk_id(document_id, i),
document_id=document_id,
content=chunk_text.strip(),
chunk_index=i,
start_pos=start_pos,
end_pos=end_pos,
metadata={
"chunk_method": "recursive",
"original_length": len(chunk_text),
"word_count": len(chunk_text.split())
}
)
chunks.append(chunk)
return chunks
def _sentence_chunk(self, document_id: str, content: str) -> List[Chunk]:
"""Chunk text by sentences"""
chunks = []
sentences = self.preprocessor.extract_sentences(content)
current_chunk = ""
chunk_index = 0
start_pos = 0
for sentence in sentences:
if len(current_chunk) + len(sentence) <= self.chunk_size:
if current_chunk:
current_chunk += " " + sentence
else:
current_chunk = sentence
start_pos = content.find(sentence)
else:
if current_chunk:
chunk = Chunk(
id=self._generate_chunk_id(document_id, chunk_index),
document_id=document_id,
content=current_chunk.strip(),
chunk_index=chunk_index,
start_pos=start_pos,
end_pos=start_pos + len(current_chunk),
metadata={
"chunk_method": "sentence",
"sentence_count": len(self.preprocessor.extract_sentences(current_chunk))
}
)
chunks.append(chunk)
chunk_index += 1
current_chunk = sentence
start_pos = content.find(sentence)
# Add final chunk
if current_chunk:
chunk = Chunk(
id=self._generate_chunk_id(document_id, chunk_index),
document_id=document_id,
content=current_chunk.strip(),
chunk_index=chunk_index,
start_pos=start_pos,
end_pos=start_pos + len(current_chunk),
metadata={
"chunk_method": "sentence",
"sentence_count": len(self.preprocessor.extract_sentences(current_chunk))
}
)
chunks.append(chunk)
return chunks
def _paragraph_chunk(self, document_id: str, content: str) -> List[Chunk]:
"""Chunk text by paragraphs"""
chunks = []
paragraphs = [p.strip() for p in content.split('\n\n') if p.strip()]
current_chunk = ""
chunk_index = 0
start_pos = 0
for paragraph in paragraphs:
if len(current_chunk) + len(paragraph) <= self.chunk_size:
if current_chunk:
current_chunk += "\n\n" + paragraph
else:
current_chunk = paragraph
start_pos = content.find(paragraph)
else:
if current_chunk:
chunk = Chunk(
id=self._generate_chunk_id(document_id, chunk_index),
document_id=document_id,
content=current_chunk.strip(),
chunk_index=chunk_index,
start_pos=start_pos,
end_pos=start_pos + len(current_chunk),
metadata={
"chunk_method": "paragraph",
"paragraph_count": len([p for p in current_chunk.split('\n\n') if p.strip()])
}
)
chunks.append(chunk)
chunk_index += 1
# If paragraph is too long, split it further
if len(paragraph) > self.chunk_size:
para_chunks = self._fixed_chunk(document_id, paragraph)
for pc in para_chunks:
pc.chunk_index = chunk_index
pc.id = self._generate_chunk_id(document_id, chunk_index)
chunks.append(pc)
chunk_index += 1
else:
current_chunk = paragraph
start_pos = content.find(paragraph)
# Add final chunk
if current_chunk:
chunk = Chunk(
id=self._generate_chunk_id(document_id, chunk_index),
document_id=document_id,
content=current_chunk.strip(),
chunk_index=chunk_index,
start_pos=start_pos,
end_pos=start_pos + len(current_chunk),
metadata={
"chunk_method": "paragraph",
"paragraph_count": len([p for p in current_chunk.split('\n\n') if p.strip()])
}
)
chunks.append(chunk)
return chunks
def _fixed_chunk(self, document_id: str, content: str) -> List[Chunk]:
"""Simple fixed-size chunking with overlap"""
chunks = []
for i in range(0, len(content), self.chunk_size - self.chunk_overlap):
chunk_text = content[i:i + self.chunk_size]
if not chunk_text.strip():
continue
chunk = Chunk(
id=self._generate_chunk_id(document_id, len(chunks)),
document_id=document_id,
content=chunk_text.strip(),
chunk_index=len(chunks),
start_pos=i,
end_pos=min(i + self.chunk_size, len(content)),
metadata={
"chunk_method": "fixed",
"original_length": len(chunk_text)
}
)
chunks.append(chunk)
return chunks
def _generate_chunk_id(self, document_id: str, chunk_index: int) -> str:
"""Generate a unique chunk ID"""
return f"{document_id}_chunk_{chunk_index}"
def optimize_chunks_for_embedding(self, chunks: List[Chunk]) -> List[Chunk]:
"""Optimize chunks for better embedding generation"""
optimized_chunks = []
for chunk in chunks:
# Clean the content for embedding
clean_content = self.preprocessor.prepare_for_embedding(chunk.content)
# Skip very short chunks
if len(clean_content.split()) < 5:
continue
# Update chunk with optimized content
optimized_chunk = Chunk(
id=chunk.id,
document_id=chunk.document_id,
content=clean_content,
chunk_index=chunk.chunk_index,
start_pos=chunk.start_pos,
end_pos=chunk.end_pos,
metadata={
**chunk.metadata,
"optimized_for_embedding": True,
"original_content_length": len(chunk.content),
"optimized_content_length": len(clean_content)
}
)
optimized_chunks.append(optimized_chunk)
return optimized_chunks |