Struct lipsum::MarkovChain
[−]
[src]
pub struct MarkovChain<'a, R: Rng> { /* fields omitted */ }
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.
Methods
impl<'a> MarkovChain<'a, ThreadRng>
[src]
fn new() -> MarkovChain<'a, ThreadRng>
[src]
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());
impl<'a, R: Rng> MarkovChain<'a, R>
[src]
fn new_with_rng(rng: R) -> MarkovChain<'a, R>
[src]
Create a new empty Markov chain that uses the given random number generator.
Examples
extern crate rand; use rand::XorShiftRng; use lipsum::MarkovChain; let rng = XorShiftRng::new_unseeded(); 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), "Green."); assert_eq!(chain.generate(1), "Red.");
fn learn(&mut self, sentence: &'a str)
[src]
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"]));
fn len(&self) -> usize
[src]
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);
fn is_empty(&self) -> bool
[src]
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());
fn words(&self, state: Bigram<'a>) -> Option<&Vec<&str>>
[src]
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);
fn generate(&mut self, n: usize) -> String
[src]
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.
fn generate_from(&mut self, n: usize, from: Bigram<'a>) -> String
[src]
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.
fn iter(&mut self) -> Words
[src]
Make a never-ending iterator over the words in the Markov chain. The iterator starts at a random point in the chain.
fn iter_from(&mut self, from: Bigram<'a>) -> Words
[src]
Make a never-ending iterator over the words in the Markov chain. The iterator starts at the given bigram.