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
use std::fmt::Debug;
use std::rc::Rc;
use failure::Fallible;
use crate::algorithms::arc_filters::{ArcFilter, InputEpsilonArcFilter, OutputEpsilonArcFilter};
use crate::fst_traits::iterators::{ArcIterator, StateIterator};
use crate::semirings::Semiring;
use crate::{StateId, SymbolTable};
/// 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).unwrap(), None);
/// assert_eq!(fst.final_weight(s2).unwrap(), Some(&BooleanWeight::one()));
/// assert!(fst.final_weight(s2 + 1).is_err());
/// ```
fn final_weight(&self, state_id: StateId) -> Fallible<Option<&<Self as CoreFst>::W>>;
unsafe fn final_weight_unchecked(&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>;
unsafe fn num_arcs_unchecked(&self, s: StateId) -> 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_eq!(fst.is_final(s1).unwrap(), false);
/// assert_eq!(fst.is_final(s2).unwrap(), true);
/// assert!(fst.is_final(s2 + 1).is_err());
/// ```
#[inline]
fn is_final(&self, state_id: StateId) -> Fallible<bool> {
let w = self.final_weight(state_id)?;
Ok(w.is_some())
}
#[inline]
unsafe fn is_final_unchecked(&self, state_id: StateId) -> bool {
self.final_weight_unchecked(state_id).is_some()
}
/// Check whether a state is the start state or not.
#[inline]
fn is_start(&self, state_id: StateId) -> bool {
Some(state_id) == self.start()
}
}
/// Trait defining the minimum interface necessary for a wFST.
pub trait Fst: CoreFst + for<'a> ArcIterator<'a> + for<'b> StateIterator<'b> + Debug {
// TODO: Move niepsilons and noepsilons to required methods.
/// 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::<IntegerWeight>::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::<IntegerWeight>::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;
}
}
}
true
}
/// Retrieves the input `SymbolTable` associated to the Fst.
/// If no SymbolTable has been previously attached then `None` is returned.
fn input_symbols(&self) -> Option<Rc<SymbolTable>>;
/// Retrieves the output `SymbolTable` associated to the Fst.
/// If no SymbolTable has been previously attached then `None` is returned.
fn output_symbols(&self) -> Option<Rc<SymbolTable>>;
}