Trait rustfst::fst_traits::MutableFst
source · pub trait MutableFst: CoreFst + for<'a> MutableArcIterator<'a> {
fn new() -> Self;
fn set_start(&mut self, state_id: &StateId) -> Result<(), Error>;
fn set_final(
&mut self,
state_id: &StateId,
final_weight: <Self as CoreFst>::W
) -> Result<(), Error>;
fn add_state(&mut self) -> StateId;
fn del_state(&mut self, state_id: &StateId) -> Result<(), Error>;
fn del_states<T: IntoIterator<Item = StateId>>(
&mut self,
states: T
) -> Result<(), Error>;
fn add_arc(
&mut self,
source: &StateId,
arc: Arc<<Self as CoreFst>::W>
) -> Result<(), Error>;
fn add_fst<F: ExpandedFst<W = Self::W>>(
&mut self,
fst_to_add: &F
) -> Result<HashMap<StateId, StateId>, Error> { ... }
}
Expand description
Trait defining the methods to modify a wFST
Required Methods
sourcefn set_start(&mut self, state_id: &StateId) -> Result<(), Error>
fn set_start(&mut self, state_id: &StateId) -> Result<(), Error>
The state with identifier state_id
is now the start state.
Note that only one start state is allowed in this implementation. Calling this function twice
will mean losing the first start state.
If the state_id
doesn’t exist an error is raised.
use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::{BooleanWeight, Semiring};
use rustfst::arc::Arc;
let mut fst = VectorFst::<BooleanWeight>::new();
let s1 = fst.add_state();
let s2 = fst.add_state();
assert_eq!(fst.start(), None);
fst.set_start(&s1);
assert_eq!(fst.start(), Some(s1));
fst.set_start(&s2);
assert_eq!(fst.start(), Some(s2));
sourcefn set_final(
&mut self,
state_id: &StateId,
final_weight: <Self as CoreFst>::W
) -> Result<(), Error>
fn set_final(
&mut self,
state_id: &StateId,
final_weight: <Self as CoreFst>::W
) -> Result<(), Error>
The state with identifier state_id
is now a final state with a weight final_weight
.
If the state_id
doesn’t exist an error is raised.
use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::{BooleanWeight, Semiring};
use rustfst::arc::Arc;
let mut fst = VectorFst::<BooleanWeight>::new();
let s1 = fst.add_state();
let s2 = fst.add_state();
assert_eq!(fst.final_weight(&s1), None);
assert_eq!(fst.final_weight(&s2), None);
fst.set_final(&s1, BooleanWeight::one());
assert_eq!(fst.final_weight(&s1), Some(BooleanWeight::one()));
assert_eq!(fst.final_weight(&s2), None);
fst.set_final(&s2, BooleanWeight::one());
assert_eq!(fst.final_weight(&s1), Some(BooleanWeight::one()));
assert_eq!(fst.final_weight(&s2), Some(BooleanWeight::one()));
sourcefn add_state(&mut self) -> StateId
fn add_state(&mut self) -> StateId
Adds a new state to the current FST. The identifier of the new state is returned
Example
use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::{BooleanWeight, Semiring};
let mut fst = VectorFst::<BooleanWeight>::new();
assert_eq!(fst.num_states(), 0);
fst.add_state();
assert_eq!(fst.num_states(), 1);
fst.add_state();
assert_eq!(fst.num_states(), 2);
sourcefn del_state(&mut self, state_id: &StateId) -> Result<(), Error>
fn del_state(&mut self, state_id: &StateId) -> Result<(), Error>
Removes a state from an FST. It also removes all the arcs starting from another state and
reaching this state. An error is raised if the state state_id
doesn’t exist.
Example
use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst, StateIterator};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::{BooleanWeight, Semiring};
let mut fst = VectorFst::<BooleanWeight>::new();
assert_eq!(fst.states_iter().count(), 0);
let s1 = fst.add_state();
assert_eq!(fst.states_iter().count(), 1);
fst.del_state(&s1);
assert_eq!(fst.states_iter().count(), 0);
sourcefn del_states<T: IntoIterator<Item = StateId>>(
&mut self,
states: T
) -> Result<(), Error>
fn del_states<T: IntoIterator<Item = StateId>>(
&mut self,
states: T
) -> Result<(), Error>
Removes multiple states from an FST. If one of the states doesn’t exist, an error is raised.
Warning
This method modifies the id of the states that are left in the FST. Id that were used before calling this function should no longer be used.
Example
use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst, StateIterator};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::{BooleanWeight, Semiring};
let mut fst = VectorFst::<BooleanWeight>::new();
assert_eq!(fst.states_iter().count(), 0);
let s1 = fst.add_state();
let s2 = fst.add_state();
assert_eq!(fst.states_iter().count(), 2);
let states_to_remove = vec![s1, s2];
fst.del_states(states_to_remove.into_iter());
assert_eq!(fst.states_iter().count(), 0);
sourcefn add_arc(
&mut self,
source: &StateId,
arc: Arc<<Self as CoreFst>::W>
) -> Result<(), Error>
fn add_arc(
&mut self,
source: &StateId,
arc: Arc<<Self as CoreFst>::W>
) -> Result<(), Error>
Adds an arc to the FST. The arc will start in the state source
.
An error is raised if the state source
doesn’t exist.
Warning
This method modifies the id of the states that are left in the FST. Id that were used before calling this function should no longer be used.
Example
use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::{BooleanWeight, Semiring};
use rustfst::arc::Arc;
let mut fst = VectorFst::<BooleanWeight>::new();
let s1 = fst.add_state();
let s2 = fst.add_state();
assert_eq!(fst.num_arcs(), 0);
fst.add_arc(&s1, Arc::new(3, 5, BooleanWeight::new(true), s2));
assert_eq!(fst.num_arcs(), 1);