lling-llang 0.1.0

WFST framework for text normalization and grammar correction
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Lattice construction builder.

use rustc_hash::FxHashMap;

use super::lattice::Lattice;
use super::types::{Edge, EdgeId, EdgeMetadata, Node, NodeId};
use crate::backend::LatticeBackend;
use crate::semiring::Semiring;

/// Builder for constructing lattices incrementally.
///
/// The builder handles:
/// - Creating nodes for positions automatically
/// - Interning vocabulary via the backend
/// - Tracking edge connectivity
///
/// # Example
///
/// ```rust
/// use lling_llang::lattice::{LatticeBuilder, EdgeMetadata};
/// use lling_llang::backend::HashMapBackend;
/// use lling_llang::semiring::TropicalWeight;
///
/// let backend = HashMapBackend::new();
/// let mut builder = LatticeBuilder::new(backend);
///
/// // Add corrections for position 0 -> 1
/// builder.add_correction(0, 1, "the", TropicalWeight::new(0.5), EdgeMetadata::correction(1));
/// builder.add_correction(0, 1, "teh", TropicalWeight::new(0.0), EdgeMetadata::original());
///
/// // Add corrections for position 1 -> 2
/// builder.add_correction(1, 2, "quick", TropicalWeight::new(0.5), EdgeMetadata::correction(1));
///
/// let lattice = builder.build(2);
/// ```
#[derive(Clone, Debug)]
pub struct LatticeBuilder<W: Semiring, B: LatticeBackend> {
    /// Nodes indexed by ID.
    nodes: Vec<Node>,
    /// Edges.
    edges: Vec<Edge<W>>,
    /// Backend for vocabulary interning.
    backend: B,
    /// Map from position to node ID.
    position_map: FxHashMap<usize, NodeId>,
}

