pub struct Chain<T>where
T: Chainable,{ /* private fields */ }
Expand description
A generic Markov chain for almost any type.
In particular, elements of the chain must be Eq
, Hash
, and Clone
.
Implementations§
Source§impl<T> Chain<T>where
T: Chainable,
impl<T> Chain<T>where
T: Chainable,
Sourcepub fn of_order(order: usize) -> Chain<T>
pub fn of_order(order: usize) -> Chain<T>
Creates a new Markov chain of the specified order. The order is the number of previous tokens to use for each mapping in the chain. Higher orders mean that the generated text will more closely resemble the training set. Increasing the order can yield more realistic output, but typically at the cost of requiring more training data.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Determines whether or not the chain is empty. A chain is considered empty if nothing has been fed into it.
Sourcepub fn feed<S: AsRef<[T]>>(&mut self, tokens: S) -> &mut Chain<T>
pub fn feed<S: AsRef<[T]>>(&mut self, tokens: S) -> &mut Chain<T>
Feeds the chain a collection of tokens. This operation is O(n)
where n
is the number of
tokens to be fed into the chain.
Sourcepub fn generate(&self) -> Vec<T>
pub fn generate(&self) -> Vec<T>
Generates a collection of tokens from the chain. This operation is O(mn)
where m
is the
length of the generated collection, and n
is the number of possible states from a given
state.
Sourcepub fn generate_from_token(&self, token: T) -> Vec<T>
pub fn generate_from_token(&self, token: T) -> Vec<T>
Generates a collection of tokens from the chain, starting with the given token. This operation is O(mn) where m is the length of the generated collection, and n is the number of possible states from a given state. This returns an empty vector if the token is not found.
Sourcepub fn iter(&self) -> InfiniteChainIterator<'_, T> ⓘ
pub fn iter(&self) -> InfiniteChainIterator<'_, T> ⓘ
Produces an infinite iterator of generated token collections.
Sourcepub fn iter_for(&self, size: usize) -> SizedChainIterator<'_, T> ⓘ
pub fn iter_for(&self, size: usize) -> SizedChainIterator<'_, T> ⓘ
Produces an iterator for the specified number of generated token collections.
Source§impl<T> Chain<T>where
T: Chainable + DeserializeOwned,
impl<T> Chain<T>where
T: Chainable + DeserializeOwned,
Source§impl Chain<String>
impl Chain<String>
Sourcepub fn feed_str(&mut self, string: &str) -> &mut Chain<String>
pub fn feed_str(&mut self, string: &str) -> &mut Chain<String>
Feeds a string of text into the chain.
Sourcepub fn feed_file<P: AsRef<Path>>(
&mut self,
path: P,
) -> Result<&mut Chain<String>>
pub fn feed_file<P: AsRef<Path>>( &mut self, path: P, ) -> Result<&mut Chain<String>>
Feeds a properly formatted file into the chain. This file should be formatted such that each line is a new sentence. Punctuation may be included if it is desired.
Sourcepub fn generate_str(&self) -> String
pub fn generate_str(&self) -> String
Generates a random string of text.
Sourcepub fn generate_str_from_token(&self, string: &str) -> String
pub fn generate_str_from_token(&self, string: &str) -> String
Generates a random string of text starting with the desired token. This returns an empty string if the token is not found.
Sourcepub fn str_iter(&self) -> InfiniteChainStringIterator<'_>
pub fn str_iter(&self) -> InfiniteChainStringIterator<'_>
Produces an infinite iterator of generated strings.
Sourcepub fn str_iter_for(&self, size: usize) -> SizedChainStringIterator<'_>
pub fn str_iter_for(&self, size: usize) -> SizedChainStringIterator<'_>
Produces a sized iterator of generated strings.