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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! # Macros
//!
//! Convenience macros for FST construction.
//!
//! ## Overview
//!
//! This module provides declarative macros that simplify the construction of
//! FSTs by allowing inline specification of states, arcs, and properties.
//! These macros reduce boilerplate compared to imperative construction while
//! maintaining type safety.
//!
//! ## Available Macros
//!
//! | Macro | Purpose |
//! |-------|---------|
//! | `fst!` | Create a complete [`VectorFst`] with states and arcs |
//! | `symt!` | Create a [`SymbolTable`] from symbol strings |
//! | `fst_path!` | Create an [`FstPath`] for path representation |
//!
//! ## Examples
//!
//! ### Building an FST
//!
//! ```
//! use arcweight::fst;
//! use arcweight::prelude::*;
//!
//! // Create a simple two-state acceptor
//! let acceptor = fst!(
//! start: 0,
//! finals: [1],
//! arcs: [
//! (0, 1, 1, 1, 0.5),
//! ]
//! );
//!
//! assert_eq!(acceptor.num_states(), 2);
//! ```
//!
//! ### Creating Symbol Tables
//!
//! ```
//! use arcweight::symt;
//!
//! let symbols = symt!["a", "b", "c"];
//! assert_eq!(symbols.find_id("a"), Some(1));
//! ```
//!
//! [`VectorFst`]: crate::fst::VectorFst
//! [`SymbolTable`]: crate::utils::SymbolTable
//! [`FstPath`]: crate::utils::FstPath
/// Creates a [`VectorFst<TropicalWeight>`] from a declarative specification.
///
/// This macro provides a concise syntax for constructing FSTs by specifying
/// the start state, final states, and arcs in a structured format.
///
/// # Syntax
///
/// ```text
/// fst!(
/// start: <state_id>,
/// finals: [<state_id>, ...],
/// arcs: [
/// (<from>, <to>, <ilabel>, <olabel>, <weight>),
/// ...
/// ]
/// )
/// ```
///
/// # Arguments
///
/// * `start` - The initial state ID (states are created automatically)
/// * `finals` - A list of final state IDs (all receive weight `TropicalWeight::one()`)
/// * `arcs` - A list of arc tuples: `(from_state, to_state, input_label, output_label, weight)`
///
/// # Returns
///
/// A [`VectorFst<TropicalWeight>`] containing all specified states and arcs.
///
/// # Examples
///
/// Basic acceptor:
///
/// ```
/// use arcweight::fst;
/// use arcweight::prelude::*;
///
/// let fst = fst!(
/// start: 0,
/// finals: [1],
/// arcs: [
/// (0, 1, 1, 1, 1.0),
/// ]
/// );
///
/// assert_eq!(fst.num_states(), 2);
/// assert_eq!(fst.start(), Some(0));
/// assert!(fst.is_final(1));
/// ```
///
/// Multi-path transducer:
///
/// ```
/// use arcweight::fst;
/// use arcweight::prelude::*;
///
/// let fst = fst!(
/// start: 0,
/// finals: [2, 3],
/// arcs: [
/// (0, 1, 1, 10, 0.5), // Transform 1 -> 10
/// (1, 2, 2, 20, 0.3), // Transform 2 -> 20
/// (1, 3, 3, 30, 0.7), // Transform 3 -> 30
/// ]
/// );
///
/// assert_eq!(fst.num_states(), 4);
/// ```
///
/// [`VectorFst<TropicalWeight>`]: crate::fst::VectorFst
// Set start
fst.set_start;
// Set finals
$*
// Add arcs
$*
fst
}};
}
/// Creates a [`SymbolTable`] from a list of symbol strings.
///
/// Symbol IDs are assigned sequentially starting from 1, with ID 0 reserved
/// for epsilon (the empty symbol). This is consistent with OpenFST conventions.
///
/// # Arguments
///
/// * `symbols` - A comma-separated list of string literals or expressions
///
/// # Returns
///
/// A [`SymbolTable`] with symbols assigned sequential IDs starting from 1.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use arcweight::symt;
///
/// let symbols = symt!["hello", "world", "test"];
///
/// assert_eq!(symbols.find_id("hello"), Some(1));
/// assert_eq!(symbols.find_id("world"), Some(2));
/// assert_eq!(symbols.find_id("test"), Some(3));
/// assert_eq!(symbols.find_key(1), Some("hello"));
/// ```
///
/// With trailing comma:
///
/// ```
/// use arcweight::symt;
///
/// let symbols = symt![
/// "cat",
/// "dog",
/// "bird",
/// ];
///
/// // Note: len() includes the epsilon symbol at index 0
/// assert_eq!(symbols.len(), 4);
/// ```
///
/// [`SymbolTable`]: crate::utils::SymbolTable
};
}
/// Creates an [`FstPath`] representing a path through an FST.
///
/// This macro constructs a path object containing the sequence of states,
/// arcs traversed, and total path weight. Useful for testing and representing
/// specific paths through an FST.
///
/// # Syntax
///
/// ```text
/// fst_path!(
/// states: [<state_id>, ...],
/// arcs: [
/// (<ilabel>, <olabel>, <weight>),
/// ...
/// ],
/// weight: <total_weight>
/// )
/// ```
///
/// # Arguments
///
/// * `states` - Ordered list of state IDs in the path
/// * `arcs` - Arc labels and weights for each transition (one fewer than states)
/// * `weight` - Total accumulated weight of the path
///
/// # Returns
///
/// An [`FstPath<TropicalWeight>`] containing the specified path information.
///
/// # Examples
///
/// Simple path:
///
/// ```
/// use arcweight::fst_path;
/// use arcweight::prelude::*;
///
/// let path = fst_path!(
/// states: [0, 1, 2],
/// arcs: [
/// (1, 1, 1.0),
/// (2, 2, 0.5),
/// ],
/// weight: 1.5
/// );
///
/// assert_eq!(path.states.len(), 3);
/// assert_eq!(path.arcs.len(), 2);
/// assert_eq!(path.final_state, 2);
/// ```
///
/// Single-arc path:
///
/// ```
/// use arcweight::fst_path;
/// use arcweight::prelude::*;
///
/// let path = fst_path!(
/// states: [0, 1],
/// arcs: [
/// (10, 20, 0.25),
/// ],
/// weight: 0.25
/// );
///
/// assert_eq!(path.arcs[0].ilabel, 10);
/// assert_eq!(path.arcs[0].olabel, 20);
/// ```
///
/// [`FstPath`]: crate::utils::FstPath
/// [`FstPath<TropicalWeight>`]: crate::utils::FstPath
}
}};
}