wordcloud-rs

Rust library to generate word cloud images from text and images !

Example

Code

```rs use std::collections::HashMap; use std::fs; use lazystatic::lazystatic; use regex::Regex; use wordcloud_rs::*;

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

fn tokenize(text: String) -> Vec<(Token, f32)> { let mut counts: HashMap = HashMap::new(); for token in RETOKEN.finditer(&text) { *counts.entry(token.asstr().tostring()).ordefault() += 1; } counts.intoiter().map(|(k, v)| (Token::Text(k), v as f32)).collect() }

fn main() { // Prepare the tokens let text = fs::readtostring("assets/sampletext.txt").unwrap(); let mut tokens = tokenize(text); tokens.push((Token::Img("assets/alanturing.jpg".tostring()), 60.)); tokens.push((Token::Img("assets/turingstatuebletchley.jpg".tostring()), 80.)); tokens.push((Token::Img("assets/computeremoji.png".tostring()), 40.)); // Generate the word-cloud let wc = WordCloud::new().generate(tokens); // Save it wc.save("sample_cloud.png").unwrap(); } ```

Output

word<em>cloud</em>demo