pub struct Chain { /* private fields */ }Expand description
A Markov chain representing processes that have both beginnings and ends.
The chain is represented as a HashMap where keys are states (tuples of words) and values are HashMaps of possible next words with their frequencies.
Implementations§
Source§impl Chain
impl Chain
Sourcepub fn new(corpus: &[Vec<String>], state_size: usize) -> Self
pub fn new(corpus: &[Vec<String>], state_size: usize) -> Self
Create a new Markov chain from a corpus
§Arguments
corpus- A list of runs, where each run is a sequence of items (e.g., words in a sentence)state_size- The number of items used to represent the chain’s state
Sourcepub fn compile(&self) -> Self
pub fn compile(&self) -> Self
Compile the chain for faster generation
This converts the frequency dictionaries into cumulative frequency lists for more efficient random selection.
Sourcepub fn gen(&self, init_state: Option<&[String]>) -> ChainGenerator<'_> ⓘ
pub fn gen(&self, init_state: Option<&[String]>) -> ChainGenerator<'_> ⓘ
Generate items from the chain
Returns an iterator that yields successive items until the END state is reached.
Sourcepub fn walk(&self, init_state: Option<&[String]>) -> Vec<String>
pub fn walk(&self, init_state: Option<&[String]>) -> Vec<String>
Walk the chain and return a complete sequence
Returns a vector representing a single run of the Markov model.
Sourcepub fn state_size(&self) -> usize
pub fn state_size(&self) -> usize
Get the state size
Sourcepub fn model(&self) -> &FxHashMap<Vec<String>, FxHashMap<String, usize>>
pub fn model(&self) -> &FxHashMap<Vec<String>, FxHashMap<String, usize>>
Get the model (for inspection or combination)
Sourcepub fn is_compiled(&self) -> bool
pub fn is_compiled(&self) -> bool
Check if the chain is compiled
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Chain
impl<'de> Deserialize<'de> for Chain
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Chain
impl RefUnwindSafe for Chain
impl Send for Chain
impl Sync for Chain
impl Unpin for Chain
impl UnsafeUnpin for Chain
impl UnwindSafe for Chain
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more