duallity 0.2.0

Levenshtein automata as lling-llang WFSTs: composition adapters bridging liblevenshtein fuzzy matching and the lling-llang weighted-transducer framework.
Documentation
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# WFST Text Normalization: Limitations and Trade-offs

**Status**: Design Document
**Last Updated**: 2025-11-20
**Purpose**: Understanding capabilities and boundaries of each layer

---

## Table of Contents

1. [Introduction]#introduction
2. [Chomsky Hierarchy Positioning]#chomsky-hierarchy-positioning
3. [FST/NFA Capabilities (Type 3)]#fstnfa-capabilities-type-3
4. [CFG Capabilities (Type 2)]#cfg-capabilities-type-2
5. [Neural Capabilities (Beyond Type 2)]#neural-capabilities-beyond-type-2
6. [Parsing Complexity Trade-offs]#parsing-complexity-trade-offs
7. [When to Use Each Layer]#when-to-use-each-layer
8. [Common Pitfalls]#common-pitfalls
9. [Design Recommendations]#design-recommendations

---

## Introduction

### Why Limitations Matter

**Problem**: Choosing the wrong formalism leads to:
- **Under-powered**: Cannot handle the error type (FST for grammar)
- **Over-powered**: Unnecessary complexity and latency (neural for spelling)
- **Combinatorial explosion**: Wrong algorithm for problem size

**Solution**: Understand the **formal language hierarchy** and select the minimal sufficient formalism.

### Three-Tier Philosophy

```
┌─────────────────────────────────────────────────────────┐
│ Tier 3: Neural (Unrestricted)                           │
│ - Semantic disambiguation                               │
│ - Discourse coherence                                   │
│ - Cost: High latency, non-deterministic                 │
└─────────────────────────────────────────────────────────┘
                          ↑ (only if needed)
┌─────────────────────────────────────────────────────────┐
│ Tier 2: CFG (Context-Free)                              │
│ - Syntax, phrase structure                              │
│ - Cost: O(n³) parsing                                   │
└─────────────────────────────────────────────────────────┘
                          ↑ (only if needed)
┌─────────────────────────────────────────────────────────┐
│ Tier 1: FST/NFA (Regular)                               │
│ - Spelling, phonetic, morphology                        │
│ - Cost: O(n) recognition                                │
└─────────────────────────────────────────────────────────┘
                          ↑ (always start here)
```

**Principle**: Use the **simplest formalism** that can solve the problem.

---

## Chomsky Hierarchy Positioning

### The Hierarchy

**Type 3: Regular Languages** (FST/NFA)
- **Grammar**: A → aB | a (right-linear)
- **Automaton**: Finite-state automaton
- **Memory**: Finite states (no stack)
- **Complexity**: O(n) recognition
- **Example**: a*b*

**Type 2: Context-Free Languages** (CFG)
- **Grammar**: A → α (any string on RHS)
- **Automaton**: Pushdown automaton (FSA + stack)
- **Memory**: Unbounded stack
- **Complexity**: O(n³) CYK, O(n²) average Earley
- **Example**: a^n b^n (balanced parentheses)

**Type 1: Context-Sensitive Languages** (CSG)
- **Grammar**: αAβ → αγβ (context around A)
- **Automaton**: Linear-bounded automaton
- **Memory**: Tape bounded by input length
- **Complexity**: PSPACE-complete
- **Example**: a^n b^n c^n

**Type 0: Recursively Enumerable** (Turing Machine)
- **Grammar**: Unrestricted rewriting
- **Automaton**: Turing machine
- **Memory**: Unbounded tape
- **Complexity**: Undecidable
- **Example**: Halting problem

### Inclusion Relationships

```
Type 3 ⊂ Type 2 ⊂ Type 1 ⊂ Type 0

Regular ⊂ Context-Free ⊂ Context-Sensitive ⊂ Recursively Enumerable
```

**Strict Inclusions** (proven via pumping lemmas):
- {a^n b^n} ∈ Type 2, {a^n b^n} ∉ Type 3
- {a^n b^n c^n} ∈ Type 1, {a^n b^n c^n} ∉ Type 2

### Text Normalization Mapping

