wordmarkov/chain/selectors/
random.rs

1//! Random and weighted random selectors.
2
3use crate::prelude::MarkovTraverseDir;
4
5use super::super::token::MarkovToken;
6use super::interface::{MarkovSelector, SelectionType};
7
8pub struct WeightedRandomSelector;
9
10impl MarkovSelector for WeightedRandomSelector {
11    fn reset(&mut self, _dir: MarkovTraverseDir) {}
12
13    fn weight<'a>(
14        &mut self,
15        _from: &MarkovToken<'a>,
16        _to: &MarkovToken<'a>,
17        _punct: &MarkovToken<'a>,
18        hits: usize,
19    ) -> f32 {
20        hits as f32
21    }
22
23    fn selection_type(&mut self) -> SelectionType {
24        SelectionType::WeightedRandom
25    }
26}
27
28pub struct NaiveRandomSelector;
29
30impl MarkovSelector for NaiveRandomSelector {
31    fn reset(&mut self, _dir: MarkovTraverseDir) {}
32
33    fn weight<'a>(
34        &mut self,
35        _from: &MarkovToken<'a>,
36        _to: &MarkovToken<'a>,
37        _punct: &MarkovToken<'a>,
38        _hits: usize,
39    ) -> f32 {
40        1.0
41    }
42
43    fn selection_type(&mut self) -> SelectionType {
44        SelectionType::WeightedRandom
45    }
46}