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
# Path Sampling

Path sampling provides algorithms for randomly drawing accepting paths from WFSTs, enabling Monte Carlo methods, online learning, and diverse output generation.

## Concepts

### Why Sample Paths?

While shortest-path algorithms find the single best path, sampling provides:

| Application | Benefit |
|-------------|---------|
| **Monte Carlo estimation** | Approximate expectations when exact computation is intractable |
| **Online learning** | RRWM and FPTL algorithms require sampling predictions |
| **Diverse outputs** | Generate multiple plausible alternatives, not just the best |
| **Uncertainty estimation** | Sample distribution reflects model confidence |
| **Data augmentation** | Generate training data from WFST models |

### Forward Sampling Algorithm

The sampling algorithm performs a random walk from the start state to a final state:

```
ForwardSample(WFST):
    current ← start state
    path ← empty list

    while not at final state:
        if current is final and should_stop():
            return path

        transitions ← outgoing transitions from current
        trans ← sample_transition(transitions)
        path.append(trans)
        current ← trans.destination

    return path
```

The key decision is how to choose transitions. This is controlled by the **sampling strategy**.

### Sampling Strategies

Two strategies are supported:

**Proportional Sampling**

```
P(transition t) = weight(t) / Σ weight(all transitions)
```

Transitions are chosen proportional to their weights. For a stochastic (weight-pushed) WFST, this gives proper probability sampling.

```
State 0:                         Proportional sampling:
  ┌─ a/0.3 → State 1            P(a) = 0.3/(0.3+0.5+0.2) = 0.3
  ├─ b/0.5 → State 2            P(b) = 0.5/(0.3+0.5+0.2) = 0.5
  └─ c/0.2 → State 3            P(c) = 0.2/(0.3+0.5+0.2) = 0.2
```

**Uniform Sampling**

```
P(transition t) = 1 / (number of transitions)
```

All transitions have equal probability, regardless of weights. Useful for exploration.

```
State 0:                         Uniform sampling:
  ┌─ a/0.3 → State 1            P(a) = 1/3 ≈ 0.33
  ├─ b/0.5 → State 2            P(b) = 1/3 ≈ 0.33
  └─ c/0.2 → State 3            P(c) = 1/3 ≈ 0.33
```

### Stochastic vs Non-Stochastic WFSTs

For best results with proportional sampling, use **weight-pushed** WFSTs where outgoing weights sum to 1:

```
Before pushing:                  After pushing:
State 0:                        State 0:
  ├─ a/2.0 → s1                   ├─ a/0.4 → s1 (2/5)
  └─ b/3.0 → s2                   └─ b/0.6 → s2 (3/5)

Not stochastic                   Stochastic (sums to 1.0)
```

Non-stochastic WFSTs are normalized on-the-fly during sampling.

## Core API

### Configuration

```rust
use lling_llang::algorithms::{SampleConfig, SampleStrategy};

let config = SampleConfig::new()
    .max_length(1000)           // Maximum path length (default: 10,000)
    .strategy(SampleStrategy::Proportional)  // Proportional (default) or Uniform
    .include_epsilon(false)     // Include epsilon labels in output?
    .seed(42);                  // Fixed seed for reproducibility
```

### Sampling Functions

| Function | Purpose |
|----------|---------|
| `sample_path()` | Sample a single accepting path |
| `sample_paths()` | Sample multiple paths (fixed count) |
| `sample_paths_until()` | Sample until target successes or max attempts |
| `estimate_expected_weight()` | Monte Carlo estimation of expected weight |

### Error Handling

```rust
use lling_llang::algorithms::SampleError;

match sample_path(&wfst, config) {
    Ok(path) => println!("Sampled: {:?}", path.output_string()),
    Err(SampleError::EmptyWfst) => println!("WFST has no states"),
    Err(SampleError::MaxLengthExceeded) => println!("Path too long"),
    Err(SampleError::NoAcceptingPaths) => println!("No final states reachable"),
    Err(SampleError::DeadState(s)) => println!("Hit dead state {}", s),
    Err(SampleError::ZeroWeights(s)) => println!("All weights zero at state {}", s),
}
```

### SampledPath Structure

```rust
pub struct SampledPath<L, W> {
    pub input_labels: Vec<Option<L>>,   // Input labels (None = epsilon)
    pub output_labels: Vec<Option<L>>,  // Output labels (None = epsilon)
    pub weight: W,                       // Accumulated path weight
    pub states: Vec<StateId>,            // States visited
    pub length: usize,                   // Number of transitions
}

// Convenience methods
let path = sample_path(&wfst, config)?;
let inputs: Vec<&L> = path.input_string();   // Non-epsilon inputs
let outputs: Vec<&L> = path.output_string(); // Non-epsilon outputs
```

