Struct Chain

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

Source

pub fn new() -> Chain<T>

Constructs a new Markov chain.

Source

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.

Examples found in repository?
examples/graph.rs (line 7)
6fn main() {
7    let mut chain = markov::Chain::of_order(2);
8    chain.feed(vec!('e', 'r', 't', 'r', 't', 'y', 'r', 't', 'e', 'r', 't', 'y', 't', 'r'));
9    let graph = chain.graph();
10
11    println!("{:?}", petgraph::dot::Dot::new(&graph));
12}
Source

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.

Source

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.

Examples found in repository?
examples/graph.rs (line 8)
6fn main() {
7    let mut chain = markov::Chain::of_order(2);
8    chain.feed(vec!('e', 'r', 't', 'r', 't', 'y', 'r', 't', 'e', 'r', 't', 'y', 't', 'r'));
9    let graph = chain.graph();
10
11    println!("{:?}", petgraph::dot::Dot::new(&graph));
12}
Source

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.

Source

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.

Source

pub fn iter(&self) -> InfiniteChainIterator<'_, T>

Produces an infinite iterator of generated token collections.

Source

pub fn iter_for(&self, size: usize) -> SizedChainIterator<'_, T>

Produces an iterator for the specified number of generated token collections.

Source

pub fn graph(&self) -> Graph<Vec<Option<T>>, f64>

Create a graph using petgraph from the markov chain.

Examples found in repository?
examples/graph.rs (line 9)
6fn main() {
7    let mut chain = markov::Chain::of_order(2);
8    chain.feed(vec!('e', 'r', 't', 'r', 't', 'y', 'r', 't', 'e', 'r', 't', 'y', 't', 'r'));
9    let graph = chain.graph();
10
11    println!("{:?}", petgraph::dot::Dot::new(&graph));
12}
Source§

impl<T> Chain<T>
where T: Chainable + Serialize,

Source

pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()>

Saves the current chain to the specified path.

Source§

impl<T> Chain<T>

Source

pub fn load<P: AsRef<Path>>(path: P) -> Result<Chain<T>>

Loads a chain from the specified path.

Source§

impl Chain<String>

Source

pub fn feed_str(&mut self, string: &str) -> &mut Chain<String>

Feeds a string of text into the chain.

Source

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.

Source

pub fn generate_str(&self) -> String

Generates a random string of text.

Source

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.

Source

pub fn str_iter(&self) -> InfiniteChainStringIterator<'_>

Produces an infinite iterator of generated strings.

Source

pub fn str_iter_for(&self, size: usize) -> SizedChainStringIterator<'_>

Produces a sized iterator of generated strings.

Trait Implementations§

Source§

impl<T> Debug for Chain<T>
where T: Chainable + Debug,

Source§

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

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

impl<'de, T> Deserialize<'de> for Chain<T>
where T: Chainable + Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> PartialEq for Chain<T>
where T: Chainable + PartialEq,

Source§

fn eq(&self, other: &Chain<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for Chain<T>
where T: Chainable + Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> StructuralPartialEq for Chain<T>
where T: Chainable,

Auto Trait Implementations§

§

impl<T> Freeze for Chain<T>

§

impl<T> RefUnwindSafe for Chain<T>
where T: RefUnwindSafe,

§

impl<T> Send for Chain<T>
where T: Send,

§

impl<T> Sync for Chain<T>
where T: Sync,

§

impl<T> Unpin for Chain<T>
where T: Unpin,

§

impl<T> UnwindSafe for Chain<T>
where T: 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> 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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,