use symspellrs::{include_dictionary, SymSpell, Verbosity};
fn print_suggestions(title: &str, suggestions: &[symspellrs::Suggestion]) {
println!("-- {} ({} suggestions) --", title, suggestions.len());
for s in suggestions {
println!(
" term: {:<12} distance: {:>2} frequency: {}",
s.term, s.distance, s.frequency
);
}
}
fn example_compile_time() {
let sym = include_dictionary!("tests/data/words.txt", max_distance = 2, lowercase = true);
println!("=== Compile-time built SymSpell ===");
let exact = sym.lookup("world", 2, Verbosity::Top);
print_suggestions("Exact lookup for 'world'", &exact);
let suggestions = sym.lookup("helo", 2, Verbosity::Closest);
print_suggestions("Closest suggestions for 'helo'", &suggestions);
let all = sym.lookup("teso", 2, Verbosity::All);
print_suggestions("All suggestions for 'teso'", &all);
}
fn example_runtime_build() {
println!("\n=== Runtime-built SymSpell ===");
let entries = vec![
("hello".to_string(), 3usize),
("hell".to_string(), 1usize),
("help".to_string(), 1usize),
("world".to_string(), 5usize),
("test".to_string(), 2usize),
("tost".to_string(), 4usize),
("applied".to_string(), 1usize),
("apple".to_string(), 2usize),
("apply".to_string(), 1usize),
];
let sym = SymSpell::from_iter(2, entries);
let s1 = sym.lookup("helo", 2, Verbosity::Top);
print_suggestions("Top suggestion for 'helo' (runtime)", &s1);
let s2 = sym.lookup("appl", 2, Verbosity::Closest);
print_suggestions("Closest suggestions for 'appl' (runtime)", &s2);
let s3 = sym.lookup("testo", 2, Verbosity::All);
print_suggestions("All suggestions for 'testo' (runtime)", &s3);
}
fn main() {
println!("symspellrs example: compile-time macro and runtime builder\n");
example_compile_time();
example_runtime_build();
println!("\nDone.");
}