Trait rustfst::fst_traits::MutableFst

source ·
pub trait MutableFst<W: Semiring>: ExpandedFst<W> {
Show 36 methods // Required methods fn new() -> Self; fn set_start(&mut self, state_id: StateId) -> Result<()>; unsafe fn set_start_unchecked(&mut self, state: StateId); fn set_final<S: Into<W>>( &mut self, state_id: StateId, final_weight: S ) -> Result<()>; unsafe fn set_final_unchecked<S: Into<W>>( &mut self, state_id: StateId, final_weight: S ); fn add_state(&mut self) -> StateId; fn add_states(&mut self, n: usize); fn tr_iter_mut(&mut self, state: StateId) -> Result<TrsIterMut<'_, W>>; unsafe fn tr_iter_unchecked_mut( &mut self, state_id: StateId ) -> TrsIterMut<'_, W>; fn del_state(&mut self, state_id: StateId) -> Result<()>; fn del_states<T: IntoIterator<Item = StateId>>( &mut self, states: T ) -> Result<()>; fn del_all_states(&mut self); unsafe fn del_trs_id_sorted_unchecked( &mut self, state: StateId, to_del: &[usize] ); fn add_tr(&mut self, source: StateId, tr: Tr<W>) -> Result<()>; unsafe fn add_tr_unchecked(&mut self, state: StateId, tr: Tr<W>); unsafe fn set_trs_unchecked(&mut self, source: StateId, trs: Vec<Tr<W>>); fn delete_final_weight(&mut self, source: StateId) -> Result<()>; unsafe fn delete_final_weight_unchecked(&mut self, source: StateId); fn delete_trs(&mut self, source: StateId) -> Result<()>; fn pop_trs(&mut self, source: StateId) -> Result<Vec<Tr<W>>>; unsafe fn pop_trs_unchecked(&mut self, source: StateId) -> Vec<Tr<W>>; fn take_final_weight(&mut self, state_id: StateId) -> Result<Option<W>>; unsafe fn take_final_weight_unchecked( &mut self, state: StateId ) -> Option<W>; fn sort_trs_unchecked<F: Fn(&Tr<W>, &Tr<W>) -> Ordering>( &mut self, state: StateId, f: F ); unsafe fn unique_trs_unchecked(&mut self, state: StateId); unsafe fn sum_trs_unchecked(&mut self, state: StateId); fn set_properties(&mut self, props: FstProperties); fn set_properties_with_mask( &mut self, props: FstProperties, mask: FstProperties ); // Provided methods fn emplace_tr<S: Into<W>>( &mut self, source: StateId, ilabel: Label, olabel: Label, weight: S, nextstate: StateId ) -> Result<()> { ... } unsafe fn emplace_tr_unchecked<S: Into<W>>( &mut self, state: StateId, ilabel: Label, olabel: Label, weight: S, nextstate: StateId ) { ... } fn closure(&mut self, closure_type: ClosureType) { ... } fn tr_map<M: TrMapper<W>>(&mut self, mapper: &mut M) -> Result<()> { ... } fn compute_and_update_properties( &mut self, mask: FstProperties ) -> Result<FstProperties> { ... } fn compute_and_update_properties_all(&mut self) -> Result<FstProperties> { ... } fn set_symts_from_fst<W2: Semiring, OF: Fst<W2>>(&mut self, other_fst: &OF) { ... } fn relabel_tables( &mut self, old_isymbols: Option<&Arc<SymbolTable>>, new_isymbols: &Arc<SymbolTable>, attach_new_isymbols: bool, old_osymbols: Option<&Arc<SymbolTable>>, new_osymbols: &Arc<SymbolTable>, attach_new_osymbols: bool ) -> Result<()> { ... }
}
Expand description

Trait defining the methods to modify a wFST.

Required Methods§

source

fn new() -> Self

Creates an empty wFST.

source

fn set_start(&mut self, state_id: StateId) -> Result<()>

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.

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));
source

unsafe fn set_start_unchecked(&mut self, state: StateId)

The state with identifier state_id is now the start state.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn set_final<S: Into<W>>( &mut self, state_id: StateId, final_weight: S ) -> Result<()>

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.

let mut fst = VectorFst::<BooleanWeight>::new();
let s1 = fst.add_state();
let s2 = fst.add_state();

assert_eq!(fst.final_weight(s1).unwrap(), None);
assert_eq!(fst.final_weight(s2).unwrap(), None);

fst.set_final(s1, BooleanWeight::one());
assert_eq!(fst.final_weight(s1).unwrap(), Some(BooleanWeight::one()));
assert_eq!(fst.final_weight(s2).unwrap(), None);

