Struct Chain

Source
pub struct Chain<T>
where T: Clone + Chainable,
{ /* private fields */ }
Expand description

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);

Implementations§

Source§

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

Source

pub 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);
Source

pub fn order(&self) -> usize

Gets the order of the markov chain. This is static from chain to chain.

Source

pub 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![]);
Source

pub 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);
Source

pub fn generate(&self) -> Vec<T>

Generates a string of items with no maximum limit. This is equivalent to generate_limit(-1).

Source

pub 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.

Source§

impl Chain<String>

String-specific implementation of the chain. Contains some special string- specific functions.

Source

pub 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.

Source

pub fn generate_sentence(&self) -> String

Generates a sentence, which are ended by “break” strings or null links. “Break” strings are: ., ?, !, .", !", ?", ,"

Source

pub fn generate_paragraph(&self, sentences: usize) -> String

Generates a paragraph of N sentences. Each sentence is broken off by N spaces.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Chain<T>

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<T> Debug for Chain<T>
where T: Clone + 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: Clone + 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: Clone + 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: Clone + 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: Clone + 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> 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.
Source§

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