basic_usage/
basic_usage.rs1use marisa_ffi::{version, Agent, Keyset, Trie};
11
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13 println!("Marisa FFI Example");
14 println!("Version: {}", version());
15 println!();
16
17 let mut keyset = Keyset::new()?;
19
20 let sample_keys = vec![
21 "apple",
22 "application",
23 "apply",
24 "app",
25 "banana",
26 "band",
27 "bandit",
28 "cherry",
29 "cheese",
30 "chef",
31 "dog",
32 "doggy",
33 "doggie",
34 "elephant",
35 "eleven",
36 "elevator",
37 ];
38
39 println!("Adding {} keys to keyset...", sample_keys.len());
40 for key in &sample_keys {
41 keyset.push(key)?;
42 }
43
44 println!("Building trie...");
46 let trie = Trie::build(&keyset)?;
47 println!("Trie built successfully!");
48 println!();
49
50 println!("=== Exact Lookup Tests ===");
52 let test_keys = vec!["apple", "banana", "nonexistent", "app"];
53 for key in test_keys {
54 match trie.lookup(key) {
55 Some(id) => println!("Found '{}' with ID: {}", key, id),
56 None => println!("'{}' not found", key),
57 }
58 }
59 println!();
60
61 println!("=== Predictive Search Tests ===");
63 let prefixes = vec!["app", "ban", "che", "dog", "ele"];
64
65 for prefix in prefixes {
66 println!("Keys starting with '{}':", prefix);
67 let mut agent = Agent::new()?;
68
69 if trie.predictive_search(prefix, &mut agent)? {
70 let key = agent.key()?;
73 let id = agent.id();
74 println!(" - {} (ID: {})", key, id);
75 println!(" Total: 1 key (iteration not implemented)");
76 } else {
77 println!(" No keys found");
78 }
79 println!();
80 }
81
82 println!("=== Reverse Lookup Tests ===");
84 let test_ids = vec![0, 1, 2, 3, 4];
85 let mut agent = Agent::new()?;
86
87 for id in test_ids {
88 match trie.reverse_lookup(id, &mut agent) {
89 Ok(_) => {
90 let key = agent.key()?;
91 println!("ID {} -> '{}'", id, key);
92 }
93 Err(e) => println!("ID {} -> Error: {}", id, e),
94 }
95 }
96 println!();
97
98 println!("=== Common Prefix Search Tests ===");
100 let test_strings = vec!["application", "bandit", "chef", "doggie"];
101
102 for test_str in test_strings {
103 println!("Common prefixes of '{}':", test_str);
104 let mut agent = Agent::new()?;
105
106 if trie.common_prefix_search(test_str, &mut agent)? {
107 let key = agent.key()?;
110 let id = agent.id();
111 println!(" - {} (ID: {})", key, id);
112 println!(" Total: 1 prefix (iteration not implemented)");
113 } else {
114 println!(" No prefixes found");
115 }
116 println!();
117 }
118
119 println!("=== Save/Load Test ===");
121 let filename = "test_trie.marisa";
122
123 trie.save(filename)?;
125 println!("Trie saved to '{}'", filename);
126
127 let mut loaded_trie = Trie::new()?;
129 loaded_trie.load(filename)?;
130 println!("Trie loaded from '{}'", filename);
131
132 match loaded_trie.lookup("apple") {
134 Some(id) => println!("Loaded trie lookup 'apple' -> ID: {}", id),
135 None => println!("Loaded trie lookup 'apple' -> Not found"),
136 }
137
138 std::fs::remove_file(filename)?;
140 println!("Test file '{}' removed", filename);
141
142 Ok(())
143}