Struct markov_chain::Chain
[−]
[src]
pub struct Chain<T> where
T: Clone + Chainable, { /* fields omitted */ }
A struct representing a markov chain.
A markov chain has an order, which determines how many items per node are held. The chain itself is a map of vectors, which point to a map of single elements pointing at a weight.
use markov_chain::Chain; let mut chain = Chain::new(1); // 1 is the order of the chain // Train the chain on some vectors chain.train(vec![1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 1]) .train(vec![5, 4, 3, 2, 1]); // Generate a sequence and print it out let sequence = chain.generate(); println!("{:?} ", sequence);
Methods
impl<T> Chain<T> where
T: Clone + Chainable,
[src]
T: Clone + Chainable,
fn new(order: usize) -> Self
Initializes a new markov chain with a given order.
Examples
use markov_chain::Chain; let chain: Chain<u32> = Chain::new(1);
fn order(&self) -> usize
Gets the order of the markov chain. This is static from chain to chain.
fn train(&mut self, string: Vec<T>) -> &mut Self
Trains a sentence on a string of items.
Examples
use markov_chain::Chain; let mut chain = Chain::new(1); let data = vec![10, 15, 20]; chain.train(data) .train(vec![]);
fn merge(&mut self, other: &Self) -> &mut Self
Merges this markov chain with another.
Examples
use markov_chain::Chain; let mut chain1 = Chain::new(1); let mut chain2 = chain1.clone(); chain1.train(vec![1, 2, 3]); chain2.train(vec![2, 3, 4, 5, 6]) .merge(&chain1);
fn generate(&self) -> Vec<T>
Generates a string of items with no maximum limit.
This is equivalent to generate_limit(-1)
.
fn generate_limit(&self, max: isize) -> Vec<T>
Generates a string of items, based on the training, of up to N items. Specifying a maximum of -1 allows any arbitrary size of list.
impl Chain<String>
[src]
String-specific implementation of the chain. Contains some special string- specific functions.
fn train_string(&mut self, sentence: &str) -> &mut Self
Trains this chain on a single string. Strings are broken into words, which are split by whitespace and punctuation.
fn generate_sentence(&self) -> String
Generates a sentence, which are ended by "break" strings or null links.
"Break" strings are:
.
, ?
, !
, ."
, !"
, ?"
, ,"
fn generate_paragraph(&self, sentences: usize) -> String
Generates a paragraph of N sentences. Each sentence is broken off by N spaces.
Trait Implementations
impl<T: Clone> Clone for Chain<T> where
T: Clone + Chainable,
[src]
T: Clone + Chainable,
fn clone(&self) -> Chain<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T: PartialEq> PartialEq for Chain<T> where
T: Clone + Chainable,
[src]
T: Clone + Chainable,
fn eq(&self, __arg_0: &Chain<T>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Chain<T>) -> bool
This method tests for !=
.
impl<T: Debug> Debug for Chain<T> where
T: Clone + Chainable,
[src]
T: Clone + Chainable,