fst.set_final(s2, BooleanWeight::one());
assert_eq!(fst.final_weight(s1).unwrap(), Some(BooleanWeight::one()));
assert_eq!(fst.final_weight(s2).unwrap(), Some(BooleanWeight::one()));
source

unsafe fn set_final_unchecked<S: Into<W>>( &mut self, state_id: StateId, final_weight: S )

Set the final weight of the state with state if state_id.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn add_state(&mut self) -> StateId

Adds a new state to the current FST. The identifier of the new state is returned

§Example
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);
source

fn add_states(&mut self, n: usize)

Add n states to the Fst.

source

fn tr_iter_mut(&mut self, state: StateId) -> Result<TrsIterMut<'_, W>>

Return a mutable iterator on the Trs of the state state.

source

unsafe fn tr_iter_unchecked_mut( &mut self, state_id: StateId ) -> TrsIterMut<'_, W>

Return a mutable iterator on the Trs of the state state.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn del_state(&mut self, state_id: StateId) -> Result<()>

Removes a state from an FST. It also removes all the trs starting from another state and reaching this state. An error is raised if the state state_id doesn’t exist.

§Example
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);
source

fn del_states<T: IntoIterator<Item = StateId>>( &mut self, states: T ) -> Result<()>

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
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);
source

fn del_all_states(&mut self)

Remove all the states in the FST. As a result, all the trs are also removed, as well as the start state and all the fina states.

§Example
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);

fst.del_all_states();

assert_eq!(fst.states_iter().count(), 0);
source

unsafe fn del_trs_id_sorted_unchecked( &mut self, state: StateId, to_del: &[usize] )

Remove transitions from the fst at state state. Transitions are specified with their index. The to_del vector MUST be sorted.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn add_tr(&mut self, source: StateId, tr: Tr<W>) -> Result<()>

Adds a transition to the FST. The transition will start in the state source.

§Errors

An error is raised if the state source doesn’t exist.

§Example
let mut fst = VectorFst::<ProbabilityWeight>::new();
let s1 = fst.add_state();
let s2 = fst.add_state();

assert_eq!(fst.num_trs(s1)?, 0);
fst.add_tr(s1, Tr::new(3, 5, 1.2, s2));
assert_eq!(fst.num_trs(s1)?, 1);
source

unsafe fn add_tr_unchecked(&mut self, state: StateId, tr: Tr<W>)

Adds a transition to the FST. The transition will start in the state state.

§Safety

Unsafe behaviour if state is not present in Fst.

source

unsafe fn set_trs_unchecked(&mut self, source: StateId, trs: Vec<Tr<W>>)

Set all the Trs leaving the state state to the parameters trs erasing the Trs previously stored.

Be careful as this function doesn’t update the FstProperties.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn delete_final_weight(&mut self, source: StateId) -> Result<()>

Remove the final weight of a specific state.

source

unsafe fn delete_final_weight_unchecked(&mut self, source: StateId)

Remove the final weight of state state.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn delete_trs(&mut self, source: StateId) -> Result<()>

Deletes all the trs leaving a state.

source

fn pop_trs(&mut self, source: StateId) -> Result<Vec<Tr<W>>>

Remove all trs leaving a state and return them.

source

unsafe fn pop_trs_unchecked(&mut self, source: StateId) -> Vec<Tr<W>>

Remove all the Tr leaving the state state and return them.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn take_final_weight(&mut self, state_id: StateId) -> Result<Option<W>>

Takes the final weight out of the fst, leaving a None in its place.

§Errors

An error is raised if the state with id state_id doesn’t exist.

§Example
let mut fst = VectorFst::<ProbabilityWeight>::new();
let s1 = fst.add_state();
fst.set_final(s1, 1.2)?;

assert_eq!(fst.final_weight(s1)?, Some(ProbabilityWeight::new(1.2)));
let weight = fst.take_final_weight(s1)?;
assert_eq!(weight, Some(ProbabilityWeight::new(1.2)));
assert_eq!(fst.final_weight(s1)?, None);
source

unsafe fn take_final_weight_unchecked(&mut self, state: StateId) -> Option<W>

Takes the final weight out of the fst, leaving a None in its place. This version leads to undefined behaviour if the state doesn’t exist.

§Example
let mut fst = VectorFst::<ProbabilityWeight>::new();
let s1 = fst.add_state();
fst.set_final(s1, 1.2)?;