impl<W: Semiring, B: LatticeBackend> LatticeBuilder<W, B> {
    /// Create a new builder with the given backend.
    pub fn new(backend: B) -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            backend,
            position_map: FxHashMap::default(),
        }
    }

    /// Create a new builder with pre-allocated capacity.
    pub fn with_capacity(backend: B, num_positions: usize, edges_per_position: usize) -> Self {
        let estimated_nodes = num_positions + 1;
        let estimated_edges = num_positions * edges_per_position;

        Self {
            nodes: Vec::with_capacity(estimated_nodes),
            edges: Vec::with_capacity(estimated_edges),
            backend,
            position_map: FxHashMap::with_capacity_and_hasher(estimated_nodes, Default::default()),
        }
    }

    /// Get or create a node for a position.
    fn get_or_create_node(&mut self, position: usize) -> NodeId {
        if let Some(&node_id) = self.position_map.get(&position) {
            return node_id;
        }

        let node_id = NodeId::new(self.nodes.len() as u32);
        self.nodes.push(Node::with_position(node_id, position));
        self.position_map.insert(position, node_id);
        node_id
    }

    /// Add a correction edge from one position to another.
    ///
    /// # Arguments
    ///
    /// * `start_pos` - Starting position in the input sequence
    /// * `end_pos` - Ending position in the input sequence
    /// * `word` - The correction word
    /// * `weight` - Edge weight
    /// * `metadata` - Edge metadata
    ///
    /// # Returns
    ///
    /// The ID of the created edge.
    pub fn add_correction(
        &mut self,
        start_pos: usize,
        end_pos: usize,
        word: &str,
        weight: W,
        metadata: EdgeMetadata,
    ) -> EdgeId {
        let source = self.get_or_create_node(start_pos);
        let target = self.get_or_create_node(end_pos);
        let label = self.backend.intern(word);
        let edge_id = EdgeId::new(self.edges.len() as u32);

        let edge = Edge::new(edge_id, source, target, label, weight, metadata);
        self.edges.push(edge);

        // Update node adjacency
        self.nodes[source.0 as usize].outgoing.push(edge_id);
        self.nodes[target.0 as usize].incoming.push(edge_id);

        edge_id
    }

    /// Add an edge by vocabulary ID (if word is already interned).
    pub fn add_correction_by_id(
        &mut self,
        start_pos: usize,
        end_pos: usize,
        label: crate::backend::VocabId,
        weight: W,
        metadata: EdgeMetadata,
    ) -> EdgeId {
        let source = self.get_or_create_node(start_pos);
        let target = self.get_or_create_node(end_pos);
        let edge_id = EdgeId::new(self.edges.len() as u32);

        let edge = Edge::new(edge_id, source, target, label, weight, metadata);
        self.edges.push(edge);

        // Update node adjacency
        self.nodes[source.0 as usize].outgoing.push(edge_id);
        self.nodes[target.0 as usize].incoming.push(edge_id);

        edge_id
    }

    /// Get access to the backend for vocabulary operations.
    #[inline]
    pub fn backend(&self) -> &B {
        &self.backend
    }

    /// Get mutable access to the backend.
    #[inline]
    pub fn backend_mut(&mut self) -> &mut B {
        &mut self.backend
    }

    /// Pre-intern multiple words for later use.
    pub fn intern_words<'a>(&mut self, words: impl IntoIterator<Item = &'a str>) {
        for word in words {
            self.backend.intern(word);
        }
    }

    /// Get the number of positions (nodes) currently in the builder.
    #[inline]
    pub fn num_positions(&self) -> usize {
        self.nodes.len()
    }

    /// Get the number of edges currently in the builder.
    #[inline]
    pub fn num_edges(&self) -> usize {
        self.edges.len()
    }

    /// Build the final lattice.
    ///
    /// # Arguments
    ///
    /// * `end_pos` - The final position (end node will be created if needed)
    ///
    /// # Returns
    ///
    /// The constructed lattice.
    pub fn build(mut self, end_pos: usize) -> Lattice<W, B> {
        // Ensure start and end nodes exist
        let start = self.get_or_create_node(0);
        let end = self.get_or_create_node(end_pos);

        // Sort nodes by position for proper topological order
        // (positions are already assigned, so we can use them directly)
        self.nodes.sort_by_key(|n| n.position.unwrap_or(usize::MAX));

        // Reassign node IDs after sorting
        let mut old_to_new: FxHashMap<NodeId, NodeId> = FxHashMap::default();
        for (new_id, node) in self.nodes.iter_mut().enumerate() {
            old_to_new.insert(node.id, NodeId::new(new_id as u32));
            node.id = NodeId::new(new_id as u32);
        }

        // Update edge source/target references
        for edge in &mut self.edges {
            edge.source = *old_to_new.get(&edge.source).expect("source exists");
            edge.target = *old_to_new.get(&edge.target).expect("target exists");
        }

        // Update start and end with new IDs
        let new_start = *old_to_new.get(&start).expect("start exists");
        let new_end = *old_to_new.get(&end).expect("end exists");

        Lattice::new(self.nodes, self.edges, new_start, new_end, self.backend)
    }

    /// Reserve capacity for additional edges.
    #[inline]
    pub fn reserve_edges(&mut self, additional: usize) {
        self.edges.reserve(additional);
    }

    /// Reserve capacity for additional positions.
    #[inline]
    pub fn reserve_positions(&mut self, additional: usize) {
        self.nodes.reserve(additional);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::HashMapBackend;
    use crate::semiring::TropicalWeight;

    #[test]
    fn test_builder_new() {
        let backend = HashMapBackend::new();
        let builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);
        assert_eq!(builder.num_positions(), 0);
        assert_eq!(builder.num_edges(), 0);
    }

    #[test]
    fn test_add_correction() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        let edge_id = builder.add_correction(
            0,
            1,
            "hello",
            TropicalWeight::new(1.0),
            EdgeMetadata::default(),
        );

        assert_eq!(edge_id, EdgeId::new(0));
        assert_eq!(builder.num_positions(), 2); // 0 and 1
        assert_eq!(builder.num_edges(), 1);
    }

    #[test]
    fn test_multiple_corrections_same_position() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        builder.add_correction(
            0,
            1,
            "the",
            TropicalWeight::new(0.5),
            EdgeMetadata::default(),
        );
        builder.add_correction(
            0,
            1,
            "teh",
            TropicalWeight::new(0.0),
            EdgeMetadata::default(),
        );
        builder.add_correction(
            0,
            1,
            "tea",
            TropicalWeight::new(1.0),
            EdgeMetadata::default(),
        );

        assert_eq!(builder.num_positions(), 2);
        assert_eq!(builder.num_edges(), 3);
    }

    #[test]
    fn test_build_simple() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        builder.add_correction(
            0,
            1,
            "hello",
            TropicalWeight::new(1.0),
            EdgeMetadata::default(),
        );
        builder.add_correction(
            1,
            2,
            "world",
            TropicalWeight::new(1.0),
            EdgeMetadata::default(),
        );

        let lattice = builder.build(2);

        assert_eq!(lattice.num_nodes(), 3);
        assert_eq!(lattice.num_edges(), 2);
        assert_eq!(lattice.start(), NodeId::new(0));
        assert_eq!(lattice.end(), NodeId::new(2));
    }

    #[test]
    fn test_vocabulary_interning() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        builder.add_correction(
            0,
            1,
            "hello",
            TropicalWeight::new(1.0),
            EdgeMetadata::default(),
        );
        builder.add_correction(
            1,
            2,
            "hello",
            TropicalWeight::new(1.0),
            EdgeMetadata::default(),
        );

        let lattice = builder.build(2);

        // Both edges should have the same label (interned)
        let labels: Vec<_> = lattice.edges().iter().map(|e| e.label).collect();
        assert_eq!(labels[0], labels[1]);

        // Backend should only have one entry
        assert_eq!(lattice.backend().vocab_size(), 1);
    }

    #[test]
    fn test_intern_words() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        builder.intern_words(["hello", "world", "test"]);

        assert_eq!(builder.backend().vocab_size(), 3);
    }

    #[test]
    fn test_add_correction_by_id() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        let id = builder.backend_mut().intern("hello");
        builder.add_correction_by_id(0, 1, id, TropicalWeight::new(1.0), EdgeMetadata::default());

        assert_eq!(builder.num_edges(), 1);
    }

    #[test]
    fn test_with_capacity() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> =
            LatticeBuilder::with_capacity(backend, 10, 5);

        // Should not panic with reserved capacity
        for i in 0..10 {
            for _ in 0..5 {
                builder.add_correction(
                    i,
                    i + 1,
                    "word",
                    TropicalWeight::new(1.0),
                    EdgeMetadata::default(),
                );
            }
        }

        assert_eq!(builder.num_positions(), 11);
        assert_eq!(builder.num_edges(), 50);
    }

    #[test]
    fn test_empty_build() {
        let backend = HashMapBackend::new();
        let builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);
        let lattice = builder.build(0);

        assert_eq!(lattice.num_nodes(), 1);
        assert_eq!(lattice.num_edges(), 0);
        assert_eq!(lattice.start(), lattice.end());
    }

    #[test]
    fn test_node_adjacency() {
        let backend = HashMapBackend::new();
        let mut builder: LatticeBuilder<TropicalWeight, _> = LatticeBuilder::new(backend);

        builder.add_correction(0, 1, "a", TropicalWeight::new(1.0), EdgeMetadata::default());
        builder.add_correction(0, 1, "b", TropicalWeight::new(1.0), EdgeMetadata::default());
        builder.add_correction(1, 2, "c", TropicalWeight::new(1.0), EdgeMetadata::default());

        let lattice = builder.build(2);

        // Start node has 2 outgoing, 0 incoming
        let start = lattice
            .node(NodeId::new(0))
            .expect("lattice/builder.rs: required value was None/Err");
        assert_eq!(start.out_degree(), 2);
        assert_eq!(start.in_degree(), 0);

        // Middle node has 2 incoming (from start), 1 outgoing
        let middle = lattice
            .node(NodeId::new(1))
            .expect("lattice/builder.rs: required value was None/Err");
        assert_eq!(middle.in_degree(), 2);
        assert_eq!(middle.out_degree(), 1);

        // End node has 1 incoming, 0 outgoing
        let end = lattice
            .node(NodeId::new(2))
            .expect("lattice/builder.rs: required value was None/Err");
        assert_eq!(end.in_degree(), 1);
        assert_eq!(end.out_degree(), 0);
    }
}