1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::fmt::{Debug, Display};
use failure::Fallible;
use crate::algorithms::arc_filters::{ArcFilter, InputEpsilonArcFilter, OutputEpsilonArcFilter};
use crate::arc::Arc;
use crate::semirings::Semiring;
use crate::StateId;
/// Trait defining necessary methods for a wFST to access start states and final states.
pub trait CoreFst {
/// Weight use in the wFST. This type must implement the Semiring trait.
type W: Semiring;
/// Returns the ID of the start state of the wFST if it exists else none.
///
/// # Example
///
/// ```
/// # use rustfst::fst_traits::{CoreFst, MutableFst};
/// # use rustfst::fst_impls::VectorFst;
/// # use rustfst::semirings::BooleanWeight;
/// // 1 - Create an FST
/// let mut fst = VectorFst::<BooleanWeight>::new();
/// let s = fst.add_state();
/// fst.set_start(s);
///
/// // 2 - Access the start state
/// let start_state = fst.start();
/// assert_eq!(start_state, Some(s));
/// ```
fn start(&self) -> Option<StateId>;
/// Retrieves the final weight of a state (if the state is a final one).
///
/// # Example
///
/// ```
/// # use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
/// # use rustfst::fst_impls::VectorFst;
/// # use rustfst::semirings::{BooleanWeight, Semiring};
/// // 1 - Create an FST
/// let mut fst = VectorFst::<BooleanWeight>::new();
/// let s1 = fst.add_state();
/// let s2 = fst.add_state();
/// fst.set_final(s2, BooleanWeight::one());
///
/// // 2 - Access the final weight of each state
/// assert_eq!(fst.final_weight(s1), None);
/// assert_eq!(fst.final_weight(s2), Some(BooleanWeight::one()));
/// ```
fn final_weight(&self, state_id: StateId) -> Option<<Self as CoreFst>::W>;
/// Number of arcs leaving a specific state in the wFST.
///
/// # Example
///
/// ```
/// # use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
/// # use rustfst::fst_impls::VectorFst;
/// # use rustfst::semirings::{BooleanWeight, Semiring};
/// # use rustfst::Arc;
/// let mut fst = VectorFst::<BooleanWeight>::new();
/// let s1 = fst.add_state();
/// let s2 = fst.add_state();
///
/// assert_eq!(fst.num_arcs(s1).unwrap(), 0);
/// fst.add_arc(s1, Arc::new(3, 5, BooleanWeight::new(true), s2));
/// assert_eq!(fst.num_arcs(s1).unwrap(), 1);
/// ```
fn num_arcs(&self, s: StateId) -> Fallible<usize>;
/// Returns whether or not the state with identifier passed as parameters is a final state.
///
/// # Example
///
/// ```
/// # use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
/// # use rustfst::fst_impls::VectorFst;
/// # use rustfst::semirings::{BooleanWeight, Semiring};
/// // 1 - Create an FST
/// let mut fst = VectorFst::<BooleanWeight>::new();
/// let s1 = fst.add_state();
/// let s2 = fst.add_state();
/// fst.set_final(s2, BooleanWeight::one());
///
/// // 2 - Test if a state is final
/// assert!(!fst.is_final(s1));
/// assert!(fst.is_final(s2));
/// ```
fn is_final(&self, state_id: StateId) -> bool {
self.final_weight(state_id).is_some()
}
/// Check whether a state is the start state or not.
fn is_start(&self, state_id: StateId) -> bool {
Some(state_id) == self.start()
}
}
/// Trait to iterate over the states of a wFST.
pub trait StateIterator<'a> {
/// Iterator used to iterate over the `state_id` of the states of an FST.
type Iter: Iterator<Item = StateId> + Clone;
/// Creates an iterator over the `state_id` of the states of an FST.
///
/// # 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();
///
/// let s1 = fst.add_state();
/// let s2 = fst.add_state();
///
/// for state_id in fst.states_iter() {
/// println!("State ID : {:?}", state_id);
/// }
///
/// let states : Vec<_> = fst.states_iter().collect();
/// assert_eq!(states, vec![s1, s2]);
/// ```
fn states_iter(&'a self) -> Self::Iter;
}
/// Trait to iterate over the outgoing arcs of a particular state in a wFST
pub trait ArcIterator<'a>: CoreFst
where
Self::W: 'a,
{
/// Iterator used to iterate over the arcs leaving a state of an FST.
type Iter: Iterator<Item = &'a Arc<Self::W>> + Clone;
fn arcs_iter(&'a self, state_id: StateId) -> Fallible<Self::Iter>;
}
/// Trait defining the minimum interface necessary for a wFST.
pub trait Fst:
CoreFst + PartialEq + Clone + for<'a> ArcIterator<'a> + for<'b> StateIterator<'b> + Display + Debug
{
/// Returns the number of arcs with epsilon input labels leaving a state.
///
/// # Example :
/// ```
/// # use rustfst::fst_traits::{MutableFst, Fst};
/// # use rustfst::fst_impls::VectorFst;
/// # use rustfst::semirings::{Semiring, IntegerWeight};
/// # use rustfst::EPS_LABEL;
/// # use rustfst::Arc;
/// let mut fst = VectorFst::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
///
/// fst.add_arc(s0, Arc::new(EPS_LABEL, 18, IntegerWeight::one(), s1));
/// fst.add_arc(s0, Arc::new(76, EPS_LABEL, IntegerWeight::one(), s1));
/// fst.add_arc(s0, Arc::new(EPS_LABEL, 18, IntegerWeight::one(), s1));
/// fst.add_arc(s0, Arc::new(45, 18, IntegerWeight::one(), s0));
/// fst.add_arc(s1, Arc::new(76, 18, IntegerWeight::one(), s1));
///
/// assert_eq!(fst.num_input_epsilons(s0).unwrap(), 2);
/// assert_eq!(fst.num_input_epsilons(s1).unwrap(), 0);
/// ```
fn num_input_epsilons(&self, state: StateId) -> Fallible<usize> {
let filter = InputEpsilonArcFilter {};
Ok(self.arcs_iter(state)?.filter(|v| filter.keep(v)).count())
}
/// Returns the number of arcs with epsilon output labels leaving a state.
///
/// # Example :
/// ```
/// # use rustfst::fst_traits::{MutableFst, Fst};
/// # use rustfst::fst_impls::VectorFst;
/// # use rustfst::semirings::{Semiring, IntegerWeight};
/// # use rustfst::EPS_LABEL;
/// # use rustfst::Arc;
/// let mut fst = VectorFst::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
///
/// fst.add_arc(s0, Arc::new(EPS_LABEL, 18, IntegerWeight::one(), s1));
/// fst.add_arc(s0, Arc::new(76, EPS_LABEL, IntegerWeight::one(), s1));
/// fst.add_arc(s0, Arc::new(EPS_LABEL, 18, IntegerWeight::one(), s1));
/// fst.add_arc(s0, Arc::new(45, 18, IntegerWeight::one(), s0));
/// fst.add_arc(s1, Arc::new(76, 18, IntegerWeight::one(), s1));
///
/// assert_eq!(fst.num_output_epsilons(s0).unwrap(), 1);
/// assert_eq!(fst.num_output_epsilons(s1).unwrap(), 0);
/// ```
fn num_output_epsilons(&self, state: StateId) -> Fallible<usize> {
let filter = OutputEpsilonArcFilter {};
Ok(self.arcs_iter(state)?.filter(|v| filter.keep(v)).count())
}
/// Returns true if the Fst is an acceptor. False otherwise.
/// Acceptor means for all arc, arc.ilabel == arc.olabel
fn is_acceptor(&self) -> bool {
let states: Vec<_> = self.states_iter().collect();
for state in states {
for arc in self.arcs_iter(state).unwrap() {
if arc.ilabel != arc.olabel {
return false;
}
}
}
return true;
}
}