Skip to content

Highly customizable Rust library for building Markov chains and generating random data from them

License

Notifications You must be signed in to change notification settings

taylordotfish/markov-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

markov-generator

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

Chain implements Serde’s Serialize and Deserialize traits, so you can reuse chains without having to regenerate them every time (which can be a lengthy process).

Example

use markov_generator::{AddEdges, HashChain};

const DEPTH: usize = 6;
// Maps each sequence of 6 items to a list of possible next items.
// `HashChain` uses hash maps internally; b-trees can also be used.
let mut chain = HashChain::new(DEPTH);

// In this case, corpus.txt contains one paragraph per line.
let file = File::open("examples/corpus.txt")?;
let mut reader = BufReader::new(file);
let mut line = String::new();
// The previous `DEPTH` characters before `line`.
let mut prev = Vec::<char>::new();

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

    // 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.iter().copied().chain(line.chars().take(DEPTH)),
        AddEdges::Neither,
    );

    if let Some((n, (i, _c))) =
        line.char_indices().rev().enumerate().take(DEPTH).last()
    {
        // Keep only the most recent `DEPTH` characters.
        prev.drain(0..prev.len().saturating_sub(DEPTH - n - 1));
        prev.extend(line[i..].chars());
    }
    line.clear();
}

// Generate and print random Markov data.
let mut stdout = BufWriter::new(io::stdout().lock());
let mut prev_newline = false;
for &c in chain.generate() {
    if prev_newline {
        writeln!(stdout)?;
    }
    prev_newline = c == '\n';
    write!(stdout, "{c}")?;
}
stdout.flush()?;

Crate features

  • std (default: enabled): Use std. If disabled, this crate is marked no_std.
  • serde (default: enabled): Implement Serde’s Serialize and Deserialize traits for Chain.

Documentation

Documentation is available on docs.rs.

License

markov-generator is licensed under version 3 of the GNU General Public License, or (at your option) any later version. See LICENSE.

Contributing

By contributing to markov-generator, you agree that your contribution may be used according to the terms of markov-generator’s license.

About

Highly customizable Rust library for building Markov chains and generating random data from them

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages