Coder Social home page Coder Social logo

inspirateur / wordcloud-rs Goto Github PK

View Code? Open in Web Editor NEW
11.0 1.0 4.0 2.52 MB

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

Home Page: https://crates.io/crates/wordcloud-rs

License: The Unlicense

Rust 100.00%
rust rust-crate rust-library wordcloud wordcloud-generator wordcloud-visualization

wordcloud-rs's Introduction

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::Text("๐Ÿ’ป".to_string()), 20.));
    // Generate the word-cloud
    let wc = WordCloud::new().generate(tokens);
    // Save it
    wc.save("sample_cloud.png").unwrap();
}

Output

word_cloud_demo

wordcloud-rs's People

Contributors

inspirateur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

wordcloud-rs's Issues

adding emojis broke wasm build

with f3adda1 and adding twemoji-rs unconditionally as a dependency wordcloud-rs does not longer run in wasm environments anymore. unfortunately it will only break at runtime with:

panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Unsupported, message: "operation not supported on this platform" }', /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/twemoji-rs-0.1.2/src/lib.rs:10:37

could you make emoji support conditional so it could be disabled via features?

SVG output

Thanks for this crate!

I am looking to use this crate in my rust full stack web application live-ask is it possible to have it generate SVG instead of PNG?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.