🦀 Rust crate that allows creating weighted prefix trees that can be used in autocomplete
To use weigthed-trie, add the following to your Cargo.toml file:
toml
[dependencies]
weighted_trie = "0.1.0" # NOTE: Replace to latest minor version.
```rust
use weighted_trie::WeightedTrie;
fn main() { let mut trie = WeightedTrie::new(); // build trie with words and assoicated weights trie.insert("pie".toowned(), 5); trie.insert("pita".toowned(), 2); trie.insert("pi".toowned(), 1); trie.insert("pizza".toowned(), 10);
// get prefix based suggestions sorted by weight
let suggestions = trie.search("pi");
assert_eq!(suggestions, vec!["pizza", "pie", "pita", "pi"]);
let suggestions = trie.search("piz");
assert_eq!(suggestions, vec!["pizza"]);
// out of vocabulary
let suggestions = trie.search("apple");
assert_eq!(suggestions.len(), 0);
}
``
Alternatively you can use
.build` method
```rust use weighted_trie::{WeightedString, WeightedTrie};
fn main() { let weightedstrings = vec![ WeightedString { word: "pie".toowned(), weight: 5, }, WeightedString { word: "pita".toowned(), weight: 2, }, WeightedString { word: "pi".toowned(), weight: 1, }, WeightedString { word: "pizza".to_owned(), weight: 10, }, ];
let trie = WeightedTrie::build(weighted_strings);
}
```
Using 100k weighted strings
weighted_trie/insert time: [342.41 ms 343.86 ms 345.56 ms]
weighted_trie/lookup time: [1.8608 ms 1.9351 ms 2.0834 ms]
weighted_trie/build time: [326.27 ms 330.74 ms 337.03 ms]
README.md
is generated from cargo readme
command.
Do not manually update README.md
instead edit src/lib.rs
and then run cargo readme > README.md
.
hashbrown
instead of standart HashMapLicense: Apache-2.0