Pure Programmer
Blue Matrix


Cluster Map

Project: Frequency Table (Words)

Write a program to generate a table of word frequencies. The program should accept a stream on stdin and total the number of each word seen. Once the stream has been read the program should print a tab-delimited table with columns for the Word, Count, and Frequency. The frequency of each word is the count for that word divided by the total number of words in the file.

You can get books from the [[Project Gutenberg]] site to use in testing your program.

Output
$ rustc FrequencyTableWords.rs error: unknown format trait `s` --> FrequencyTableWords.rs:34:30 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> FrequencyTableWords.rs:34:37 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> FrequencyTableWords.rs:34:46 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0432]: unresolved import `regex` --> FrequencyTableWords.rs:8:5 | 8 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find function `getline` in this scope --> FrequencyTableWords.rs:15:8 | 15 | while getline(line) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `split` in this scope --> FrequencyTableWords.rs:16:27 | 16 | let words:Vec<String> = split(line,Regex::new(r"\\W+").unwrap()); | ^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> FrequencyTableWords.rs:18:20 | 18 | let lc:String = tolower(word); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `strlen` in this scope --> FrequencyTableWords.rs:19:7 | 19 | if strlen(lc) > 0 { | ^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> FrequencyTableWords.rs:20:9 | 20 | if !mapContains(countTable,lc) { | ^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `sortedKeys` in this scope --> FrequencyTableWords.rs:31:31 | 31 | let sortedKeys:Vec<String> = sortedKeys(countTable); | ^^^^^^^^^^ not found in this scope error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableWords.rs:33:39 | 33 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | ^^ method not found in `HashMap<String, isize>` error[E0277]: the trait bound `f64: From<isize>` is not satisfied --> FrequencyTableWords.rs:33:58 | 33 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | --------- ^^^^^^^^^^ the trait `From<isize>` is not implemented for `f64` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <f64 as From<bool>> <f64 as From<f32>> <f64 as From<i16>> <f64 as From<i32>> <f64 as From<i8>> <f64 as From<u16>> <f64 as From<u32>> <f64 as From<u8>> error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableWords.rs:34:65 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^^ method not found in `HashMap<String, isize>` error: aborting due to 13 previous errors Some errors have detailed explanations: E0277, E0425, E0432, E0599. For more information about an error, try `rustc --explain E0277`. $ rustc FrequencyTableWords.rs error: unknown format trait `s` --> FrequencyTableWords.rs:34:30 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> FrequencyTableWords.rs:34:37 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> FrequencyTableWords.rs:34:46 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0432]: unresolved import `regex` --> FrequencyTableWords.rs:8:5 | 8 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find function `getline` in this scope --> FrequencyTableWords.rs:15:8 | 15 | while getline(line) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `split` in this scope --> FrequencyTableWords.rs:16:27 | 16 | let words:Vec<String> = split(line,Regex::new(r"\\W+").unwrap()); | ^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> FrequencyTableWords.rs:18:20 | 18 | let lc:String = tolower(word); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `strlen` in this scope --> FrequencyTableWords.rs:19:7 | 19 | if strlen(lc) > 0 { | ^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> FrequencyTableWords.rs:20:9 | 20 | if !mapContains(countTable,lc) { | ^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `sortedKeys` in this scope --> FrequencyTableWords.rs:31:31 | 31 | let sortedKeys:Vec<String> = sortedKeys(countTable); | ^^^^^^^^^^ not found in this scope error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableWords.rs:33:39 | 33 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | ^^ method not found in `HashMap<String, isize>` error[E0277]: the trait bound `f64: From<isize>` is not satisfied --> FrequencyTableWords.rs:33:58 | 33 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | --------- ^^^^^^^^^^ the trait `From<isize>` is not implemented for `f64` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <f64 as From<bool>> <f64 as From<f32>> <f64 as From<i16>> <f64 as From<i32>> <f64 as From<i8>> <f64 as From<u16>> <f64 as From<u32>> <f64 as From<u8>> error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableWords.rs:34:65 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^^ method not found in `HashMap<String, isize>` error: aborting due to 13 previous errors Some errors have detailed explanations: E0277, E0425, E0432, E0599. For more information about an error, try `rustc --explain E0277`. $ rustc FrequencyTableWords.rs error: unknown format trait `s` --> FrequencyTableWords.rs:34:30 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> FrequencyTableWords.rs:34:37 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> FrequencyTableWords.rs:34:46 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0432]: unresolved import `regex` --> FrequencyTableWords.rs:8:5 | 8 | use regex::Regex; | ^^^^^ maybe a missing crate `regex`? | = help: consider adding `extern crate regex` to use the `regex` crate error[E0425]: cannot find function `getline` in this scope --> FrequencyTableWords.rs:15:8 | 15 | while getline(line) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `split` in this scope --> FrequencyTableWords.rs:16:27 | 16 | let words:Vec<String> = split(line,Regex::new(r"\\W+").unwrap()); | ^^^^^ not found in this scope error[E0425]: cannot find function `tolower` in this scope --> FrequencyTableWords.rs:18:20 | 18 | let lc:String = tolower(word); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `strlen` in this scope --> FrequencyTableWords.rs:19:7 | 19 | if strlen(lc) > 0 { | ^^^^^^ not found in this scope error[E0425]: cannot find function `mapContains` in this scope --> FrequencyTableWords.rs:20:9 | 20 | if !mapContains(countTable,lc) { | ^^^^^^^^^^^ not found in this scope error[E0425]: cannot find function `sortedKeys` in this scope --> FrequencyTableWords.rs:31:31 | 31 | let sortedKeys:Vec<String> = sortedKeys(countTable); | ^^^^^^^^^^ not found in this scope error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableWords.rs:33:39 | 33 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | ^^ method not found in `HashMap<String, isize>` error[E0277]: the trait bound `f64: From<isize>` is not satisfied --> FrequencyTableWords.rs:33:58 | 33 | let freq:f64 = f64::from(countTable.at(x)) / f64::from(totalCount); | --------- ^^^^^^^^^^ the trait `From<isize>` is not implemented for `f64` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <f64 as From<bool>> <f64 as From<f32>> <f64 as From<i16>> <f64 as From<i32>> <f64 as From<i8>> <f64 as From<u16>> <f64 as From<u32>> <f64 as From<u8>> error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> FrequencyTableWords.rs:34:65 | 34 | println!("{}", format!("{0:s}\t{1:d}\t{2:.6f}", x, countTable.at(x), freq)); | ^^ method not found in `HashMap<String, isize>` error: aborting due to 13 previous errors Some errors have detailed explanations: E0277, E0425, E0432, E0599. For more information about an error, try `rustc --explain E0277`.

Solution