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>
impl<I: ChainItem> MarkovChain<I>
Sourcepub fn with_order<T>(order: usize) -> MarkovChain<I>
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?
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}
Sourcepub fn feed(&mut self, input: impl Iterator<Item = I>)
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?
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>
impl<I: Clone + ChainItem> Clone for MarkovChain<I>
Source§fn clone(&self) -> MarkovChain<I>
fn clone(&self) -> MarkovChain<I>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<I: ChainItem> IntoIterator for MarkovChain<I>
impl<I: ChainItem> IntoIterator for MarkovChain<I>
Source§fn into_iter(self) -> Self::IntoIter
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.