Struct lipsum::MarkovChain [−][src]
pub struct MarkovChain<'a> { /* fields omitted */ }
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
impl<'a> MarkovChain<'a>
[src]
impl<'a> MarkovChain<'a>
[src]pub fn new() -> MarkovChain<'a>
[src]
pub fn new() -> MarkovChain<'a>
[src]Create a new empty Markov chain.
Examples
use lipsum::MarkovChain; use rand::SeedableRng; use rand_chacha::ChaCha20Rng; let mut chain = MarkovChain::new(); chain.learn("infra-red red orange yellow green blue indigo x-ray"); let mut rng = ChaCha20Rng::seed_from_u64(0); // The chain jumps consistently like this: assert_eq!(chain.generate_with_rng(&mut rng, 1), "Orange."); assert_eq!(chain.generate_with_rng(&mut rng, 1), "Infra-red."); assert_eq!(chain.generate_with_rng(&mut rng, 1), "Yellow.");
pub fn learn(&mut self, sentence: &'a str)
[src]
pub 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"]));
pub fn len(&self) -> usize
[src]
pub 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);
pub fn is_empty(&self) -> bool
[src]
pub 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());
pub fn words(&self, state: Bigram<'a>) -> Option<&Vec<&str>>
[src]
pub 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);
pub fn generate_with_rng<R: Rng>(&self, rng: R, n: usize) -> String
[src]
pub fn generate_with_rng<R: Rng>(&self, rng: R, 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
generated using the specified random number generator,
and a .
will be added as necessary to form a full sentence.
See generate_with_rng_from
if you want to control the
starting point for the generated text and see iter_with_rng
if you simply want a sequence of words.
Examples
Generating the sounds of a grandfather clock:
use lipsum::MarkovChain; use rand::thread_rng; let mut chain = MarkovChain::new(); chain.learn("Tick, Tock, Tick, Tock, Ding! Tick, Tock, Ding! Ding!"); println!("{}", chain.generate_with_rng(thread_rng(), 15));
The output looks like this:
Ding! Tick, Tock, Tick, Tock, Ding! Ding! Tock, Ding! Tick, Tock, Tick, Tock, Tick, Tock.
pub fn generate(&self, n: usize) -> String
[src]
pub fn generate(&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
generated using the default random number generator 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.
pub fn generate_with_rng_from<R: Rng>(
&self,
rng: R,
n: usize,
from: Bigram<'a>
) -> String
[src]
pub fn generate_with_rng_from<R: Rng>(
&self,
rng: R,
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_with_rng
if the starting point is not important. See
iter_with_rng_from
if you want a sequence of words that you can
format yourself.
pub fn generate_from(&self, n: usize, from: Bigram<'a>) -> String
[src]
pub fn generate_from(&self, n: usize, from: Bigram<'a>) -> String
[src]pub fn iter_with_rng<R: Rng>(&self, rng: R) -> Words<'_, R>ⓘ
[src]
pub fn iter_with_rng<R: Rng>(&self, rng: R) -> Words<'_, R>ⓘ
[src]Make a never-ending iterator over the words in the Markov chain. The iterator starts at a random point in the chain.
pub fn iter(&self) -> Words<'_, ThreadRng>ⓘ
[src]
pub fn iter(&self) -> Words<'_, ThreadRng>ⓘ
[src]Make a never-ending iterator over the words in the Markov chain. The iterator starts at a random point in the chain.
pub fn iter_with_rng_from<R: Rng>(
&self,
rng: R,
from: Bigram<'a>
) -> Words<'_, R>ⓘ
[src]
pub fn iter_with_rng_from<R: Rng>(
&self,
rng: R,
from: Bigram<'a>
) -> Words<'_, R>ⓘ
[src]Make a never-ending iterator over the words in the Markov chain. The iterator starts at the given bigram.
Trait Implementations
impl<'a> Clone for MarkovChain<'a>
[src]
impl<'a> Clone for MarkovChain<'a>
[src]fn clone(&self) -> MarkovChain<'a>
[src]
fn clone(&self) -> MarkovChain<'a>
[src]Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]Performs copy-assignment from source
. Read more
impl<'a> Debug for MarkovChain<'a>
[src]
impl<'a> Debug for MarkovChain<'a>
[src]impl<'a> Default for MarkovChain<'a>
[src]
impl<'a> Default for MarkovChain<'a>
[src]fn default() -> MarkovChain<'a>
[src]
fn default() -> MarkovChain<'a>
[src]Returns the “default value” for a type. Read more
Auto Trait Implementations
impl<'a> RefUnwindSafe for MarkovChain<'a>
impl<'a> Send for MarkovChain<'a>
impl<'a> Sync for MarkovChain<'a>
impl<'a> Unpin for MarkovChain<'a>
impl<'a> UnwindSafe for MarkovChain<'a>
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,