graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Comprehensive example demonstrating trait-based chunking strategies
//!
//! This example implements the cAST (Context-Aware Splitting with Tree-sitter) approach
//! from the CMU paper, showing how AST-based chunking improves RAG performance.
//!
//! We use Plato's Symposium as real-world text to demonstrate:
//! 1. Hierarchical chunking for philosophical text - respects paragraph/sentence boundaries
//! 2. Tree-sitter AST-based chunking for embedded code snippets - preserves syntactic boundaries

use graphrag_core::{
    core::{Document, DocumentId, TextChunk},
    text::{HierarchicalChunkingStrategy, TextProcessor},
};
use std::path::Path;
use std::time::Instant;

#[cfg(feature = "code-chunking")]
use graphrag_core::text::RustCodeChunkingStrategy;

/// Metrics for comparing chunking strategies
#[derive(Debug)]
struct ChunkingMetrics {
    strategy_name: String,
    num_chunks: usize,
    avg_chunk_size: f64,
    min_chunk_size: usize,
    max_chunk_size: usize,
    #[allow(dead_code)]
    total_chars: usize,
    processing_time_ms: u64,
}

impl ChunkingMetrics {
    fn from_chunks(strategy_name: &str, chunks: &[TextChunk], processing_time: u64) -> Self {
        if chunks.is_empty() {
            return Self {
                strategy_name: strategy_name.to_string(),
                num_chunks: 0,
                avg_chunk_size: 0.0,
                min_chunk_size: 0,
                max_chunk_size: 0,
                total_chars: 0,
                processing_time_ms: processing_time,
            };
        }

        let sizes: Vec<usize> = chunks.iter().map(|c| c.content.len()).collect();
        let total_chars: usize = sizes.iter().sum();
        let avg_chunk_size = total_chars as f64 / chunks.len() as f64;
        let min_chunk_size = *sizes.iter().min().unwrap();
        let max_chunk_size = *sizes.iter().max().unwrap();

        Self {
            strategy_name: strategy_name.to_string(),
            num_chunks: chunks.len(),
            avg_chunk_size,
            min_chunk_size,
            max_chunk_size,
            total_chars,
            processing_time_ms: processing_time,
        }
    }

    fn print(&self) {
        println!("┌─ {:^20} ─────┐", self.strategy_name);
        println!("│ Chunks: {:13} │", self.num_chunks);
        println!("│ Avg size: {:11.1} │", self.avg_chunk_size);
        println!("│ Min size: {:11} │", self.min_chunk_size);
        println!("│ Max size: {:11} │", self.max_chunk_size);
        println!("│ Time: {:12} ms │", self.processing_time_ms);
        println!("└─────────────────────┘");
    }
}

fn load_symposium_text() -> Result<String, Box<dyn std::error::Error>> {
    let path = Path::new("docs-example/Symposium.txt");
    let content = std::fs::read_to_string(path)?;

    // Remove BOM if present and normalize line endings
    let content = content
        .trim_start_matches('\u{FEFF}') // Remove BOM
        .replace("\r\n", "\n") // Normalize Windows line endings
        .replace('\r', "\n"); // Normalize old Mac line endings

    Ok(content)
}

fn create_document(content: String) -> Document {
    Document::new(
        DocumentId::new("symposium_plato".to_string()),
        "Symposium by Plato (Translated by Benjamin Jowett)".to_string(),
        content,
    )
}

fn demonstrate_hierarchical_chunking(document: &Document) -> (Vec<TextChunk>, ChunkingMetrics) {
    println!("\n🔹 Hierarchical Chunking (LangChain-style)");
    println!("   Respects paragraph and sentence boundaries");

    let start = Instant::now();
    let processor = TextProcessor::new(1000, 100).unwrap();
    let strategy = HierarchicalChunkingStrategy::new(1000, 100, document.id.clone());
    let chunks = processor.chunk_with_strategy(document, &strategy).unwrap();
    let elapsed = start.elapsed().as_millis() as u64;

    let metrics = ChunkingMetrics::from_chunks("Hierarchical", &chunks, elapsed);

    // Show first chunk as example
    if !chunks.is_empty() {
        let preview = chunks[0].content.chars().take(100).collect::<String>();
        println!("   Example chunk: \"{}...\"", preview.replace('\n', " "));
    }

    (chunks, metrics)
}