| Error Type | Language Class | Formalism | Why |
|------------|----------------|-----------|-----|
| Spelling (typos) | Type 3 | FST | Character edits (no counting) |
| Phonetic ("fone") | Type 3 | NFA | Pattern matching (no nesting) |
| Morphology (plurals) | Type 3 | FST | Suffix rules (finite patterns) |
| Article (a/an) | Type 2 | CFG | Phonological context (requires parsing) |
| Subject-verb agreement | Type 2 | CFG | Counting (NP number ↔ VP number) |
| Nested phrases | Type 2 | CFG | Balanced structures (recursion) |
| Anaphora resolution | Beyond Type 2 | Neural | Cross-sentence dependencies |
| Semantic ("bank") | Beyond Type 2 | Neural | World knowledge |

---

## FST/NFA Capabilities (Type 3)

### What FSTs CAN Do

**1. Character-Level Edit Operations**:
```
Levenshtein distance: insertion, deletion, substitution, transposition
Example: "teh" → "the" (transposition)
Complexity: O(n) with pre-compiled automaton
```

**2. Phonetic Transformations**:
```
Pattern matching: (ph|f), (ough|uff), c→s/_[ei]
Example: "fone" → "phone", "sity" → "city"
Complexity: O(n·|NFA states|)
```

**3. Morphological Variants** (if context-free):
```
Plurals: cat → cats (suffix -s)
Past tense: walk → walked (suffix -ed)
Limitation: Only if rules don't depend on word structure
```

**4. Lexical Normalization**:
```
Abbreviations: "u" → "you", "4" → "for"
Emoticons: ":)" → "happy", ":(" → "sad"
Complexity: O(n) lookup in trie
```

**5. Finite-State Constraints**:
```
Character classes: [aeiou], [^aeiou]
Repetition: a*, a+, a{2,5}
Concatenation, alternation
```

### What FSTs CANNOT Do

**1. Balanced Structures** (requires stack):
```
❌ Balanced parentheses: (a(b)c)
❌ XML tags: <tag>content</tag>
❌ Nested quotes: "He said 'hello' to me"

Why: Requires counting opens vs closes (a^n b^n)
FST has finite memory, cannot count unboundedly
```

**2. Cross-Serial Dependencies**:
```
❌ Swiss German: a^n b^m c^n d^m
❌ English: "The cats that the dogs chased ran"
    (cats_plural ↔ ran_plural, across "that..." clause)

Why: Requires tracking multiple counters
```

**3. Subject-Verb Agreement** (beyond trigram):
```
❌ "The cat [that the dog chased] runs fast"
    (cat_singular ↔ runs_singular, long distance)

✅ "The cat runs" (within FST trigram window)

Why: FST can handle local agreement (3-gram context)
      Cannot handle long-range dependencies
```

**4. Copy Language**:
```
❌ L = {ww | w ∈ Σ*}  (two identical copies)
Example: "abcabc" (first half = second half)

Why: Pumping lemma proof shows not regular
```

**5. Semantic Disambiguation**:
```
❌ "bank" = river bank vs financial bank
❌ "run" = jog vs execute program vs color spreading

Why: Requires world knowledge, not syntactic patterns
```

### Pumping Lemma for Regular Languages

**Theorem**: If L is regular, then ∃p (pumping length) such that ∀s ∈ L with |s| ≥ p,
s can be written as xyz where:
1. |xy| ≤ p
2. |y| > 0
3. xy^i z ∈ L for all i ≥ 0

**Proof that {a^n b^n} is not regular**:

Assume {a^n b^n} is regular with pumping length p.

Let s = a^p b^p ∈ L (clearly |s| ≥ p).

By pumping lemma, s = xyz where |xy| ≤ p and |y| > 0.

Since |xy| ≤ p, both x and y consist only of a's.

So y = a^k for some k > 0.

Pump down (i=0): xz = a^(p-k) b^p.

But (p-k) ≠ p, so xz ∉ {a^n b^n}.

**Contradiction**! Therefore {a^n b^n} is not regular. ∎

---

## CFG Capabilities (Type 2)

### What CFGs CAN Do

**1. Nested Phrase Structures**:
```coq
S → NP VP
NP → DT N | NP PP
PP → P NP

Example: "the cat [on the mat [in the room]]"
         (nested PPs)
```

**2. Subject-Verb Agreement**:
```coq
S → NP[num=X] VP[num=X]

Example: "The cats run" (plural ↔ plural)
         "The cat runs" (singular ↔ singular)

Feature unification ensures agreement
```

**3. Balanced Structures**:
```coq
S → ε
S → ( S )
S → S S

Language: Balanced parentheses
Example: "(()())", "((()))", "()()"
```

