Struct levenshtein_automata::DFA
source · [−]pub struct DFA { /* private fields */ }
Expand description
Implementation of a Deterministic Finite Automaton for a Levenshtein Automaton targeting UTF-8 encoded strings.
The automaton does not validate utf-8
.
It will not return errors when fed with invalid utf-8
The only sink
state is guaranteed to be SINK
.
This means that if you reach the sink state you are guaranteed that regardless of the sequence of bytes you might consume in the future, you will always remain in the same state.
This property can be exploited to abort further evaluation.
Usage
let mut state = dfa.initial_state();
for &byte in str.as_bytes() {
state = dfa.transition(state, byte);
if state == SINK_STATE {
break;
}
}
let distance = dfa.distance(state);
Implementations
sourceimpl DFA
impl DFA
sourcepub fn initial_state(&self) -> u32
pub fn initial_state(&self) -> u32
Returns the initial state
sourcepub fn eval<B: AsRef<[u8]>>(&self, text: B) -> Distance
pub fn eval<B: AsRef<[u8]>>(&self, text: B) -> Distance
Helper function that consumes all of the bytes a sequence of bytes and returns the resulting distance.
sourcepub fn distance(&self, state_id: u32) -> Distance
pub fn distance(&self, state_id: u32) -> Distance
Returns the Levenshtein distance associated to the current state.
sourcepub fn num_states(&self) -> usize
pub fn num_states(&self) -> usize
Returns the number of states in the DFA
.
sourcepub fn transition(&self, from_state_id: u32, b: u8) -> u32
pub fn transition(&self, from_state_id: u32, b: u8) -> u32
Returns the destination state reached after consuming a given byte.