#[cfg(feature = "code-chunking")]
fn demonstrate_tree_sitter_chunking() -> (Vec<TextChunk>, ChunkingMetrics) {
    println!("\n🔹 Tree-sitter AST Chunking (cAST approach)");
    println!("   Preserves syntactic boundaries for code");

    // Create a Rust code snippet that could appear in a technical document
    let rust_code = r#"/// Example of Rust code with proper syntactic structure
fn analyze_philosophical_concepts(text: &str) -> Vec<String> {
    let concepts: Vec<String> = text
        .split_whitespace()
        .filter(|word| is_philosophical_term(word))
        .map(|word| word.to_lowercase())
        .collect();

    // Deduplicate while preserving order
    let mut unique_concepts = Vec::new();
    for concept in concepts {
        if !unique_concepts.contains(&concept) {
            unique_concepts.push(concept);
        }
    }

    unique_concepts
}

fn is_philosophical_term(word: &str) -> bool {
    let philosophical_terms = [
        "virtue", "wisdom", "justice", "beauty", "truth",
        "knowledge", "love", "good", "evil", "soul"
    ];
    philosophical_terms.contains(&word.to_lowercase().as_str())
}

struct PhilosophicalAnalysis {
    concepts: Vec<String>,
    sentiment: f64,
    complexity: u8,
}

impl PhilosophicalAnalysis {
    fn new(text: &str) -> Self {
        let concepts = analyze_philosophical_concepts(text);
        let sentiment = calculate_sentiment(text);
        let complexity = estimate_complexity(text);

        Self {
            concepts,
            sentiment,
            complexity,
        }
    }

    fn summarize(&self) -> String {
        format!(
            "Found {} concepts with {:.2} sentiment and {} complexity",
            self.concepts.len(),
            self.sentiment,
            self.complexity
        )
    }
}

fn calculate_sentiment(text: &str) -> f64 {
    // Simplified sentiment analysis
    let positive_words = ["good", "beautiful", "wise", "virtuous", "true"];
    let negative_words = ["evil", "ugly", "foolish", "vicious", "false"];

    let words: Vec<&str> = text.split_whitespace().collect();
    let mut score = 0.0;

    for word in words {
        if positive_words.contains(&word) {
            score += 0.1;
        } else if negative_words.contains(&word) {
            score -= 0.1;
        }
    }

    score.clamp(-1.0, 1.0)
}

fn estimate_complexity(text: &str) -> u8 {
    // Simple complexity heuristic based on sentence length and vocabulary
    let words: Vec<&str> = text.split_whitespace().collect();
    let sentences: Vec<&str> = text.split(&['.', '!', '?'][..]).collect();

    let avg_words_per_sentence = if !sentences.is_empty() {
        words.len() / sentences.len()
    } else {
        0
    };

    match avg_words_per_sentence {
        0..=10 => 1,
        11..=20 => 2,
        21..=30 => 3,
        _ => 4,
    }
}"#;

    let start = Instant::now();

    let document_id = DocumentId::new("rust_code_example".to_string());
    let strategy = RustCodeChunkingStrategy::new(50, document_id);
    let chunks = strategy.chunk(rust_code);
    let elapsed = start.elapsed().as_millis() as u64;

    let metrics = ChunkingMetrics::from_chunks("Tree-sitter (Rust)", &chunks, elapsed);

    // Show first chunk as example
    if !chunks.is_empty() {
        println!(
            "   Example chunk: {}",
            chunks[0].content.chars().take(80).collect::<String>()
        );
        println!(
            "   Chunk type: {}",
            extract_function_name(&chunks[0].content).unwrap_or("unknown")
        );
    }

    (chunks, metrics)
}

#[cfg(not(feature = "code-chunking"))]
fn demonstrate_tree_sitter_chunking() -> (Vec<TextChunk>, ChunkingMetrics) {
    println!("\n🔹 Tree-sitter AST Chunking");
    println!(
        "   (Feature 'code-chunking' not enabled - would demonstrate AST-based code chunking)"
    );

    let chunks = Vec::new();
    let metrics = ChunkingMetrics {
        strategy_name: "Tree-sitter (Rust)".to_string(),
        num_chunks: 0,
        avg_chunk_size: 0.0,
        min_chunk_size: 0,
        max_chunk_size: 0,
        total_chars: 0,
        processing_time_ms: 0,
    };

    (chunks, metrics)
}

#[cfg(feature = "code-chunking")]
fn extract_function_name(code: &str) -> Option<&str> {
    use std::collections::HashSet;

    // Simple heuristic to extract function/struct names
    let keywords = HashSet::from(["fn", "struct", "impl", "trait", "enum", "mod"]);

    for line in code.lines() {
        let trimmed = line.trim();
        if let Some(first_word) = trimmed.split_whitespace().next() {
            if keywords.contains(first_word) {
                if let Some(name) = trimmed.split_whitespace().nth(1) {
                    // Remove any generics or parameters
                    if let Some(clean_name) = name.split('<').next() {
                        if let Some(cleaner_name) = clean_name.split('(').next() {
                            return Some(cleaner_name);
                        }
                    }
                }
            }
        }
    }
    None
}