**4. Arithmetic Expressions**:
```coq
E → E + E | E * E | (E) | number

Example: "1 + (2 * 3)"
Correctly handles operator precedence with grammar rules
```

**5. Determiners and Articles**:
```coq
NP → DT[a] N[-vowel_initial]
NP → DT[an] N[+vowel_initial]

Example: "an apple", "a banana"
Phonological context handled via features
```

**6. Auxiliary Verb Selection**:
```coq
VP → AUX[modal] VP[inf]
VP → AUX[have] VP[past_part]

Example: "can swim" (modal + infinitive)
         "have eaten" (perfect aspect)
```

### What CFGs CANNOT Do

**1. Cross-Serial Dependencies** (a^n b^m c^n d^m):
```
❌ Swiss German: "mer em Hans es huus hälfe aastriiche"
   (we Hans_DAT the house_ACC help paint)
   Datives and accusatives cross-serialize

Why: CFG stack processes in LIFO order
     Cannot interleave two dependency chains
```

**2. Copy Language** (ww):
```
❌ L = {ww | w ∈ Σ*}
Example: "abcabc"

Why: Pumping lemma for CFLs
     Cannot verify first half = second half
```

**3. MIX Language** ({a,b,c}* with equal counts):
```
❌ L = {w ∈ {a,b,c}* | #_a(w) = #_b(w) = #_c(w)}
Example: "aabbcc", "abcabc", "cabacb"

Why: Requires three independent counters
     CFG stack can only count one dependency
```

**4. Semantic Constraints**:
```
❌ "Colorless green ideas sleep furiously" (syntactically valid, semantically nonsense)
❌ "The table ate the chair" (syntax OK, semantics wrong)

Why: CFG models syntax, not meaning
```

**5. Long-Range Anaphora**:
```
❌ "John said Mary thought Bill believed [he]_? was right"
   (who does "he" refer to? John, Bill, or someone else?)

Why: Requires discourse model, world knowledge
```

### Pumping Lemma for Context-Free Languages

**Theorem**: If L is context-free, then ∃p (pumping length) such that ∀s ∈ L with |s| ≥ p,
s can be written as uvxyz where:
1. |vxy| ≤ p
2. |vy| > 0
3. uv^i xy^i z ∈ L for all i ≥ 0

**Proof that {a^n b^n c^n} is not context-free**:

Assume {a^n b^n c^n} is context-free with pumping length p.

Let s = a^p b^p c^p ∈ L.

By pumping lemma, s = uvxyz where |vxy| ≤ p and |vy| > 0.

**Case 1**: vxy contains only a's.
Pump up (i=2): uv²xy²z = a^(p+k) b^p c^p for some k > 0.
Not in L (unequal counts).

**Case 2**: vxy contains only b's.
Similar argument.

**Case 3**: vxy contains only c's.
Similar argument.

