probe-code 0.6.0

AI-friendly, fully local, semantic code search tool for large codebases
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
// No need to import HashMap and HashSet as they're already imported in the parent module
use probe_code::search::tokenization::tokenize_and_stem;

/// Process the terms in an AST expression, applying tokenization and stemming
/// to the terms unless they are marked as exact.
fn process_ast_terms(expr: Expr) -> Expr {
    match expr {
        Expr::Term { keywords, field, required, excluded, exact } => {
            // If exact or excluded => skip tokenization
            let processed_keywords = if exact || excluded {
                keywords
            } else {
                // Apply tokenization and stemming to each keyword
                let mut processed = Vec::new();
                for keyword in keywords {
                    let stemmed = tokenize_and_stem(&keyword);
                    processed.extend(stemmed);
                }
                processed
            };
            
            Expr::Term {
                keywords: processed_keywords,
                field,
                required,
                excluded,
                exact,
            }
        },
        Expr::And(left, right) => {
            Expr::And(
                Box::new(process_ast_terms(*left)),
                Box::new(process_ast_terms(*right))
            )
        },
        Expr::Or(left, right) => {
            Expr::Or(
                Box::new(process_ast_terms(*left)),
                Box::new(process_ast_terms(*right))
            )
        },
    }
}

#[test]
fn test_tokenize_and_stem() {
    // Test basic stemming
    let result = tokenize_and_stem("running");
    assert_eq!(result, vec!["run"]);
    
    // Test camel case splitting and stemming
    let result = tokenize_and_stem("enableIpWhiteListing");
    assert!(result.contains(&"enabl".to_string()));
    assert!(result.contains(&"ip".to_string()));
    assert!(result.contains(&"white".to_string()));
    assert!(result.contains(&"list".to_string()));
    
    // Test compound word splitting and stemming
    // "whitelist" is now a special case word that should not be split
    let result = tokenize_and_stem("whitelist");
    assert!(result.contains(&"whitelist".to_string()));
    
    // Test stop word filtering
    let result = tokenize_and_stem("function");
    assert!(result.len() == 1); // "function" is not a stop word in this context
    
    // Test with a term that might be stemmed
    let result = tokenize_and_stem("firewall");
    // The stemmer might stem "firewall" to "firewal" depending on the stemming algorithm
    assert!(result.len() == 1);
    assert!(result[0] == "firewall" || result[0] == "firewal");
}

#[test]
fn test_process_ast_terms() {
    // Test processing a simple term
    let expr = Expr::Term {
        keywords: vec!["running".to_string()],
        field: None,
        required: false,
        excluded: false,
        exact: false,
    };
    
    let processed = process_ast_terms(expr);
    
    if let Expr::Term { keywords, field, required, excluded, exact } = processed {
        assert_eq!(keywords, vec!["run"]);
        assert_eq!(field, None);
        assert!(!required);
        assert!(!excluded);
        assert!(!exact);
    } else {
        panic!("Expected Term expression");
    }
    
    // Test processing a term with camel case
    let expr = Expr::Term {
        keywords: vec!["enableIpWhiteListing".to_string()],
        field: None,
        required: true,
        excluded: false,
        exact: false,
    };
    
    let processed = process_ast_terms(expr);
    
    if let Expr::Term { keywords, field, required, excluded, exact } = processed {
        assert!(keywords.contains(&"enabl".to_string()));
        assert!(keywords.contains(&"ip".to_string()));
        assert!(keywords.contains(&"white".to_string()));
        assert!(keywords.contains(&"list".to_string()));
        assert_eq!(field, None);
        assert!(required);
        assert!(!excluded);
        assert!(!exact);
    } else {
        panic!("Expected Term expression");
    }
    
    // Test processing a compound expression
    let expr = Expr::And(
        Box::new(Expr::Term {
            keywords: vec!["running".to_string()],
            field: None,
            required: false,
            excluded: false,
            exact: false,
        }),
        Box::new(Expr::Term {
            keywords: vec!["whitelist".to_string()],
            field: None,
            required: false,
            excluded: false,
            exact: false,
        })
    );
    
    let processed = process_ast_terms(expr);
    
    if let Expr::And(left, right) = processed {
        if let Expr::Term { keywords, .. } = *left {
            assert_eq!(keywords, vec!["run"]);
        } else {
            panic!("Expected Term expression for left side");
        }
        
        if let Expr::Term { keywords, .. } = *right {
            // "whitelist" is now a special case word that should not be split
            assert!(keywords.contains(&"whitelist".to_string()));
        } else {
            panic!("Expected Term expression for right side");
        }
    } else {
        panic!("Expected And expression");
    }
}

