12 releases

0.1.11 Jan 22, 2023
0.1.10 Jan 21, 2023
0.1.6 Dec 27, 2022

#66 in Visualization

Download history 91/week @ 2022-11-30 41/week @ 2022-12-07 10/week @ 2022-12-14 35/week @ 2022-12-21 14/week @ 2022-12-28 7/week @ 2023-01-04 30/week @ 2023-01-11 96/week @ 2023-01-18 36/week @ 2023-01-25

173 downloads per month

Unlicense

305KB
608 lines

wordcloud-rs

A Rust library to generate word-clouds from text and images!

Example

Code

use std::collections::HashMap;
use std::fs;
use lazy_static::lazy_static;
use regex::Regex;
use wordcloud_rs::*;

lazy_static! {
    static ref RE_TOKEN: Regex = Regex::new(r"\w+").unwrap();
}

fn tokenize(text: String) -> Vec<(Token, f32)> {
    let mut counts: HashMap<String, usize> = HashMap::new();
    for token in RE_TOKEN.find_iter(&text) {
        *counts.entry(token.as_str().to_string()).or_default() += 1;
    }
    counts.into_iter().map(|(k, v)| (Token::Text(k), v as f32)).collect()
}

fn main() {
    // Prepare the tokens
    let text = fs::read_to_string("assets/sample_text.txt").unwrap();
    let mut tokens = tokenize(text);
    tokens.push((Token::from("assets/alan_turing.jpg"), 15.));
    tokens.push((Token::from("assets/turing_statue_bletchley.jpg"), 20.));
    tokens.push((Token::from("assets/computer_emoji.png"), 10.));
    // Generate the word-cloud
    let wc = WordCloud::new().generate(tokens);
    // Save it
    wc.save("sample_cloud.png").unwrap();
}

Output

word_cloud_demo

Dependencies

~17MB
~234K SLoC