**Case 4**: vxy spans a's and b's (but not c's, since |vxy| ≤ p).
Pump up: increases a's and/or b's, but not c's.
Not in L.

**Case 5**: vxy spans b's and c's (but not a's).
Similar argument.

All cases lead to contradiction! Therefore {a^n b^n c^n} is not context-free. ∎

---

## Neural Capabilities (Beyond Type 2)

### What Neural Models CAN Do

**1. Semantic Disambiguation**:
```
Input: "I went to the bank"
Context: "to deposit money" → financial bank
Context: "by the river" → river bank

Method: Contextual embeddings (BERT, GPT)
```

**2. Discourse Coherence**:
```
Input: "Mary loves ice cream. She ate it yesterday."
Resolve: "She" → Mary, "it" → ice cream

Method: Coreference resolution, transformer attention
```

**3. Pragmatic Inference**:
```
Input: "Can you pass the salt?"
Interpretation: Request (not yes/no question)

Method: Learned pragmatic conventions
```

**4. Style Transfer**:
```
Input: "hey whats up lol"
Output: "Hello, how are you?"

Method: Seq2seq with style embeddings
```

**5. Cross-Lingual Transfer**:
```
Input: "Je suis un étudiant"
Output: "I am a student"

Method: Multilingual transformers (mBERT, XLM)
```

**6. Creative Generation**:
```
Input: "Once upon a time"
Output: "there was a dragon who loved to read books..."

Method: Autoregressive LM (GPT-3, Claude)
```

### What Neural Models CANNOT Guarantee

**1. Determinism**:
```
❌ Same input may yield different outputs (temperature, sampling)
✅ CFG/FST: Same input → same output (reproducible)
```

**2. Unrecoverable Errors**:
```
❌ "I have 5 apples" → "I have 7 bananas" (hallucination)
❌ "Meet at 3pm" → "Meet at 5am" (date/time errors)

Why: No hard constraints, only soft probabilities
```

**3. Formal Correctness**:
```
❌ "2 + 2 = 5" (arithmetic error)
❌ Balanced parentheses (may produce unbalanced)

Why: Learned patterns, not formal rules
```

**4. Explainability**:
```
❌ "Why did you change 'seen' to 'saw'?"
    Neural answer: "High probability in context"
    (black box, no interpretable rule)

✅ CFG/FST: Point to explicit grammar rule
```

**5. Low-Latency Guarantees**:
```
❌ Transformer inference: 50-500ms (variable)
✅ FST: <10ms (constant for given input size)
```

---

## Parsing Complexity Trade-offs

### Time Complexity

| Formalism | Recognition | Parsing | Example |
|-----------|-------------|---------|---------|
| FST | O(n) | O(n) | Levenshtein |
| NFA | O(n·\|Q\|) | O(n·\|Q\|) | Phonetic regex |
| CYK (CNF) | O(n³·\|G\|) | O(n³·\|G\|) | Grammar |
| Earley (general CFG) | O(n³) worst, O(n²) avg | O(n³) | Grammar |
| Transformer | O(n²·d) | O(n²·d) | BERT, GPT |

**Legend**:
- n = input length
- |Q| = NFA state count
- |G| = grammar size (productions)
- d = model dimension (768, 1024, etc.)

### Space Complexity

| Formalism | Memory | Notes |
|-----------|--------|-------|
| FST | O(\|Q\|) | States + transitions (constant per input) |
| NFA | O(\|Q\|) | Active state set |
| DFA | O(2^{\|Q\|}) | Exponential blowup in worst case |
| CYK | O(n²·\|G\|) | Chart size (triangular matrix) |
| Earley | O(n²·\|G\|) | State sets (can be sparse) |
| Transformer | O(n²) | Attention matrix |

### Latency Benchmarks (Estimated)

**Input**: 50-character sentence

| Layer | Formalism | Latency | Throughput |
|-------|-----------|---------|------------|
| Tier 1 (FST) | Levenshtein | <5ms | >10,000 sent/sec |
| Tier 1 (NFA) | Phonetic regex | <10ms | >5,000 sent/sec |
| Tier 2 (CFG) | Earley parser | <100ms | >500 sent/sec |
| Tier 3 (Neural) | BERT-base (CPU) | ~200ms | >25 sent/sec |
| Tier 3 (Neural) | BERT-base (GPU) | ~50ms | >100 sent/sec |
| Tier 3 (Neural) | GPT-3 API | ~500ms | Variable (rate-limited) |

**Recommendation**: Start with Tier 1, add Tier 2 only if needed, Tier 3 as last resort.

---

## When to Use Each Layer

### Decision Tree

```
Start: Is error character-level (spelling, typo)?
  ├─ YES → Use FST (Tier 1)
  │   └─ Levenshtein automaton (O(n), <10ms)
  │
  └─ NO → Is error phonetic (sound-based)?
      ├─ YES → Use NFA (Tier 1)
      │   └─ Phonetic regex (O(n·|NFA|), <20ms)
      └─ NO → Does error require phrase structure (grammar)?
          ├─ YES → Use CFG (Tier 2)
          │   └─ Earley parser (O(n³), <200ms)
          └─ NO → Does error require semantics/discourse?
              ├─ YES → Use Neural (Tier 3)
              │   └─ BERT/GPT (O(n²), 50-500ms)
              └─ NO → Error not handleable (out of scope)
```

### Error Type Examples

**Tier 1 (FST)** - Use for:
- ✅ Spelling: "teh" → "the"
- ✅ Typos: "helo" → "hello"
- ✅ Abbreviations: "u" → "you"
- ✅ Emoticons: ":)" → "happy"
- ✅ Morphology (simple): "cats" → "cat"

**Tier 1 (NFA)** - Use for:
- ✅ Phonetic: "fone" → "phone"
- ✅ Sound patterns: "nite" → "night"
- ✅ Dialect: "wanna" → "want to"

**Tier 2 (CFG)** - Use for:
- ✅ Articles: "a apple" → "an apple"
- ✅ Agreement: "they was" → "they were"
- ✅ Auxiliaries: "can able" → "can" or "is able"
- ✅ Tense: "yesterday he eats" → "yesterday he ate"

**Tier 3 (Neural)** - Use for:
- ✅ Semantic: "bank" → river/financial (context)
- ✅ Anaphora: "She" → who?
- ✅ Pragmatics: politeness, formality
- ✅ Style: informal → formal

### Performance vs Accuracy

**Fast Mode** (<50ms total):
```
[FST + NFA only]
Latency: <20ms
Accuracy: ~85%
Use case: Mobile keyboard, real-time chat
```

**Balanced Mode** (<300ms total):
```
[FST + NFA + CFG]
Latency: ~200ms
Accuracy: ~92%
Use case: Desktop editor, batch processing
```

**Accurate Mode** (<1s total):
```
[FST + NFA + CFG + Neural]
Latency: ~500ms
Accuracy: ~96%
Use case: Document polishing, professional writing
```

---

## Common Pitfalls

### Pitfall 1: Using Neural for Everything

**❌ Mistake**:
```rust
// Bad: Neural LM for simple spelling correction
let corrected = bert_model.correct("teh cat");
// Latency: 200ms, may hallucinate
```

**✅ Correct**:
```rust
// Good: FST for spelling, fast and deterministic
let corrected = levenshtein.query("teh", 1).next().unwrap();
// Latency: 2ms, guaranteed correct if in dictionary
```

**Why**: Over-powered tool adds latency without benefit.

### Pitfall 2: Using FST for Grammar

**❌ Mistake**:
```rust
// Bad: FST cannot handle subject-verb agreement
let fst = compile_regex("the cat run|the cats run|the cat runs|the cats run");
// Doesn't scale, misses patterns
```

**✅ Correct**:
```rust
// Good: CFG with feature unification
let cfg = parse_grammar("S → NP[num=X] VP[num=X]");
// Handles all cases systematically
```

**Why**: Under-powered tool cannot express the constraint.

### Pitfall 3: Exponential DFA Blowup

**❌ Mistake**:
```rust
// Bad: NFA → DFA conversion for complex patterns
let nfa = compile_regex("(a|b)*c(d|e)*f(g|h)*");
let dfa = nfa.to_dfa();  // May create 2^|states| states!
```

**✅ Correct**:
```rust
// Good: Keep as NFA, use lazy DFA
let nfa = compile_regex("(a|b)*c(d|e)*f(g|h)*");
let lazy_dfa = LazyDFA::new(nfa);  // Build states on-demand
```

**Why**: Subset construction can cause exponential blowup.

### Pitfall 4: Ignoring Pumping Lemma

**❌ Mistake**:
```rust
// Bad: Trying to use FST for balanced parentheses
let fst = compile_regex("()*");  // Doesn't work!
```

**✅ Correct**:
```rust
// Good: Use CFG for balanced structures
let cfg = parse_grammar("S → ε | (S) | SS");
```

**Why**: FST fundamentally cannot count (pumping lemma).

### Pitfall 5: Trusting Neural Without Constraints

**❌ Mistake**:
```rust
// Bad: Neural model may hallucinate dates
let corrected = gpt3("I have a meeting on 3pm");
// Possible output: "I have a meeting on 5am" ❌
```

**✅ Correct**:
```rust
// Good: FST constraints + Neural ranking
let candidates = fst.extract_times("3pm");  // ["3pm", "15:00"]
let best = neural_lm.rank(candidates, context);
```

**Why**: Neural models lack hard constraints (unrecoverable errors).

---

## Design Recommendations

### Recommendation 1: Start Simple

**Principle**: Use the simplest formalism that solves the problem.

```
Decision order:
1. Try FST first (O(n), fast)
2. If FST insufficient, try CFG (O(n³), slower but correct)
3. If CFG insufficient, try Neural (O(n²), slowest but flexible)
```

### Recommendation 2: Hybrid Pipelines

**Architecture**: Layer symbolic (FST/CFG) with neural ranking.

```rust
// Symbolic generates candidates (deterministic)
let fst_candidates = levenshtein.query(input, 2);
let cfg_candidates = earley.parse_lattice(fst_candidates);

// Neural ranks candidates (context-aware)
let best = bert.rank(cfg_candidates, context);
```

**Benefits**:
- Deterministic candidate generation (no hallucination)
- Context-aware selection (neural strengths)

### Recommendation 3: Know Your Complexity

**Scaling**:
- FST: Scales to millions of words (O(n))
- CFG: Scales to hundreds of words (O(n³))
- Neural: Scales to hundreds of words (O(n²), but high constant)

**Rule of Thumb**:
- Input <100 chars → All tiers feasible
- Input 100-500 chars → FST + CFG feasible, Neural slow
- Input >500 chars → FST only, or split into chunks

### Recommendation 4: Formal Verification Where Possible

**Priority**:
1. **Critical rules**: Formally verify (Coq, Isabelle)
2. **Non-critical rules**: Extensive testing
3. **Neural**: Constrain with symbolic checks

**Example** (liblevenshtein-rust):
```coq
(* Coq-verified phonetic rules *)
Theorem ph_to_f_preserves_length : ...
Theorem rule_application_terminates : ...

(* Compile to FST, preserving properties *)
let fst = PhoneticFST::from_verified_rules(coq_rules);
```

### Recommendation 5: Measure, Don't Guess

**Benchmark**:
```rust
#[bench]
fn bench_fst_vs_cfg_vs_neural(b: &mut Bencher) {
    let input = "the cat run fast";

    b.iter(|| {
        // Tier 1: FST
        let t1 = Instant::now();
        let fst_result = levenshtein.correct(input);
        let fst_time = t1.elapsed();

        // Tier 2: CFG
        let t2 = Instant::now();
        let cfg_result = earley.correct(input);
        let cfg_time = t2.elapsed();

        // Tier 3: Neural
        let t3 = Instant::now();
        let neural_result = bert.correct(input);
        let neural_time = t3.elapsed();

        println!("FST: {:?}, CFG: {:?}, Neural: {:?}",
                 fst_time, cfg_time, neural_time);
    });
}
```

**Hypothesis**: Test, measure, validate. Don't assume.

---

## Summary Table

| Aspect | FST/NFA (Type 3) | CFG (Type 2) | Neural (Beyond Type 2) |
|--------|------------------|--------------|------------------------|
| **Language Class** | Regular | Context-Free | Unrestricted |
| **Memory** | Finite states | Stack (unbounded) | Learned weights |
| **Complexity** | O(n) | O(n³) CYK, O(n²) avg | O(n²) transformer |
| **Latency** | <10ms | <200ms | 50-500ms |
| **Deterministic** | ✅ Yes | ✅ Yes (with PCFG) | ❌ No |
| **Can Count** | ❌ No | ✅ Yes (one stack) | ✅ Yes (learned) |
| **Can Nest** | ❌ No | ✅ Yes (balanced) | ✅ Yes |
| **Semantics** | ❌ No | ❌ No | ✅ Yes |
| **Formal Verification** | ✅ Possible | ✅ Possible | ❌ Difficult |
| **Training Data** | ❌ Not needed | ❌ Not needed | ✅ Required (large) |
| **Hallucination Risk** | ✅ None | ✅ None | ❌ High |
| **Explainability** | ✅ Full | ✅ Full | ❌ Limited |

**Recommendation**: Use FST → CFG → Neural progression (simplest sufficient formalism).

---

## References

### Formal Language Theory

1. **Hopcroft, J.E., Motwani, R., Ullman, J.D.** (2006). Introduction to Automata Theory, Languages, and Computation (3rd ed.). Pearson.
   - Comprehensive treatment of Chomsky hierarchy

2. **Sipser, M.** (2012). Introduction to the Theory of Computation (3rd ed.). Cengage Learning.
   - Pumping lemmas, decidability

3. **Chomsky, N.** (1956). Three models for the description of language. IRE Transactions on Information Theory, 2(3), 113-124.
   - Original hierarchy definition

### Text Normalization Limitations

4. **Sproat, R., Jaitly, N.** (2016). RNN Approaches to Text Normalization: A Challenge. arXiv:1611.00068.
   - "Unrecoverable errors" in neural approaches

5. **Bakhturina, E., et al.** (2021). NeMo Inverse Text Normalization: From Development To Production. arXiv:2104.05055.
   - "Low tolerance towards unrecoverable errors is the main reason why most ITN systems in production are still largely rule-based"

### Complexity Analysis

6. **Aho, A.V., Ullman, J.D.** (1972). The Theory of Parsing, Translation, and Compiling. Prentice-Hall.
   - Parsing complexity analysis

7. **Earley, J.** (1970). An efficient context-free parsing algorithm. Communications of the ACM, 13(2), 94-102.
   - O(n³) worst case, O(n²) average for unambiguous grammars