File: string_frequency.rs

package info (click to toggle)
rust-radix-trie 0.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 224 kB
  • sloc: makefile: 2
file content (34 lines) | stat: -rw-r--r-- 1,210 bytes parent folder | download
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
extern crate radix_trie;

use radix_trie::{Trie, TrieCommon};

fn main() {
    let example = "bananaphone".to_string();
    // Our frequency trie will store various strings with the frequency that they are used
    let mut trie: Trie<String, u32> = Trie::new();
    let v: Vec<char> = example.chars().collect();

    // We want the frequencies of all strings from 1 to 3 characters long
    for window_length in 1..4 {
        for a in v.windows(window_length) {
            // Create a new String to hold our key
            let mut s = String::new();
            // Append all chars in a to the String
            s.extend(a);
            // If the value at s exists, add 1. Otherwise, assign 1 as the key's value.
            trie.map_with_default(s, |v| *v += 1, 1);
        }
    }

    // Iterate through all the values in the trie and print them with their frequencies.
    // Iterates and prints in lexicographic order.
    println!("All trie nodes");
    for (k, v) in trie.iter() {
        println!("{}: {}", k, v);
    }

    println!("All children of 'a'");
    for n in trie.subtrie(&"a".to_string()).unwrap().children() {
        println!("{}: {}", n.key().unwrap(), n.value().unwrap());
    }
}