Struct lipsum::MarkovChain

source ·
pub struct MarkovChain<'a, R: Rng> { /* private fields */ }
Expand description

Simple order two Markov chain implementation.

The Markov chain is a chain of order two, which means that it will use the previous two words (a bigram) when predicting the next word. This is normally enough to generate random text that looks somewhat plausible. The implementation is based on Generating arbitrary text with Markov chains in Rust.

Implementations§

source§

impl<'a> MarkovChain<'a, ThreadRng>

source

pub fn new() -> MarkovChain<'a, ThreadRng>

Create a new empty Markov chain. It will use a default thread-local random number generator.

§Examples
use lipsum::MarkovChain;

let chain = MarkovChain::new();
assert!(chain.is_empty());
source§

impl<'a, R: Rng> MarkovChain<'a, R>

source

pub fn new_with_rng(rng: R) -> MarkovChain<'a, R>

Create a new empty Markov chain that uses the given random number generator.

§Examples
extern crate rand;
extern crate rand_xorshift;

use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use lipsum::MarkovChain;

let rng = XorShiftRng::seed_from_u64(0);
let mut chain = MarkovChain::new_with_rng(rng);
chain.learn("infra-red red orange yellow green blue indigo x-ray");

// The chain jumps consistently like this:
assert_eq!(chain.generate(1), "Yellow.");
assert_eq!(chain.generate(1), "Blue.");
assert_eq!(chain.generate(1), "Green.");
source

pub fn learn(&mut self, sentence: &'a str)

Add new text to the Markov chain. This can be called several times to build up the chain.

§Examples
use lipsum::MarkovChain;

let mut chain = MarkovChain::new();
chain.learn("red green blue");
assert_eq!(chain.words(("red", "green")), Some(&vec!["blue"]));

chain.learn("red green yellow");
assert_eq!(chain.words(("red", "green")), Some(&vec!["blue", "yellow"]));
source

pub fn len(&self) -> usize

Returs the number of states in the Markov chain.

§Examples
use lipsum::MarkovChain;

let mut chain = MarkovChain::new();
assert_eq!(chain.len(), 0);

chain.learn("red orange yellow green blue indigo");
assert_eq!(chain.len(), 4);
source

pub fn is_empty(&self) -> bool

Returns true if the Markov chain has no states.

§Examples
use lipsum::MarkovChain;

let mut chain = MarkovChain::new();
assert!(chain.is_empty());

chain.learn("foo bar baz");
assert!(!chain.is_empty());
source

pub fn words(&self, state: Bigram<'a>) -> Option<&Vec<&str>>

Get the possible words following the given bigram, or None if the state is invalid.

§Examples
use lipsum::MarkovChain;

let mut chain = MarkovChain::new();
chain.learn("red green blue");
assert_eq!(chain.words(("red", "green")), Some(&vec!["blue"]));
assert_eq!(chain.words(("foo", "bar")), None);
source

pub fn generate(&mut self, n: usize) -> String

Generate a sentence with n words of lorem ipsum text. The sentence will start from a random point in the Markov chain and a . will be added as necessary to form a full sentence.

See generate_from if you want to control the starting point for the generated text and see iter if you simply want a sequence of words.

§Examples

Generating the sounds of a grandfather clock:

use lipsum::MarkovChain;

let mut chain = MarkovChain::new();
chain.learn("Tick, Tock, Tick, Tock, Ding! Tick, Tock, Ding! Ding!");
println!("{}", chain.generate(15));

The output looks like this:

Ding! Tick, Tock, Tick, Tock, Ding! Ding! Tock, Ding! Tick, Tock, Tick, Tock, Tick, Tock.

source

pub fn generate_from(&mut self, n: usize, from: Bigram<'a>) -> String

Generate a sentence with n words of lorem ipsum text. The sentence will start from the given bigram and a . will be added as necessary to form a full sentence.

Use generate if the starting point is not important. See iter_from if you want a sequence of words that you can format yourself.

source

pub fn iter(&mut self) -> Words<'_, R>

Make a never-ending iterator over the words in the Markov chain. The iterator starts at a random point in the chain.

source

pub fn iter_from(&mut self, from: Bigram<'a>) -> Words<'_, R>

Make a never-ending iterator over the words in the Markov chain. The iterator starts at the given bigram.

Trait Implementations§

source§

impl<'a> Default for MarkovChain<'a, ThreadRng>

source§

fn default() -> Self

Create a new empty Markov chain. It will use a default thread-local random number generator.

Auto Trait Implementations§

§

impl<'a, R> Freeze for MarkovChain<'a, R>
where R: Freeze,

§

impl<'a, R> RefUnwindSafe for MarkovChain<'a, R>
where R: RefUnwindSafe,

§

impl<'a, R> Send for MarkovChain<'a, R>
where R: Send,

§

impl<'a, R> Sync for MarkovChain<'a, R>
where R: Sync,

§

impl<'a, R> Unpin for MarkovChain<'a, R>
where R: Unpin,

§

impl<'a, R> UnwindSafe for MarkovChain<'a, R>
where R: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.