#[test]
fn test_parse_query_with_tokenization() {
    // Test parsing a query with tokenization and stemming
    let result = parse_query_test("running").unwrap();
    
    if let Expr::Term { keywords, .. } = result {
        assert_eq!(keywords, vec!["run"]);
    } else {
        panic!("Expected Term expression");
    }
    
    // Test parsing a query with camel case
    let result = parse_query_test("enableIpWhiteListing").unwrap();
    
    if let Expr::Term { keywords, .. } = result {
        assert!(keywords.contains(&"enabl".to_string()));
        assert!(keywords.contains(&"ip".to_string()));
        assert!(keywords.contains(&"white".to_string()));
        assert!(keywords.contains(&"list".to_string()));
    } else {
        panic!("Expected Term expression");
    }
    
    // Test parsing a complex query
    let result = parse_query_test("running AND whitelist").unwrap();
    
    if let Expr::And(left, right) = result {
        if let Expr::Term { keywords, .. } = *left {
            assert_eq!(keywords, vec!["run"]);
        } else {
            panic!("Expected Term expression for left side");
        }
        
        if let Expr::Term { keywords, .. } = *right {
            // "whitelist" is now a special case word that should not be split
            assert!(keywords.contains(&"whitelist".to_string()));
        } else {
            panic!("Expected Term expression for right side");
        }
    } else {
        panic!("Expected And expression");
    }
}

#[test]
fn test_query_evaluation_with_tokenization() {
    // Create term indices with stemmed terms
    let mut term_indices = HashMap::new();
    term_indices.insert("run".to_string(), 0);
    term_indices.insert("whitelist".to_string(), 1);
    
    // Parse a query that will be tokenized and stemmed
    let expr = parse_query_test("running").unwrap();
    
    // Match when the stemmed term is present
    let matched_terms = HashSet::from([0]); // "run"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when the stemmed term is absent
    let matched_terms = HashSet::from([1]); // "whitelist"
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // Parse a compound query
    let expr = parse_query_test("running AND whitelist").unwrap();
    
    // Match when all terms are present
    let matched_terms = HashSet::from([0, 1]); // "run", "whitelist"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when only some terms are present
    let matched_terms = HashSet::from([0]); // "run"
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}

#[test]
fn test_tokenize_quoted_strings() {
    // Test basic quoted string
    let tokens = tokenize("\"hello world\"").unwrap();
    assert_eq!(tokens.len(), 1);
    assert_eq!(tokens[0], Token::QuotedString("hello world".to_string()));
    
    // Test quoted string with escaped quotes
    let tokens = tokenize("\"hello \\\"world\\\"\"").unwrap();
    assert_eq!(tokens.len(), 1);
    assert_eq!(tokens[0], Token::QuotedString("hello \"world\"".to_string()));
    
    // Test quoted string with other tokens
    let tokens = tokenize("foo \"bar baz\" qux").unwrap();
    assert_eq!(tokens.len(), 3);
    assert_eq!(tokens[0], Token::Ident("foo".to_string()));
    assert_eq!(tokens[1], Token::QuotedString("bar baz".to_string()));
    assert_eq!(tokens[2], Token::Ident("qux".to_string()));
    
    // Test quoted string with prefixes
    let tokens = tokenize("+\"required term\"").unwrap();
    assert_eq!(tokens.len(), 2);
    assert_eq!(tokens[0], Token::Plus);
    assert_eq!(tokens[1], Token::QuotedString("required term".to_string()));
    
    let tokens = tokenize("-\"excluded term\"").unwrap();
    assert_eq!(tokens.len(), 2);
    assert_eq!(tokens[0], Token::Minus);
    assert_eq!(tokens[1], Token::QuotedString("excluded term".to_string()));
}

#[test]
fn test_parse_quoted_strings() {
    // Test basic quoted string
    let result = parse_query_test("\"hello world\"").unwrap();
    
    if let Expr::Term { keywords, exact, .. } = result {
        assert_eq!(keywords, vec!["hello world"]);
        assert!(exact);
    } else {
        panic!("Expected Term expression");
    }
    
    // Test quoted string with prefixes
    let result = parse_query_test("+\"required term\"").unwrap();
    
    if let Expr::Term { keywords, required, exact, .. } = result {
        assert_eq!(keywords, vec!["required term"]);
        assert!(required);
        assert!(exact);
    } else {
        panic!("Expected Term expression");
    }
    
    let result = parse_query_test("-\"excluded term\"").unwrap();
    
    if let Expr::Term { keywords, excluded, exact, .. } = result {
        assert_eq!(keywords, vec!["excluded term"]);
        assert!(excluded);
        assert!(exact);
    } else {
        panic!("Expected Term expression");
    }
    
    // Test quoted string with boolean operators
    let result = parse_query_test("\"exact term\" AND foo").unwrap();
    
    if let Expr::And(left, right) = result {
        if let Expr::Term { keywords, exact, .. } = *left {
            assert_eq!(keywords, vec!["exact term"]);
            assert!(exact);
        } else {
            panic!("Expected Term expression for left side");
        }
        
        if let Expr::Term { keywords, exact, .. } = *right {
            assert!(keywords.contains(&"foo".to_string()));
            assert!(!exact);
        } else {
            panic!("Expected Term expression for right side");
        }
    } else {
        panic!("Expected And expression");
    }
}

