#markov-chain #sequence #structure

gmarkov-lib

A library that provides Markov chain data structures

2 releases

0.1.1 May 8, 2019
0.1.0 May 7, 2019

#33 in #markov-chain


Used in gmarkov

MIT license

9KB
127 lines

gmarkov-lib

pipeline

This library provides the MarkovChain data structure. The markov chains are fed by many finite sequence of items and outputs a sequence to try and imitate the training data. In my implementation, the input must be an Iterator where item is Eq + Hash + Clone (or its alias ChainItem).

Most commonly this library is used for sequences of chars, but can be used for almost any datatype, like numbers or enums.

For more information see the documentation: https://docs.rs/gmarkov-lib


lib.rs:

A library that provides Markov chain data structures.

The MarkovChain structure allows you to feed in several sequences of items and get out a sequence that looks very similar, but is randomly generated.

This is the library to my CLI app gmarkov.

Example

extern crate gmarkov_lib;

use std::fs::File;
use std::io::{Result, BufRead, BufReader};
use gmarkov_lib::MarkovChain;

fn main() -> Result<()> {
    let mut chain = MarkovChain::with_order::<char>(2);

    let reader = BufReader::new(File::open("examples/dictionary_sample.txt")?);
    for line in reader.lines() {
        chain.feed(line?.chars());
    }

    println!("New word: {}", chain.into_iter().collect::<String>());

    Ok(())
}

The short program above will create a Markov chain of order 2, then feed it 100 random words from the dictionary (in examples/dictionary_sample.txt), then print out one new random word.

Dependencies

~570–800KB
~11K SLoC