Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SOLVED]Letter Frequency Counter Issue #45

Closed
Th3Whit3Wolf opened this issue Jun 8, 2019 · 2 comments
Closed

[SOLVED]Letter Frequency Counter Issue #45

Th3Whit3Wolf opened this issue Jun 8, 2019 · 2 comments

Comments

@Th3Whit3Wolf
Copy link
Contributor

Th3Whit3Wolf commented Jun 8, 2019

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 Th3Whit3Wolf changed the title Letter Frequency Counter Issue [SOLVED]Letter Frequency Counter Issue Jun 8, 2019
@Th3Whit3Wolf
Copy link
Contributor Author

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
Copy link
Collaborator

simonrw commented Jun 8, 2019

Glad to hear that you solved this issue! Please keep us updated on your progress using the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants