This repository was archived by the owner on Aug 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.rs
More file actions
106 lines (86 loc) · 2.62 KB
/
Copy pathrepl.rs
File metadata and controls
106 lines (86 loc) · 2.62 KB
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
//! Interactive REPL demonstrating fuzzy search.
//!
//! Automatically builds an FST dictionary on first run
//! and lets you query terms with up to 2 edits/transpositions in real-time.
//!
//! Run with: `cargo run --example repl --release`
use fuzzies::{Dictionary, DictionaryError};
use std::fs::File;
use std::io::{self, BufRead, Write};
use std::path::Path;
use std::time::Instant;
fn main() -> Result<(), DictionaryError> {
let fst_path = "dict.fst";
setup_dictionary(fst_path)?;
println!("Loading dictionary into memory...");
let dict = Dictionary::open(fst_path)?;
println!("Dictionary loaded successfully!");
println!("\nType a word to find fuzzy matches. (Type 'quit' to stop)\n");
let stdin = io::stdin();
let mut stdout = io::stdout();
loop {
print!("search > ");
stdout.flush()?;
let mut input = String::new();
stdin.lock().read_line(&mut input)?;
let query = input.trim();
if query.eq_ignore_ascii_case("quit") || query.eq_ignore_ascii_case("exit") {
println!("Goodbye!");
break;
}
if query.is_empty() {
continue;
}
let start_time = Instant::now();
let results = dict
.search(query)
.distance(2)
.transposition(true)
.limit(5)
.execute()?;
let duration = start_time.elapsed();
if results.is_empty() {
println!("No matches found for '{query}'.");
} else {
println!("Found {} matches in {duration:?}", results.len());
for (i, result) in results.into_iter().enumerate() {
let exact_marker = if result.is_exact() { " [EXACT]" } else { "" };
println!(" {}. {}{}", i + 1, result.key, exact_marker);
}
}
println!();
}
Ok(())
}
fn setup_dictionary(fst_path: &str) -> Result<(), DictionaryError> {
if Path::new(fst_path).exists() {
return Ok(());
}
println!("First run detected. Generating a sample dictionary...");
let txt_path = "dict.txt";
let mut file = File::create(txt_path)?;
let words = [
"apple",
"banana",
"mango",
"rust",
"computer",
"book",
"bar",
"bear",
"bool",
"foo",
"bar",
"love",
"programming",
"programmer",
"profanity",
];
for word in words {
writeln!(file, "{word}")?;
}
Dictionary::sort(txt_path)?;
Dictionary::build(txt_path, fst_path)?;
println!("Dictionary built successfully!\n");
Ok(())
}