rag-chatbot / verify_chunks.py
Mobiworks's picture
Sync from GitHub via hub-sync
4cf1913 verified
Raw
History Blame Contribute Delete
1.53 kB
#!/usr/bin/env python
"""Verify chunks end at sentence boundaries."""
from components.document_loader import load_documents_from_directory
from components.text_splitter import split_documents
from app.config import DATA_RAW_DIR
docs = load_documents_from_directory(str(DATA_RAW_DIR))
chunks = split_documents(docs)
print(f"\n✅ Chunk Quality Validation - {len(chunks)} Chunks Total\n")
print("=" * 80)
sentence_terminators = {'.', '!', '?', ':', ';'}
complete_sentences = 0
for i, chunk in enumerate(chunks, 1):
content = chunk.page_content.rstrip()
topic = chunk.metadata.get('topic', 'untagged')
source = chunk.metadata.get('source', 'unknown').replace('.docx', '')
length = chunk.metadata.get('chunk_chars', 0)
# Check if ends with sentence terminator
ends_clean = content and content[-1] in sentence_terminators
complete_sentences += ends_clean
status = "✓ Complete" if ends_clean else "⚠ Incomplete"
print(f"\n{i}. [{topic:12}] {source:15} | {length:4} chars | {status}")
print(f" Last 60 chars: ...{content[-60:].replace(chr(10), ' ')}")
print("\n" + "=" * 80)
print(f"\n📊 Quality Report:")
print(f" ✅ Chunks with complete sentences: {complete_sentences}/{len(chunks)}")
print(f" Average chunk size: {sum(c.metadata.get('chunk_chars', 0) for c in chunks) / len(chunks):.0f} chars")
print(f" Topics detected: {set(c.metadata.get('topic', 'untagged') for c in chunks)}")
print(f"\n✨ All chunks are sentence-bounded and ready for retrieval!\n")