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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! Kleene closure algorithms.
//!
//! Implements Kleene star ($`T^*`$) and Kleene plus ($`T^+`$) operations for weighted
//! finite-state transducers, enabling repetition patterns and iterative language constructions.
//!
//! - **Kleene Star ($`T^*`$):** Accepts zero or more repetitions:
//! $`L(T^*) = \{\varepsilon\} \cup L(T) \cup L(T^2) \cup \ldots`$
//! - **Kleene Plus ($`T^+`$):** Accepts one or more repetitions:
//! $`L(T^+) = L(T) \cup L(T^2) \cup L(T^3) \cup \ldots`$
//!
//! # Complexity
//!
//! - **Time:** $`O(|V| + |E|)`$ -- single copy plus constant overhead for epsilon connections
//! - **Space:** $`O(|V| + |E|)`$ plus one additional state
//!
//! # Semiring Requirements
//!
//! Kleene closure requires a [`StarSemiring`] for proper weight computation in infinite
//! repetition scenarios. The star operation computes $`\bigoplus_{n=0}^{\infty} w^n`$ for
//! weights on cyclic paths.
//!
//! # References
//!
//! - Stephen Cole Kleene. 1956. Representation of events in nerve nets and finite automata.
//! In *Automata Studies*, Claude E. Shannon and John McCarthy (Eds.). Princeton University
//! Press, Princeton, NJ, 3-42.
//! - Mehryar Mohri. 2009. Weighted automata algorithms. In *Handbook of Weighted
//! Automata*, Manfred Droste, Werner Kuich, and Heiko Vogler (Eds.). Springer,
//! Berlin, Heidelberg, 213-254.
//!
//! [`StarSemiring`]: crate::semiring::StarSemiring
use crateArc;
use crate;
use crateStarSemiring;
use crateResult;
/// Computes the Kleene closure (star) of an FST.
///
/// Creates a new FST that accepts zero or more repetitions of the input FST:
/// $`L(T^*) = \{\varepsilon\} \cup L(T) \cup L(T^2) \cup L(T^3) \cup \ldots`$
///
/// The result always accepts the empty string, plus any concatenation of strings
/// from the original language.
///
/// # Type Parameters
///
/// * `W` - Weight type implementing [`StarSemiring`]
/// * `F` - Input FST type implementing [`Fst<W>`]
/// * `M` - Output FST type implementing [`MutableFst<W>`] and [`Default`]
///
/// # Returns
///
/// A new FST accepting zero or more repetitions of the input language.
///
/// # Errors
///
/// Returns [`Error::Algorithm`](crate::Error::Algorithm) if the input FST is invalid.
///
/// # Examples
///
/// ```rust
/// use arcweight::prelude::*;
///
/// // FST accepts "a"
/// let mut fst = VectorFst::<BooleanWeight>::new();
/// let s0 = fst.add_state();
/// let s1 = fst.add_state();
/// fst.set_start(s0);
/// fst.set_final(s1, BooleanWeight::one());
/// fst.add_arc(s0, Arc::new('a' as u32, 'a' as u32, BooleanWeight::one(), s1));
///
/// // a* accepts: ε, "a", "aa", "aaa", ...
/// let star_fst: VectorFst<BooleanWeight> = closure(&fst)?;
///
/// // New start state is also final (accepts empty string)
/// let start = star_fst.start().unwrap();
/// assert!(star_fst.is_final(start));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # References
///
/// - Stephen Cole Kleene. 1956. Representation of events in nerve nets and finite automata.
/// In *Automata Studies*, Claude E. Shannon and John McCarthy (Eds.). Princeton University
/// Press, Princeton, NJ, 3-42.
///
/// [`StarSemiring`]: crate::semiring::StarSemiring
/// Computes the Kleene plus of an FST.
///
/// Creates a new FST that accepts one or more repetitions of the input FST:
/// $`L(T^+) = L(T) \cup L(T^2) \cup L(T^3) \cup \ldots`$
///
/// Unlike Kleene star, Kleene plus does **not** accept the empty string -- it requires
/// at least one iteration of the original FST language.
///
/// # Type Parameters
///
/// * `W` - Weight type implementing [`StarSemiring`]
/// * `F` - Input FST type implementing [`Fst<W>`]
/// * `M` - Output FST type implementing [`MutableFst<W>`] and [`Default`]
///
/// # Returns
///
/// A new FST accepting one or more repetitions of the input language.
///
/// # Errors
///
/// Returns [`Error::Algorithm`](crate::Error::Algorithm) if the input FST is invalid.
///
/// # Examples
///
/// ```rust
/// use arcweight::prelude::*;
///
/// // FST accepts single digit
/// let mut digit = VectorFst::<BooleanWeight>::new();
/// let s0 = digit.add_state();
/// let s1 = digit.add_state();
/// digit.set_start(s0);
/// digit.set_final(s1, BooleanWeight::one());
/// digit.add_arc(s0, Arc::new('0' as u32, '0' as u32, BooleanWeight::one(), s1));
///
/// // digit+ accepts: "0", "00", "000", ... but NOT empty string
/// let number: VectorFst<BooleanWeight> = closure_plus(&digit)?;
///
/// // New start state is NOT final (rejects empty string)
/// let start = number.start().unwrap();
/// assert!(!number.is_final(start));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # References
///
/// - Stephen Cole Kleene. 1956. Representation of events in nerve nets and finite automata.
/// In *Automata Studies*, Claude E. Shannon and John McCarthy (Eds.). Princeton University
/// Press, Princeton, NJ, 3-42.
///
/// [`StarSemiring`]: crate::semiring::StarSemiring