Struct MarkovChain

Source
pub struct MarkovChain<I: ChainItem> { /* private fields */ }
Expand description

The Markov Chain data structure

A Markov chain is a statistical model that is used to predict random sequences based on the probability of one symbol coming after another. For a basic introduction you can read this article. For a more technical overview you can read the wikipedia article on the subject.

Items in markov chains in gmarkov-lib must implement the Eq, Hash, and Clone traits for use.

Implementations§

Source§

impl<I: ChainItem> MarkovChain<I>

Source

pub fn with_order<T>(order: usize) -> MarkovChain<I>

Create a Markov chain with order order.

The order specifies how many steps back in the sequence the chain keeps track of. A higher order allows for better results but also requires a much larger dataset.

Examples found in repository?
examples/dictionary.rs (line 8)
7fn main() -> Result<()> {
8    let mut chain = MarkovChain::with_order::<char>(2);
9
10    let reader = BufReader::new(File::open("dictionary_sample.txt")?);
11
12    for line in reader.lines() {
13        chain.feed(line?.chars());
14    }
15
16    println!("New word: {}", chain.into_iter().collect::<String>());
17
18    Ok(())
19}
Source

pub fn feed(&mut self, input: impl Iterator<Item = I>)

Train the Markov chain by “feeding” it the iterator input.

This adds the input to the current Markov chain. The generated output will resemble this input.

Examples found in repository?
examples/dictionary.rs (line 13)
7fn main() -> Result<()> {
8    let mut chain = MarkovChain::with_order::<char>(2);
9
10    let reader = BufReader::new(File::open("dictionary_sample.txt")?);
11
12    for line in reader.lines() {
13        chain.feed(line?.chars());
14    }
15
16    println!("New word: {}", chain.into_iter().collect::<String>());
17
18    Ok(())
19}

Trait Implementations§

Source§

impl<I: Clone + ChainItem> Clone for MarkovChain<I>

Source§

fn clone(&self) -> MarkovChain<I>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I: Debug + ChainItem> Debug for MarkovChain<I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I: ChainItem> IntoIterator for MarkovChain<I>

Source§

fn into_iter(self) -> Self::IntoIter

Transform the Markov chain into an iterator.

You will usually want to clone the Markov chain before turning it into an iterator so that you don’t have to retrain it every time.

Source§

type Item = I

The type of the elements being iterated over.
Source§

type IntoIter = MarkovIterator<I>

Which kind of iterator are we turning this into?

Auto Trait Implementations§

§

impl<I> Freeze for MarkovChain<I>

§

impl<I> RefUnwindSafe for MarkovChain<I>
where I: RefUnwindSafe,

§

impl<I> Send for MarkovChain<I>
where I: Send,

§

impl<I> Sync for MarkovChain<I>
where I: Sync,

§

impl<I> Unpin for MarkovChain<I>
where I: Unpin,

§

impl<I> UnwindSafe for MarkovChain<I>
where I: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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.