fn analyze_boundary_preservation(chunks: &[TextChunk]) -> f64 {
    if chunks.len() < 2 {
        return 1.0;
    }

    let mut well_terminated = 0;

    for chunk in chunks {
        let trimmed = chunk.content.trim();
        if trimmed.is_empty() {
            continue;
        }

        // Check if chunk ends at a natural boundary
        let last_char = trimmed.chars().last().unwrap_or(' ');
        if last_char == '.'
            || last_char == '!'
            || last_char == '?'
            || last_char == ':'
            || last_char == ';'
            || last_char == '\n'
        {
            well_terminated += 1;
        }
    }

    well_terminated as f64 / chunks.len() as f64
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🎭 GraphRAG Trait-Based Chunking: Symposium Analysis");
    println!("=====================================================");
    println!("Implementing cAST (Context-Aware Splitting) approach");

    // Load the Symposium text
    println!("\n📚 Loading Symposium by Plato...");
    let content = load_symposium_text()?;
    let document = create_document(content);

    println!("   Document loaded: {} characters", document.content.len());
    println!(
        "   First 100 chars: {}",
        document
            .content
            .chars()
            .take(100)
            .collect::<String>()
            .replace('\n', " ")
    );

    // Demonstrate different chunking strategies
    println!("\n🔧 Testing Different Chunking Strategies:");
    println!("=========================================");

    let (hierarchical_chunks, hierarchical_metrics) = demonstrate_hierarchical_chunking(&document);
    let (tree_sitter_chunks, tree_sitter_metrics) = demonstrate_tree_sitter_chunking();

    // Print comprehensive comparison
    println!("\n📊 Comparative Analysis:");
    println!("========================");

    hierarchical_metrics.print();
    tree_sitter_metrics.print();

    // Analyze boundary preservation
    println!("\n🎯 Boundary Preservation Analysis:");
    println!("==================================");

    let hierarchical_boundary_score = analyze_boundary_preservation(&hierarchical_chunks);

    println!(
        "Hierarchical: {:.1}% of chunks end at natural boundaries",
        hierarchical_boundary_score * 100.0
    );

    if !tree_sitter_chunks.is_empty() {
        println!("Tree-sitter: Preserves syntactic boundaries for all code chunks");
    }

    // Performance comparison
    println!("\n⚡ Performance Comparison:");
    println!("==========================");

    let total_time =
        hierarchical_metrics.processing_time_ms + tree_sitter_metrics.processing_time_ms;

    println!(
        "Hierarchical:  {:.1}% of total time",
        (hierarchical_metrics.processing_time_ms as f64 / total_time as f64) * 100.0
    );
    println!(
        "Tree-sitter:   {:.1}% of total time",
        (tree_sitter_metrics.processing_time_ms as f64 / total_time as f64) * 100.0
    );

    // cAST Benefits Summary
    println!("\n🏆 cAST (Context-Aware Splitting) Benefits:");
    println!("===========================================");
    println!("✅ Preserves syntactic boundaries for code snippets");
    println!("✅ Maintains coherent structure in philosophical text");
    println!("✅ Flexible strategy selection based on content type");
    println!("✅ Modular architecture allows easy extension");
    println!("✅ Performance-optimized with trait-based design");

    // Show chunk diversity
    println!("\n📈 Chunk Size Distribution:");
    println!("===========================");

    for (name, chunks) in [("Hierarchical", &hierarchical_chunks)] {
        if !chunks.is_empty() {
            let sizes: Vec<usize> = chunks.iter().map(|c| c.content.len()).collect();
            let median = if !sizes.is_empty() {
                let mut sorted = sizes.clone();
                sorted.sort();
                sorted[sorted.len() / 2]
            } else {
                0
            };

            println!(
                "{}: Min={}, Median={}, Max={}",
                name,
                sizes.iter().min().unwrap(),
                median,
                sizes.iter().max().unwrap()
            );
        }
    }

    println!("\n✅ Example completed successfully!");
    println!("The trait-based chunking architecture provides:");
    println!("  • Clean separation of concerns");
    println!("  • Easy strategy switching");
    println!("  • Optimal boundary preservation");
    println!("  • Extensible design for new chunking approaches");

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_symposium_loading() {
        let content = load_symposium_text().unwrap();
        assert!(!content.is_empty());
        assert!(content.len() > 1000); // Symposium should be substantial
        assert!(content.contains("Plato") || content.contains("Symposium"));
    }

    #[test]
    fn test_chunking_strategies() {
        let content =
            "Test paragraph one. Test paragraph two.\n\nNew paragraph with different content.";
        let document = create_document(content.to_string());

        // Test hierarchical chunking
        let strategy = HierarchicalChunkingStrategy::new(50, 10, document.id.clone());
        let chunks = strategy.chunk(&document.content);
        assert!(!chunks.is_empty());

        // Test boundary preservation
        let boundary_score = analyze_boundary_preservation(&chunks);
        assert!(boundary_score > 0.0);
    }

    #[test]
    fn test_metrics_calculation() {
        let mut chunks = Vec::new();
        let doc_id = DocumentId::new("test".to_string());

        // Create test chunks
        for i in 0..5 {
            let chunk = TextChunk::new(
                graphrag_core::core::ChunkId::new(format!("chunk_{}", i)),
                doc_id.clone(),
                format!("Test chunk {}", i),
                i * 10,
                i * 10 + 10,
            );
            chunks.push(chunk);
        }

        let metrics = ChunkingMetrics::from_chunks("Test", &chunks, 100);
        assert_eq!(metrics.num_chunks, 5);
        assert_eq!(metrics.avg_chunk_size, 13.0); // "Test chunk X" = 12 chars + newline
        assert_eq!(metrics.processing_time_ms, 100);
    }
}