#markov-chain #markov #chain #generation #probability

markov_rs

A simple and fast Markov chain generator in Rust

3 releases

0.1.2 Dec 13, 2021
0.1.1 Nov 11, 2021
0.1.0 Nov 10, 2021

#1837 in Algorithms

MIT/Apache

70KB
134 lines

markov_rs

markov_rs Crates.io docs.rs Crates.io

A simple and fast Markov Chain generator in Rust.

By using the Walker's Alias Method (WAM), a weighted random sampling algorithm, the model can generate elements very quickly. For details about the WAM, see ichi-h / weighted_rand.

Benchmark

The benchmark for this crate and the Markov Chain using the Cumulative Distribution Function (CDF) is as follows.

Environment

  • OS: Windows 11 Home Insider Preview
  • CPU: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  • RAM: 16.0 GB
  • Rust version: rustc 1.56.1
  • Source code: benchmark.rs

Create Markov Chain model

The time to create a Markov Chain model from each of the dummy texts of 100, 500, and 1000 words.

create_model

For both models, the generation time increases as the number of words in the text increases, but the slope of the time is larger for the WAM.

Generate elements

The time to generate 10 elements from the above models.

generate_elements

The CDF is slower to generate elements as the number of words in the text increases, but the WAM remains fast regardless.

Usage

Add this to your Cargo.toml:

[dependencies]
markov_rs = "0.1"

Example

use markov_rs::MarkovChain;

fn main() {
    let text = [
        "I", "think", "that", "that", "that", "that", "that", "boy", "wrote", "is", "wrong",
    ];
    let mut model = MarkovChain::from(&text);
    for _ in 0..20 {
        print!("{} ", model.next());
    }
}

Planned

  • Improve performance of model building.
  • Support for Nth-order Markov Chain.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.6–1.3MB
~27K SLoC