## Examples

### Basic Path Sampling

```rust
use lling_llang::algorithms::{sample_path, SampleConfig};
use lling_llang::semiring::TropicalWeight;
use lling_llang::wfst::{VectorWfst, MutableWfst};

// Create a simple WFST
let mut wfst = VectorWfst::<char, TropicalWeight>::new();
let s0 = wfst.add_state();
let s1 = wfst.add_state();
let s2 = wfst.add_state();

wfst.set_start(s0);
wfst.set_final(s2, TropicalWeight::new(0.0));

wfst.add_arc(s0, Some('a'), Some('x'), s1, TropicalWeight::new(1.0));
wfst.add_arc(s1, Some('b'), Some('y'), s2, TropicalWeight::new(1.0));

// Sample a path
let config = SampleConfig::default().seed(42);
let path = sample_path(&wfst, config).expect("Should find path");

println!("Input: {:?}", path.input_string());   // ['a', 'b']
println!("Output: {:?}", path.output_string()); // ['x', 'y']
println!("Weight: {:?}", path.weight);
println!("Length: {}", path.length);            // 2
```

### Sampling with Different Strategies

```rust
use lling_llang::algorithms::{sample_paths_until, SampleConfig, SampleStrategy};

// WFST with two paths of different weights
let mut wfst = VectorWfst::<char, TropicalWeight>::new();
let s0 = wfst.add_state();
let s1 = wfst.add_state();
let s2 = wfst.add_state();

wfst.set_start(s0);
wfst.set_final(s1, TropicalWeight::new(0.0));
wfst.set_final(s2, TropicalWeight::new(0.0));

// High-weight path (shorter in tropical = better)
wfst.add_arc(s0, Some('a'), Some('x'), s1, TropicalWeight::new(1.0));
// Low-weight path
wfst.add_arc(s0, Some('b'), Some('y'), s2, TropicalWeight::new(5.0));

// Proportional sampling: prefers lower tropical weight
let prop_config = SampleConfig::default()
    .strategy(SampleStrategy::Proportional)
    .seed(42);

let prop_paths = sample_paths_until(&wfst, 100, 1000, prop_config);
let a_count = prop_paths.iter()
    .filter(|p| p.input_string() == vec![&'a'])
    .count();
println!("Proportional: 'a' chosen {} times out of 100", a_count);

// Uniform sampling: equal probability
let unif_config = SampleConfig::default()
    .strategy(SampleStrategy::Uniform)
    .seed(42);

let unif_paths = sample_paths_until(&wfst, 100, 1000, unif_config);
let a_count = unif_paths.iter()
    .filter(|p| p.input_string() == vec![&'a'])
    .count();
println!("Uniform: 'a' chosen {} times out of 100", a_count);  // ~50
```

### Batch Sampling

```rust
use lling_llang::algorithms::{sample_paths, SampleConfig};

let config = SampleConfig::default().seed(42);

// Sample exactly 10 paths (may include errors)
let results = sample_paths(&wfst, 10, config);

for (i, result) in results.iter().enumerate() {
    match result {
        Ok(path) => println!("Path {}: {:?}", i, path.output_string()),
        Err(e) => println!("Path {} failed: {}", i, e),
    }
}

// Count successes
let successes = results.iter().filter(|r| r.is_ok()).count();
println!("{} successful samples out of 10", successes);
```

### Sampling Until Target Count

```rust
use lling_llang::algorithms::{sample_paths_until, SampleConfig};

// Sample until we have 50 successful paths (or 500 attempts)
let config = SampleConfig::default().seed(42);
let paths = sample_paths_until(&wfst, 50, 500, config);

println!("Got {} successful paths", paths.len());

// All paths in the result are successful
for path in &paths {
    println!("Output: {:?}", path.output_string());
}
```

### Monte Carlo Weight Estimation

```rust
use lling_llang::algorithms::{estimate_expected_weight, SampleConfig};

// Estimate the expected weight of accepting paths
let config = SampleConfig::default().seed(42);
let expected = estimate_expected_weight(&wfst, 1000, config);

match expected {
    Some(e) => println!("Estimated expected weight: {:.4}", e),
    None => println!("Could not estimate (empty WFST or all samples failed)"),
}
```

