markov-generator

Crates.iomarkov-generator
lib.rsmarkov-generator
version
sourcesrc
created_at2022-08-24 09:59:20.815819
updated_at2024-12-07 06:44:49.274404
descriptionHighly customizable crate for building Markov chains and generating data from them
homepage
repositoryhttps://github.com/taylordotfish/markov-generator
max_upload_size
id651581
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
taylor.fish (taylordotfish)

documentation

https://docs.rs/markov-generator

README

markov-generator

A highly customizable Rust library for building Markov chains and generating sequences of data from them.

Chain implements Serde’s Serialize and Deserialize traits, so you can use a chain multiple times without having to regenerate it every time (which can be a lengthy process).

Example

use markov_generator::{AddEdges, Chain};

const DEPTH: usize = 6;
// Maps each sequence of 6 items to a list of possible next items.
let mut chain = Chain::new(DEPTH);

// In this case, corpus.txt contains one paragraph per line.
let file = File::open("examples/corpus.txt").unwrap();
let mut reader = BufReader::new(file);
let mut line = String::new();
let mut prev_line = String::new();

while let Ok(1..) = reader.read_line(&mut line) {
    // `Both` means that the generated random output could start with the
    // beginning of `line`, and that the generated output could end after
    // the end of `line`.
    chain.add_all(line.chars(), AddEdges::Both);

    // Starting index of last `DEPTH` chars in `prev_line`.
    let prev_tail =
        prev_line.char_indices().nth_back(DEPTH - 1).map_or(0, |(i, _)| i);

    // This makes sure there's a chance that the end of the previous line
    // could be followed by the start of the current line when generating
    // random output.
    chain.add_all(
        prev_line[prev_tail..].chars().chain(line.chars().take(DEPTH)),
        AddEdges::Neither,
    );

    std::mem::swap(&mut line, &mut prev_line);
    line.clear();
}

// Generate and print random Markov data.
let output: String = chain
    .generate()
    .flat_map(|c| iter::repeat(c).take(1 + (*c == '\n') as usize))
    .collect();
print!("{}", &output[..output.len() - 1]);

Crate features

  • std (default: enabled): Use std. If disabled, this crate is marked no_std.
  • hash (default: enabled): Use hash maps internally. If disabled, B-trees will be used instead. This feature requires std.
  • serde (default: enabled): Implement Serde’s Serialize and Deserialize traits for Chain.
Commit count: 10

cargo fmt