assert_eq!(fst.final_weight(s1)?, Some(ProbabilityWeight::new(1.2)));
let weight = unsafe {fst.take_final_weight_unchecked(s1)};
assert_eq!(weight, Some(ProbabilityWeight::new(1.2)));
assert_eq!(fst.final_weight(s1)?, None);
§Safety

Unsafe behaviour if state is not present in Fst.

source

fn sort_trs_unchecked<F: Fn(&Tr<W>, &Tr<W>) -> Ordering>( &mut self, state: StateId, f: F )

source

unsafe fn unique_trs_unchecked(&mut self, state: StateId)

Remove duplicate Trs leaving the state state with the same ilabel, olabel, weight and nextstate.

Be careful as this function doesn’t update the FstProperties!

§Safety

Unsafe behaviour if state not present in Fst.

source

unsafe fn sum_trs_unchecked(&mut self, state: StateId)

Merge the Trs leaving the state state with the same ilabel, olabel and nextstate and sum their weights.

Be careful as this function doesn’t update the FstProperties!

§Safety

Unsafe behaviour if state not present in Fst.

source

fn set_properties(&mut self, props: FstProperties)

Set the internal properties of the Fst. All the set properties must be verified by the Fst!

source

fn set_properties_with_mask( &mut self, props: FstProperties, mask: FstProperties )

Set only a subset of the internal properties of the Fst.

Provided Methods§

source

fn emplace_tr<S: Into<W>>( &mut self, source: StateId, ilabel: Label, olabel: Label, weight: S, nextstate: StateId ) -> Result<()>

Adds a transition to the FST. The transition will start in the state source.

§Errors

An error is raised if the state source doesn’t exist.

§Example
let mut fst = VectorFst::<ProbabilityWeight>::new();
let s1 = fst.add_state();
let s2 = fst.add_state();

assert_eq!(fst.num_trs(s1)?, 0);
fst.emplace_tr(s1, 3, 5, 1.2, s2);
assert_eq!(fst.num_trs(s1)?, 1);
source

unsafe fn emplace_tr_unchecked<S: Into<W>>( &mut self, state: StateId, ilabel: Label, olabel: Label, weight: S, nextstate: StateId )

Adds a transition to the FST. The transition will start in the state state.

§Safety

Unsafe behaviour if state is not present in Fst.

source

fn closure(&mut self, closure_type: ClosureType)

This operation computes the concatenative closure. If A transduces string x to y with weight a, then the closure transduces x to y with weight a, xx to yy with weight a ⊗ a, xxx to yyy with weight a ⊗ a ⊗ a, etc. If closure_star then the empty string is transduced to itself with weight 1 as well.

source

fn tr_map<M: TrMapper<W>>(&mut self, mapper: &mut M) -> Result<()>

Maps a transition using a TrMapper object.

source

fn compute_and_update_properties( &mut self, mask: FstProperties ) -> Result<FstProperties>

Compute the properties verified by the Fst (with a mask) and update the internal property bits.

source

fn compute_and_update_properties_all(&mut self) -> Result<FstProperties>

Compute all the properties verified by the Fst and update the internal property bits.

source

fn set_symts_from_fst<W2: Semiring, OF: Fst<W2>>(&mut self, other_fst: &OF)

source

fn relabel_tables( &mut self, old_isymbols: Option<&Arc<SymbolTable>>, new_isymbols: &Arc<SymbolTable>, attach_new_isymbols: bool, old_osymbols: Option<&Arc<SymbolTable>>, new_osymbols: &Arc<SymbolTable>, attach_new_osymbols: bool ) -> Result<()>

Destructively relabel the Fst with new Symbol Tables.

Relabelling refers to the operation where all the labels of an Fst are mapped to the equivalent labels of a new SymbolTable. If the Fst has a label 1 corresponding to the symbol “alpha” in the current symbol table and “alpha” is mapped to 4 in a new SymbolTable, then all the 1 are going to be mapped to 4.

Parameters :

  • old_isymbols: Input SymbolTable used to build the Fst. If None, uses the Input SymbolTable attached to the Fst.
  • new_isymbols: New Input SymbolTable to use.
  • attach_new_isymbols: Whether to attach the new Input SymbolTable to the Fst. If False, the resulting Fst won’t contain any attached Input SymbolTable.
  • old_osymbols: Output SymbolTable used to build the Fst. If None, uses the Output SymbolTable attached to the Fst
  • new_osymbols: New Output SymbolTable to use.
  • attach_new_osymbols: Whether to attach the new Output SymbolTable to the Fst. If False, the resulting Fst won’t contain any attached Output SymbolTable.

Object Safety§

This trait is not object safe.

Implementors§