### Reproducible Sampling

```rust
use lling_llang::algorithms::{sample_path, SampleConfig};

// Same seed = same results
let config1 = SampleConfig::default().seed(12345);
let config2 = SampleConfig::default().seed(12345);

let path1 = sample_path(&wfst, config1)?;
let path2 = sample_path(&wfst, config2)?;

assert_eq!(path1.input_string(), path2.input_string());
assert_eq!(path1.output_string(), path2.output_string());
println!("Reproducible sampling verified!");
```

### Sampling from Stochastic WFST

```rust
use lling_llang::algorithms::{push_weights, sample_path, PushConfig, SampleConfig};
use lling_llang::semiring::PowerWeight;
use lling_llang::wfst::{VectorWfst, MutableWfst};

let eta = 1.0;
let mut wfst = VectorWfst::<char, PowerWeight>::new();

// Build WFST with arbitrary weights
let s0 = wfst.add_state();
let s1 = wfst.add_state();
let s2 = wfst.add_state();

wfst.set_start(s0);
wfst.set_final(s1, PowerWeight::one_with_eta(eta));
wfst.set_final(s2, PowerWeight::one_with_eta(eta));

wfst.add_arc(s0, Some('a'), Some('x'), s1,
    PowerWeight::from_probability(3.0, eta));
wfst.add_arc(s0, Some('b'), Some('y'), s2,
    PowerWeight::from_probability(7.0, eta));

// Push weights to make stochastic (outgoing sums to 1)
push_weights(&mut wfst, PushConfig::backward())
    .expect("Push should succeed");

// Now sampling gives proper probability distribution
let config = SampleConfig::default().seed(42);
let path = sample_path(&wfst, config)?;
println!("Sampled from stochastic WFST: {:?}", path.output_string());
```

### Handling Dead States

```rust
use lling_llang::algorithms::{sample_path, SampleConfig, SampleError};
use lling_llang::semiring::TropicalWeight;
use lling_llang::wfst::{VectorWfst, MutableWfst};

// WFST with a dead state (non-final with no outgoing transitions)
let mut wfst = VectorWfst::<char, TropicalWeight>::new();
let s0 = wfst.add_state();
let s1 = wfst.add_state(); // Dead state!

wfst.set_start(s0);
// s1 is not final and has no outgoing transitions
wfst.add_arc(s0, Some('a'), Some('a'), s1, TropicalWeight::new(1.0));

let config = SampleConfig::default().seed(42);
let result = sample_path(&wfst, config);

match result {
    Err(SampleError::DeadState(state)) => {
        println!("Encountered dead state: {}", state);
    }
    _ => unreachable!(),
}
```

## When to Use Path Sampling

| Scenario | Recommended Approach |
|----------|---------------------|
| Find the single best path | Use `viterbi()` instead of sampling |
| Generate diverse alternatives | `sample_paths_until()` with uniform strategy |
| Monte Carlo approximation | `estimate_expected_weight()` |
| Online learning (RRWM) | `sample_path()` with proportional strategy |
| Reproducible experiments | Set `seed` in config |
| Unknown WFST structure | Use `sample_paths()` and check for errors |

## Performance Considerations

| Factor | Impact | Recommendation |
|--------|--------|----------------|
| Path length | Linear in length | Set reasonable `max_length` |
| Number of transitions | Constant per state | Proportional sampling has O(n) per state |
| Stochastic vs raw | Same complexity | Push weights once, sample many times |
| Seed setting | Minor overhead | Use for reproducibility |

## Algorithm Details

### Stop Decision at Final States

When the current state is final but has outgoing transitions, the algorithm must decide whether to stop or continue:

**Proportional strategy:**
```
P(stop) = final_weight / (final_weight + Σ transition_weights)
```

**Uniform strategy:**
```
P(stop) = 1 / (1 + number_of_transitions)
```

### Weight Accumulation

Path weight is accumulated using semiring multiplication:

```
path_weight = w₁ ⊗ w₂ ⊗ ... ⊗ wₙ ⊗ final_weight
```

For tropical semiring, this is addition. For probability semiring, multiplication.

## Related Documentation

- [Weight Pushing]weight-pushing.md - Make WFSTs stochastic before sampling
- [Shortest Distance]shortest-distance.md - Alternative to sampling for exact computation
- [Power Semiring]../architecture/power-semiring.md - Soft weights for sampling
- [RRWM Algorithm]rrwm.md - Online learning that uses path sampling