Debian Codegraph @

examples/string_frequency.rs

raw symbols SCIP
1 extern crate radix_trie;
2
3 use radix_trie::{Trie, TrieCommon};
4
5 fn main() {
6 let example = "bananaphone".to_string();
7 // Our frequency trie will store various strings with the frequency that they are used
8 let mut trie: Trie<String, u32> = Trie::new();
9 let v: Vec<char> = example.chars().collect();
10
11 // We want the frequencies of all strings from 1 to 3 characters long
12 for window_length in 1..4 {
13 for a in v.windows(window_length) {
14 // Create a new String to hold our key
15 let mut s = String::new();
16 // Append all chars in a to the String
17 s.extend(a);
18 // If the value at s exists, add 1. Otherwise, assign 1 as the key's value.
19 trie.map_with_default(s, |v| *v += 1, 1);
20 }
21 }
22
23 // Iterate through all the values in the trie and print them with their frequencies.
24 // Iterates and prints in lexicographic order.
25 println!("All trie nodes");
26 for (k, v) in trie.iter() {
27 println!("{}: {}", k, v);
28 }
29
30 println!("All children of 'a'");
31 for n in trie.subtrie(&"a".to_string()).unwrap().children() {
32 println!("{}: {}", n.key().unwrap(), n.value().unwrap());
33 }
34 }