#[test]
fn test_process_ast_terms_with_exact_flag() {
    // Test processing an exact term
    let expr = Expr::Term {
        keywords: vec!["running".to_string()],
        field: None,
        required: false,
        excluded: false,
        exact: true,
    };
    
    let processed = process_ast_terms(expr);
    
    if let Expr::Term { keywords, field, required, excluded, exact } = processed {
        // Keywords should not be stemmed for exact terms
        assert_eq!(keywords, vec!["running"]);
        assert_eq!(field, None);
        assert!(!required);
        assert!(!excluded);
        assert!(exact);
    } else {
        panic!("Expected Term expression");
    }
    
    // Test processing a compound expression with an exact term
    let expr = Expr::And(
        Box::new(Expr::Term {
            keywords: vec!["running".to_string()],
            field: None,
            required: false,
            excluded: false,
            exact: false,
        }),
        Box::new(Expr::Term {
            keywords: vec!["whitelist".to_string()],
            field: None,
            required: false,
            excluded: false,
            exact: true,
        })
    );
    
    let processed = process_ast_terms(expr);
    
    if let Expr::And(left, right) = processed {
        if let Expr::Term { keywords, exact, .. } = *left {
            assert_eq!(keywords, vec!["run"]);
            assert!(!exact);
        } else {
            panic!("Expected Term expression for left side");
        }
        
        if let Expr::Term { keywords, exact, .. } = *right {
            // Keywords should not be stemmed for exact terms
            assert_eq!(keywords, vec!["whitelist"]);
            assert!(exact);
        } else {
            panic!("Expected Term expression for right side");
        }
    } else {
        panic!("Expected And expression");
    }
}

#[test]
fn test_evaluate_exact_terms_tokenization() {
    // Create term indices
    let mut term_indices = HashMap::new();
    term_indices.insert("running".to_string(), 0);
    term_indices.insert("run".to_string(), 1);
    term_indices.insert("whitelist".to_string(), 2);
    term_indices.insert("white".to_string(), 3);
    term_indices.insert("list".to_string(), 4);
    
    // Test exact term
    let expr = Expr::Term {
        keywords: vec!["running".to_string()],
        field: None,
        required: false,
        excluded: false,
        exact: true,
    };
    
    // Match when the exact term is present
    let matched_terms = HashSet::from([0]); // "running"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when only the stemmed term is present
    let matched_terms = HashSet::from([1]); // "run"
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
    
    // Test non-exact term
    let expr = Expr::Term {
        keywords: vec!["run".to_string()], // Use the stemmed form directly
        field: None,
        required: false,
        excluded: false,
        exact: false,
    };
    
    // The term is "run", so it won't match "running" directly
    // We need to update the term_indices to reflect the actual relationship
    let mut term_indices_updated = HashMap::new();
    term_indices_updated.insert("run".to_string(), 1); // Map "run" to index 1
    term_indices_updated.insert("running".to_string(), 0); // Map "running" to index 0
    term_indices_updated.insert("whitelist".to_string(), 2);
    term_indices_updated.insert("white".to_string(), 3);
    term_indices_updated.insert("list".to_string(), 4);
    
    // Match when the exact term is present
    let matched_terms = HashSet::from([0, 1]); // Include both "running" and "run"
    assert!(expr.evaluate(&matched_terms, &term_indices_updated, false));
    
    // Match when only the stemmed term is present
    let matched_terms = HashSet::from([1]); // "run"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // Test compound expression with exact term - simplified approach
    // Create a new expression with the stemmed form directly
    let expr = Expr::And(
        Box::new(Expr::Term {
            keywords: vec!["run".to_string()], // Use stemmed form directly
            field: None,
            required: false,
            excluded: false,
            exact: false,
        }),
        Box::new(Expr::Term {
            keywords: vec!["whitelist".to_string()],
            field: None,
            required: false,
            excluded: false,
            exact: true,
        })
    );
    
    // Match when both terms are present
    let matched_terms = HashSet::from([1, 2]); // "run", "whitelist"
    assert!(expr.evaluate(&matched_terms, &term_indices, false));
    
    // No match when the exact term is only present as stemmed parts
    let matched_terms = HashSet::from([0, 3, 4]); // "running", "white", "list"
    assert!(!expr.evaluate(&matched_terms, &term_indices, false));
}