Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Consiliency_treesitter-chunker /examples /markdown /demo_processor.py
| #!/usr/bin/env python3 | |
| """Demo script for the Markdown processor. | |
| This script demonstrates how to use the MarkdownProcessor to chunk | |
| Markdown files intelligently while preserving structure. | |
| """ | |
| import sys | |
| from pathlib import Path | |
| # Add parent directory to path for imports | |
| sys.path.insert(0, str(Path(__file__).parent.parent.parent)) | |
| from chunker.processors import ProcessorConfig | |
| from chunker.processors.markdown import MarkdownProcessor | |
| def print_chunk_info(chunk, index): | |
| """Print information about a chunk.""" | |
| print(f"\n{'=' * 60}") | |
| print(f"Chunk {index + 1}:") | |
| print(f" Type: {chunk.node_type}") | |
| print(f" Lines: {chunk.start_line}-{chunk.end_line}") | |
| print(f" Tokens: {chunk.metadata.get('tokens', 'N/A')}") | |
| if chunk.metadata: | |
| print(" Metadata:") | |
| for key, value in chunk.metadata.items(): | |
| if key != "segment_types": # Skip verbose fields | |
| print(f" {key}: {value}") | |
| print("\nContent preview (first 200 chars):") | |
| print("-" * 40) | |
| print(chunk.content[:200] + "..." if len(chunk.content) > 200 else chunk.content) | |
| def process_file(file_path, config=None): | |
| """Process a Markdown file and display results.""" | |
| print(f"Processing: {file_path}") | |
| # Create processor | |
| processor = MarkdownProcessor(config) | |
| # Read file | |
| with Path(file_path).open(encoding="utf-8") as f: | |
| content = f.read() | |
| # Check if processor can handle file | |
| if not processor.can_process(file_path, content): | |
| print(f"Error: {file_path} is not a valid Markdown file") | |
| return | |
| # Extract structure | |
| print("\nExtracting structure...") | |
| structure = processor.extract_structure(content) | |
| print("\nStructure summary:") | |
| print(f" Headers: {len(structure['headers'])}") | |
| print(f" Code blocks: {len(structure['code_blocks'])}") | |
| print(f" Tables: {len(structure['tables'])}") | |
| print(f" Lists: {len(structure['lists'])}") | |
| if structure["front_matter"]: | |
| print(" Front matter: Present") | |
| # Process into chunks | |
| print("\nCreating chunks...") | |
| chunks = processor.process(content, file_path) | |
| print(f"\nCreated {len(chunks)} chunks") | |
| # Display chunk information | |
| for i, chunk in enumerate(chunks): | |
| print_chunk_info(chunk, i) | |
| # Summary statistics | |
| print(f"\n{'=' * 60}") | |
| print("Summary Statistics:") | |
| print(f" Total chunks: {len(chunks)}") | |
| print( | |
| f" Average chunk size: {sum(c.metadata.get('tokens', 0) for c in chunks) / len(chunks):.1f} tokens", | |
| ) | |
| print(f" Chunk types: { {c.node_type for c in chunks} }") | |
| # Check for overlap | |
| overlap_chunks = [c for c in chunks if c.metadata.get("has_overlap")] | |
| if overlap_chunks: | |
| print(f" Chunks with overlap: {len(overlap_chunks)}") | |
| def main(): | |
| """Main demo function.""" | |
| # Example configurations | |
| configs = { | |
| "default": None, | |
| "small_chunks": ProcessorConfig( | |
| max_chunk_size=300, | |
| min_chunk_size=50, | |
| overlap_size=50, | |
| ), | |
| "large_chunks": ProcessorConfig( | |
| max_chunk_size=2000, | |
| min_chunk_size=200, | |
| overlap_size=200, | |
| ), | |
| "no_overlap": ProcessorConfig( | |
| max_chunk_size=1000, | |
| overlap_size=0, | |
| ), | |
| } | |
| # Process example files | |
| example_files = [ | |
| "technical_documentation.md", | |
| "mixed_content.md", | |
| ] | |
| for file_name in example_files: | |
| file_path = Path(__file__).parent / file_name | |
| if file_path.exists(): | |
| print(f"\n{'#' * 80}") | |
| print(f"# Processing {file_name} with different configurations") | |
| print(f"{'#' * 80}") | |
| for config_name, config in configs.items(): | |
| print(f"\n\nConfiguration: {config_name}") | |
| print("-" * 40) | |
| process_file(file_path, config) | |
| # Only show first config for brevity in demo | |
| if config_name == "default": | |
| break | |
| else: | |
| print(f"Warning: {file_path} not found") | |
| print("\n\nDemo complete!") | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 4.18 kB
- Xet hash:
- 97ffb27e434b0d693d0254bbb5cb4a66aeb280d6a159c86bb39f4d6dbbe4c1b7
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.