[SOLVED]Letter Frequency Counter Issue
Th3Whit3Wolf opened this issue · 2 comments
Th3Whit3Wolf commented
Hello I'm having troubles using your library, I'm hoping maybe you could help me and we could add the code to your examples.
I'm trying to reimplement this python
import matplotlib.pyplot as plt
alphabet = "abcdefghijklmnopqrstuvwxyz"
# Get Message from user
message = "This is a long message"
letter_counts = [message.count(l) for l in alphabet]
letter_colors = plt.cm.hsv([0.8*i/max(letter_counts) for i in letter_counts])
plt.bar(range(26), letter_counts, color=letter_colors)
plt.xticks(range(26), alphabet) # letter labels on x-axis
plt.tick_params(axis="x", bottom=False) # no ticks, only labels on x-axis
plt.title("Frequency of each letter")
plt.savefig("output.png")
into rust
so far what I'm working with is
use std::collections::btree_map::BTreeMap;
use plotlib::style::BarChart;
fn main() {
let message: &str = "This is a long message";
let mut count = BTreeMap::new();
for c in message.trim().to_lowercase().chars() {
if c.is_alphabetic() {
*count.entry(c).or_insert(0) += 1
}
}
println!("Number of occurences per character");
for (ch, count) in &count {
println!("{:?}: {}", ch, count);
let count = *count as f64;
plotlib::barchart::BarChart::new(count).label(ch.to_string());
}
let v = plotlib::view::CategoricalView::new();
plotlib::page::Page::single(&v)
.save("barchart.svg")
.expect("saving svg");
}
I'm not sure how to use data in the btree with plotlib,
I'm using version 0.4.0 by the way
Th3Whit3Wolf commented
Solved
use std::collections::btree_map::BTreeMap;
use plotlib::style::BarChart;
fn main() {
let mut data = Vec::new();
let message: &str = "This is a long message";
let mut count = BTreeMap::new();
for c in message.trim().to_lowercase().chars() {
if c.is_alphabetic() {
*count.entry(c).or_insert(0) += 1
}
}
println!("Number of occurences per character");
for (ch, count) in &count {
println!("{:?}: {}", ch, count);
let count = *count as f64;
data.push(plotlib::barchart::BarChart::new(count).label(ch.to_string()));
}
// Add data to the view
let v = data
.iter()
.fold(plotlib::view::CategoricalView::new(), |view, datum| {
view.add(datum)
});
plotlib::page::Page::single(&v)
.save("barchart.svg")
.expect("saving svg");
}
simonrw commented
Glad to hear that you solved this issue! Please keep us updated on your progress using the library.