[][src]Struct chainkov::MarkovChain

pub struct MarkovChain {
    pub transition_prob: HashMap<String, Vec<(String, f32)>>,
}

chainkov Basic Usage

Example

use std::collections::HashMap;

let mut m = chainkov::MarkovChain::new();
assert_eq!(m.transition_prob, HashMap::new());
 
m.add_state_choice("a", ("b".to_string(), 1.0));
let mut new_state = &m.transition_prob.get("a").unwrap()[0];
assert_eq!(new_state.0, "b".to_string());
assert_eq!(new_state.1, 1.0);
 
m.add_state_choice("b", ("c".to_string(), 1.0));
new_state = &m.transition_prob.get("b").unwrap()[0];
assert_eq!(new_state.0, "c".to_string());
assert_eq!(new_state.1, 1.0);
 
m.add_state_choice("c", ("d".to_string(), 1.0));
new_state = &m.transition_prob.get("c").unwrap()[0];
assert_eq!(new_state.0, "d".to_string());
assert_eq!(new_state.1, 1.0);

m.add_state_choice("d", ("a".to_string(), 1.0));
new_state = &m.transition_prob.get("d").unwrap()[0];
assert_eq!(new_state.0, "a".to_string());
assert_eq!(new_state.1, 1.0);
 
let answer_vec = m.generate_states("a".to_string(), 4);
assert_eq!(answer_vec, vec!["b", "c", "d", "a"]);
let mut answer = m.next_state("a".to_string());
assert_eq!(answer, "b");

// incrementing when a state already exists
m.increment_state("a", "b");
new_state = &m.transition_prob.get("a").unwrap()[0];
assert_eq!(new_state.0, "b");
assert_eq!(new_state.1, 2.0);
// incrementing when a key exists but the state transition doesn't
m.increment_state("a", "c");
new_state = &m.transition_prob.get("a").unwrap()[1];
assert_eq!(new_state.0, "c");
assert_eq!(new_state.1, 1.0);

// incrementing when there is a new state in the MarkovChain
m.increment_state("e", "a");
new_state = &m.transition_prob.get("e").unwrap()[0];
assert_eq!(new_state.0, "a");
assert_eq!(new_state.1, 1.0);

Fields

transition_prob: HashMap<String, Vec<(String, f32)>>

Methods

impl MarkovChain[src]

pub fn add_state_choice(&mut self, key: &str, probability: (String, f32))[src]

pub fn increment_state(&mut self, key: &str, state: &str)[src]

pub fn generate_states(
    &self,
    current_state: String,
    num_of_states: u16
) -> Vec<String>
[src]

pub fn new() -> MarkovChain[src]

pub fn next_state(&self, current_state: String) -> String[src]

Trait Implementations

impl Clone for MarkovChain[src]

impl Debug for MarkovChain[src]

impl PartialEq<MarkovChain> for MarkovChain[src]

impl StructuralPartialEq for MarkovChain[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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