6 releases

0.1.5 Dec 29, 2021
0.1.4 Mar 29, 2021

#1125 in Text processing


Used in markov_bot

GPL-3.0-only

26KB
353 lines

markov_strings

A simplistic, configurable, Markov chain text generator. Give it an array of strings, and it will output a randomly generated string.

This is a port of the npm library markov-strings, from the same author.

Usage

use markov_strings::*;

// Instantiate the generator
let mut markov = Markov::new();

// Optional: specify a state size
markov.set_state_size(3); // Default: 2

// Feed it data
let data: Vec<InputData> = vec![/* a lot of data */];
markov.add_to_corpus(data);

// Define a results filter
markov
    .set_filter(|r| {
        // A minimal relative score and number of references
        // The thresholds are relative to your input
        r.score > 5 && r.refs.len() > 2
            // We want to generate random tweets
            && r.text.len() <= 280
            // No mentions
            && !r.text.contains("@")
            // No urls
            && !r.text.contains("http")
    })
    .set_max_tries(100);

let result: MarkovResult = markov.generate().unwrap();

Dependencies

~0.6–1.2MB
~27K SLoC