liblevenshtein 0.9.1

Levenshtein/Universal Automata for approximate string matching using various dictionary backends
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
//! Generalized Levenshtein Automaton
//!
//! Implements a Levenshtein automaton with runtime-configurable operations.
//!
//! # Design Philosophy
//!
//! `GeneralizedAutomaton` complements `UniversalAutomaton` by trading compile-time
//! specialization for runtime flexibility:
//!
//! - **UniversalAutomaton**: Compile-time operations (Standard, Transposition, MergeAndSplit)
//!   - Zero runtime overhead
//!   - Fixed operation sets
//!   - Perfect for standard Levenshtein variants
//!
//! - **GeneralizedAutomaton**: Runtime operations (Phase 1: standard only)
//!   - Small runtime overhead (+10-20% estimate)
//!   - Future: Custom operation sets
//!   - Perfect for phonetic corrections and custom metrics
//!
//! # Phase 1 Implementation
//!
//! Current implementation supports **standard operations only** (match, substitute, insert, delete).
//! Future phases will add:
//! - Runtime OperationSet parameter
//! - Multi-character operations
//! - Custom operation sets
//!
//! # Theory Background
//!
//! ## Definition 15: Universal Levenshtein Automaton (Page 30)
//!
//! ```text
//! A^∀,χ_n = ⟨Σ^∀_n, Q^∀,χ_n, I^∀,χ, F^∀,χ_n, δ^∀,χ_n⟩
//! ```
//!
//! Where:
//! - **Σ^∀_n**: Bit vectors of length ≤ 2n + 2
//! - **Q^∀,χ_n**: State space (I^χ_states ∪ M^χ_states)
//! - **I^∀,χ**: Initial state {I#0}
//! - **F^∀,χ_n**: Final states (states with M-type positions)
//! - **δ^∀,χ_n**: Transition function
//!
//! ## Acceptance Condition (Page 48)
//!
//! Given a word w and input x, the automaton accepts if:
//! 1. Encode the pair as h_n(w, x) = β(x₁, s_n(w,1))...β(x_t, s_n(w,t))
//! 2. Process the bit vector sequence: δ^∀,χ_n*(I^∀,χ, h_n(w, x))
//! 3. Check if the resulting state is final (contains M-type positions)
//!
//! # Examples
//!
//! ```ignore
//! use liblevenshtein::transducer::generalized::GeneralizedAutomaton;
//!
//! // Create automaton for maximum distance n=2
//! let automaton = GeneralizedAutomaton::new(2);
//!
//! // Check if "test" accepts "text" (distance 1)
//! assert!(automaton.accepts("test", "text"));
//!
//! // Check if "test" accepts "hello" (distance > 2)
//! assert!(!automaton.accepts("test", "hello"));
//! ```

use super::state::GeneralizedState;
use crate::transducer::universal::bit_vector::CharacteristicVector;
use crate::transducer::OperationSet;

/// Generalized Levenshtein Automaton A^∀,χ_n
///
/// A parameter-free automaton that recognizes the Levenshtein neighborhood
/// L^χ_{Lev}(n, w) for any word w without modification.
///
/// # Phase 2: Runtime-Configurable Operations
///
/// Supports custom operation sets including:
/// - Standard operations (match, substitute, insert, delete)
/// - Transposition
/// - Multi-character operations (phonetic corrections, merge/split)
/// - Weighted operations with custom costs
///
/// # Examples
///
/// ```ignore
/// use liblevenshtein::transducer::generalized::GeneralizedAutomaton;
/// use liblevenshtein::transducer::OperationSet;
///
/// // Standard Levenshtein
/// let automaton = GeneralizedAutomaton::new(2);
///
/// // With transposition
/// let automaton = GeneralizedAutomaton::with_operations(
///     2,
///     OperationSet::with_transposition()
/// );
///
/// // Check if "test" accepts "text" (distance 1)
/// assert!(automaton.accepts("test", "text"));
/// ```
#[derive(Debug, Clone)]
pub struct GeneralizedAutomaton {
    /// Maximum edit distance n
    max_distance: u8,

    /// Set of operations defining the edit distance metric
    operations: OperationSet,
}

impl GeneralizedAutomaton {
    /// Create a new Generalized Levenshtein Automaton with standard operations
    ///
    /// Uses the standard operation set (match, substitute, insert, delete).
    ///
    /// # Arguments
    ///
    /// - `max_distance`: Maximum edit distance n (typically 1, 2, or 3)
    ///
    /// # Returns
    ///
    /// A new `GeneralizedAutomaton` instance with standard operations
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let automaton = GeneralizedAutomaton::new(2);
    /// ```
    #[must_use]
    pub fn new(max_distance: u8) -> Self {
        Self {
            max_distance,
            operations: OperationSet::standard(),
        }
    }

    /// Create a new Generalized Levenshtein Automaton with custom operations
    ///
    /// # Arguments
    ///
    /// - `max_distance`: Maximum edit distance n (typically 1, 2, or 3)
    /// - `operations`: Set of operations defining the edit distance metric
    ///
    /// # Returns
    ///
    /// A new `GeneralizedAutomaton` instance with custom operations
    ///
    /// # Examples
    ///
    /// ```ignore
    /// // Automaton with transposition support
    /// let automaton = GeneralizedAutomaton::with_operations(
    ///     2,
    ///     OperationSet::with_transposition()
    /// );
    ///
    /// // Automaton with custom phonetic operations
    /// let mut ops = OperationSet::standard();
    /// ops.add_merge("ph", "f", 0);  // "ph" → "f" with no cost
    /// let automaton = GeneralizedAutomaton::with_operations(2, ops);
    /// ```
    #[must_use]
    pub fn with_operations(max_distance: u8, operations: OperationSet) -> Self {
        Self {
            max_distance,
            operations,
        }
    }

    /// Get the maximum edit distance n
    #[must_use]
    pub fn max_distance(&self) -> u8 {
        self.max_distance
    }

    /// Create the initial state I^∀,χ = {I#0}
    ///
    /// From thesis page 38: The initial state contains a single I-type position
    /// with offset 0 and 0 errors.
    ///
    /// # Returns
    ///
    /// Initial state {I#0}
    fn initial_state(&self) -> GeneralizedState {
        GeneralizedState::initial(self.max_distance)
    }

    /// Check if a state is accepting according to Levenshtein distance criterion
    ///
    /// From thesis page 24 (Proposition 11): A position i#e is accepting if:
    /// ```text
    /// p - i ≤ n - e
    /// ```
    /// Where p is word length, i is position, n is max distance, e is errors.
    ///
    /// This translates to: remaining characters ≤ remaining error budget.
    ///
    /// # Arguments
    ///
    /// - `state`: State to check after processing all input
    /// - `word_len`: Length of dictionary word
    /// - `input_len`: Length of input string processed
    ///
    /// # Returns
    ///
    /// `true` if state is accepting (within distance n), `false` otherwise
    #[must_use]
    fn is_accepting(&self, state: &GeneralizedState, word_len: usize, input_len: usize) -> bool {
        use crate::transducer::generalized::GeneralizedPosition;

        let n = self.max_distance as i32;

        state.positions().any(|pos| {
            match pos {
                GeneralizedPosition::MFinal { offset, errors } => {
                    // M-type positions are past the word end
                    // They're accepting if offset ≤ 0 and errors ≤ n
                    *offset <= 0 && *errors <= self.max_distance
                }
                GeneralizedPosition::INonFinal { .. } => {
                    // I-type positions: check if we can reach word end with remaining errors
                    // Current word position in the word = input_len + offset
                    let current_word_pos = input_len as i32 + pos.offset();

                    // Check bounds
                    if current_word_pos < 0 {
                        return false; // Before word start
                    }

                    // Remaining characters to match = word_len - current_word_pos
                    let remaining_chars = word_len as i32 - current_word_pos;

                    // Remaining error budget = max_distance - errors_used
                    let remaining_errors = n - (pos.errors() as i32);

                    // Accept if we can delete/skip remaining characters with available errors
                    // Proposition 11: p - i ≤ n - e
                    // where p = word_len, i = current_word_pos, e = errors used, n = max_distance
                    // Phase 4: If remaining_chars < 0, we're past the word end (acceptable)
                    // If remaining_chars >= 0, we need enough errors to delete them
                    remaining_chars <= remaining_errors
                }
                // Phase 2d: Transposing and splitting positions are intermediate states
                // They are not accepting states (operation must complete first)
                GeneralizedPosition::ITransposing { .. }
                | GeneralizedPosition::MTransposing { .. }
                | GeneralizedPosition::ISplitting { .. }
                | GeneralizedPosition::MSplitting { .. } => false,
            }
        })
    }

    /// Check if word w accepts input x within the maximum distance
    ///
    /// From thesis page 51-52: Encodes the pair (w, x) as h_n(w, x) and
    /// processes it through the automaton.
    ///
    /// # Arguments
    ///
    /// - `word`: Dictionary word w
    /// - `input`: Input string x to match against
    ///
    /// # Returns
    ///
    /// `true` if Lev(w, x) ≤ n, `false` otherwise
    ///
    /// # Algorithm
    ///
    /// 1. Start with initial state I^∀,χ = {I#0}
    /// 2. For each character x_i in input:
    ///    - Compute relevant subword s_n(w, i)
    ///    - Compute characteristic vector β(x_i, s_n(w, i))
    ///    - Apply transition: state := δ^∀,χ_n(state, β)
    /// 3. Check if final state is accepting
    ///
    /// # Phase 1 Note
    ///
    /// Uses standard operations only (match, substitute, insert, delete).
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let automaton = GeneralizedAutomaton::new(2);
    ///
    /// // Distance 1: one substitution
    /// assert!(automaton.accepts("test", "text"));
    ///
    /// // Distance 0: identical
    /// assert!(automaton.accepts("test", "test"));
    ///
    /// // Distance 3: too far
    /// assert!(!automaton.accepts("test", "hello"));
    /// ```
    pub fn accepts(&self, word: &str, input: &str) -> bool {
        // Special case 1: Empty input (outside domain of h_n from thesis page 51)
        // From Levenshtein definition: d(w, ε) = |w|
        // Accept if |w| ≤ n
        if input.is_empty() {
            return word.len() <= self.max_distance as usize;
        }

        // Special case 2: Input too long (encoding h_n undefined)
        // From thesis page 51: h_n(w, x) defined only if |x| ≤ |w| + n
        // However, expansion operations (e.g., split ⟨1,2⟩) can increase effective word length
        // Phase 3b: Calculate maximum expansion from operations
        let max_expansion = self
            .operations
            .operations()
            .iter()
            .map(|op| op.consume_y().saturating_sub(op.consume_x()))
            .max()
            .unwrap_or(0);

        if max_expansion > 0 {
            // With expansion operations (e.g., k→ch split):
            // Upper bound: each word char could expand by max_expansion, plus insertions
            // Example: "kat" with 2 splits ⟨1,2⟩ → 3 + 2*1 = 5 effective chars
            // Conservative bound: |x| ≤ |w| * (1 + max_expansion) + n
            let max_len = word.len() * (1 + max_expansion as usize) + self.max_distance as usize;
            if input.len() > max_len {
                return false;
            }
        } else {
            // Standard Levenshtein bound: |x| ≤ |w| + n
            if input.len() > word.len() + self.max_distance as usize {
                return false;
            }
        }

        // Start with initial state {I#0}
        let mut state = self.initial_state();

        // H2 Optimization: Conditionally pre-compute character vector
        // Only pre-compute for max_distance > 1 (eliminates overhead for trivial cases)
        // For distance > 1: eliminates 20+ repeated word.chars().collect() calls
        // Target: 8.47% of cycles (Iterator::collect 4.08% + cfree 4.39%)
        let word_chars: Option<Vec<char>> = if self.max_distance > 1 {
            Some(word.chars().collect())
        } else {
            None
        };

        // Process each character of input
        // This generates the bit vector sequence h_n(w, x) = β(x₁, s_n(w,1))...β(x_t, s_n(w,t))
        for (i, input_char) in input.chars().enumerate() {
            // Compute relevant subword s_n(w, i+1)
            // From thesis page 51: s_n(w, i) = w_{i-n}...w_{min(|w|, i+n+1)}
            let subword = self.relevant_subword(word, i + 1);

            #[cfg(debug_assertions)]
            eprintln!(
                "\n[DEBUG] === Input position i={}, char='{}' ===",
                i, input_char
            );
            #[cfg(debug_assertions)]
            eprintln!("  Subword: {:?}", subword);
            #[cfg(debug_assertions)]
            eprintln!(
                "  State before transition: {} positions",
                state.positions().count()
            );

            // Compute characteristic vector β(x_i, s_n(w, i))
            let bit_vector = CharacteristicVector::new(input_char, &subword);

            // Apply transition: state := δ^∀,χ_n(state, β)
            // Phase 3b: Pass full word, word slice, and input character for phonetic operations
            // H2 Optimization: Pass pre-computed character vector (conditional for distance > 1)
            if let Some(next_state) = state.transition(
                &self.operations,
                &bit_vector,
                word,
                word_chars.as_deref(),
                &subword,
                input_char,
                i + 1,
            ) {
                #[cfg(debug_assertions)]
                eprintln!(
                    "  State after transition: {} positions",
                    next_state.positions().count()
                );
                state = next_state;
            } else {
                // Transition failed, reject
                #[cfg(debug_assertions)]
                eprintln!("  ✗ Transition failed, rejecting");
                return false;
            }
        }

        // Check acceptance using Proposition 11 criterion (thesis page 24)
        // A position i#e is accepting if: p - i ≤ n - e
        // (remaining characters ≤ remaining error budget)
        #[cfg(debug_assertions)]
        {
            eprintln!("\n[DEBUG] Final state positions:");
            for pos in state.positions() {
                eprintln!("[DEBUG]   {}", pos);
            }
        }
        let accepted = self.is_accepting(&state, word.len(), input.len());
        #[cfg(debug_assertions)]
        eprintln!("[DEBUG] Accepted: {}", accepted);
        accepted
    }

    /// Compute relevant subword s_n(w, i)
    ///
    /// From thesis page 51:
    /// ```text
    /// s_n(w, i) = w_{i-n}w_{i-n+1}...w_v
    /// where v = min(|w|, i + n + 1)
    /// ```
    ///
    /// Pad with '$' for positions before start of word.
    ///
    /// # Arguments
    ///
    /// - `word`: Dictionary word w
    /// - `position`: Position i (1-indexed)
    ///
    /// # Returns
    ///
    /// Relevant subword around position i
    fn relevant_subword(&self, word: &str, position: usize) -> String {
        let n = self.max_distance as i32;
        let i = position as i32;

        // From thesis page 51: s_n(w, i) = w_{i-n}...w_v where v = min(|w|, i + n + 1)
        // The notation w_a...w_b means positions from a through b inclusive (1-indexed)
        let start = i - n;
        let v = std::cmp::min(word.len() as i32, i + n + 1);

        let mut result = String::new();

        // Note: positions are 1-indexed in the thesis, but Rust uses 0-indexing
        // The range should be inclusive of v (start..=v would work, but v might be past word end)
        for pos in start..=v {
            if pos < 1 {
                // Before the start of the word - pad with '$'
                result.push('$');
            } else if pos <= word.len() as i32 {
                // Convert 1-indexed position to 0-indexed
                let idx = (pos - 1) as usize;
                if let Some(ch) = word.chars().nth(idx) {
                    result.push(ch);
                }
            }
            // If pos > word.len(), we're past the end - don't add anything
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new() {
        let automaton = GeneralizedAutomaton::new(2);
        assert_eq!(automaton.max_distance(), 2);
    }

    #[test]
    fn test_debug_identical() {
        let automaton = GeneralizedAutomaton::new(2);
        let word = "test";
        let input = "test";

        // Manual step-through
        let mut state = automaton.initial_state();
        eprintln!("\nDEBUG: Initial state = {}", state);

        // H2 Optimization: Pre-compute character vector
        let word_chars: Vec<char> = word.chars().collect();

        for (i, ch) in input.chars().enumerate() {
            eprintln!(
                "\nDEBUG: Processing char {} ('{}') at input position {}",
                i + 1,
                ch,
                i
            );

            let subword = automaton.relevant_subword(word, i + 1);
            eprintln!("DEBUG: Relevant subword = '{}'", subword);

            let bit_vector = CharacteristicVector::new(ch, &subword);
            eprintln!("DEBUG: Bit vector length = {}", bit_vector.len());

            match state.transition(
                &automaton.operations,
                &bit_vector,
                word,
                Some(&word_chars),
                &subword,
                ch,
                i + 1,
            ) {
                Some(next) => {
                    eprintln!("DEBUG: Next state = {}", next);
                    state = next;
                }
                None => {
                    panic!("Transition failed at position {}", i);
                }
            }
        }

        eprintln!("\nDEBUG: Final state = {}", state);
        eprintln!(
            "DEBUG: Word length = {}, Input length = {}",
            word.len(),
            input.len()
        );

        // Check acceptance
        let is_accepting = automaton.is_accepting(&state, word.len(), input.len());
        eprintln!("DEBUG: is_accepting = {}", is_accepting);

        if !is_accepting {
            eprintln!("\nDEBUG: Checking each position:");
            for pos in state.positions() {
                let current_word_pos = input.len() as i32 + pos.offset();
                let remaining_chars = word.len() as i32 - current_word_pos;
                let remaining_errors = automaton.max_distance() as i32 - pos.errors() as i32;
                eprintln!("  Position {}: current_word_pos={}, remaining_chars={}, remaining_errors={}, accepting={}",
                         pos, current_word_pos, remaining_chars, remaining_errors,
                         remaining_chars >= 0 && remaining_chars <= remaining_errors);
            }
        }

        assert!(is_accepting, "Should accept identical strings");
    }

    #[test]
    fn test_accepts_identical() {
        let automaton = GeneralizedAutomaton::new(2);
        assert!(automaton.accepts("test", "test"));
        assert!(automaton.accepts("", ""));
        assert!(automaton.accepts("hello", "hello"));
    }

    #[test]
    fn test_accepts_one_substitution() {
        let automaton = GeneralizedAutomaton::new(2);
        assert!(automaton.accepts("test", "text")); // t->x
        assert!(automaton.accepts("hello", "hallo")); // e->a
    }

    #[test]
    fn test_debug_one_insertion() {
        let automaton = GeneralizedAutomaton::new(2);
        let word = "test";
        let input = "tests";

        eprintln!(
            "\nDEBUG: Testing insertion word='{}', input='{}', max_distance={}",
            word,
            input,
            automaton.max_distance()
        );

        let mut state = automaton.initial_state();
        eprintln!("Initial state: {}", state);

        // H2 Optimization: Pre-compute character vector
        let word_chars: Vec<char> = word.chars().collect();

        for (i, ch) in input.chars().enumerate() {
            eprintln!(
                "\n--- Processing char {} ('{}') at position {} ---",
                i + 1,
                ch,
                i + 1
            );

            let subword = automaton.relevant_subword(word, i + 1);
            eprintln!("Relevant subword: '{}'", subword);

            let bit_vector = CharacteristicVector::new(ch, &subword);
            eprintln!("Bit vector length: {}", bit_vector.len());

            match state.transition(
                &automaton.operations,
                &bit_vector,
                word,
                Some(&word_chars),
                &subword,
                ch,
                i + 1,
            ) {
                Some(next) => {
                    eprintln!("Next state: {}", next);
                    state = next;
                }
                None => {
                    eprintln!("Transition failed!");
                    panic!("Should not fail at position {}", i + 1);
                }
            }
        }

        eprintln!("\n--- Final state check ---");
        eprintln!("Final state: {}", state);
        eprintln!("Word length: {}", word.len());
        eprintln!("Input length: {}", input.len());

        let n = automaton.max_distance() as i32;
        for pos in state.positions() {
            let current_word_pos = input.len() as i32 + pos.offset();
            let remaining_chars = word.len() as i32 - current_word_pos;
            let remaining_errors = n - (pos.errors() as i32);

            eprintln!("\nPosition: {}", pos);
            eprintln!(
                "  current_word_pos = {} + {} = {}",
                input.len(),
                pos.offset(),
                current_word_pos
            );
            eprintln!(
                "  remaining_chars = {} - {} = {}",
                word.len(),
                current_word_pos,
                remaining_chars
            );
            eprintln!(
                "  remaining_errors = {} - {} = {}",
                n,
                pos.errors(),
                remaining_errors
            );
            eprintln!(
                "  Accept? {} >= 0 && {} <= {} = {}",
                remaining_chars,
                remaining_chars,
                remaining_errors,
                remaining_chars >= 0 && remaining_chars <= remaining_errors
            );
        }

        let is_accepting = automaton.is_accepting(&state, word.len(), input.len());
        eprintln!("\nFinal is_accepting result: {}", is_accepting);

        assert!(is_accepting, "Should accept insertion");
    }

    #[test]
    fn test_accepts_one_insertion() {
        let automaton = GeneralizedAutomaton::new(2);
        assert!(automaton.accepts("test", "tests")); // +s at end
        assert!(automaton.accepts("test", "ttest")); // +t at start
    }

    #[test]
    fn test_accepts_one_deletion() {
        let automaton = GeneralizedAutomaton::new(2);
        assert!(automaton.accepts("tests", "test")); // -s at end
        assert!(automaton.accepts("ttest", "test")); // -t at start
    }

    #[test]
    fn test_rejects_too_far() {
        let automaton = GeneralizedAutomaton::new(2);
        assert!(!automaton.accepts("test", "hello")); // distance 4
        assert!(!automaton.accepts("abc", "xyz")); // distance 3
    }

    #[test]
    fn test_empty_input() {
        let automaton = GeneralizedAutomaton::new(2);
        assert!(automaton.accepts("", "")); // distance 0
        assert!(automaton.accepts("ab", "")); // distance 2
        assert!(!automaton.accepts("abc", "")); // distance 3 > 2
    }

    #[test]
    fn test_empty_word() {
        let automaton = GeneralizedAutomaton::new(2);
        assert!(automaton.accepts("", "ab")); // distance 2
        assert!(!automaton.accepts("", "abc")); // distance 3 > 2
    }

    #[test]
    fn test_max_distance_zero() {
        let automaton = GeneralizedAutomaton::new(0);
        assert!(automaton.accepts("test", "test")); // exact match
        assert!(!automaton.accepts("test", "text")); // any difference rejected
    }

    #[test]
    fn test_debug_deletion_middle() {
        let automaton = GeneralizedAutomaton::new(1);
        let word = "test";
        let input = "tst"; // Missing 'e' in middle

        eprintln!(
            "\nDEBUG: Testing deletion in middle: word='{}', input='{}', max_distance={}",
            word,
            input,
            automaton.max_distance()
        );
        eprintln!("Expected: true (1 deletion)");

        // Manual step-through
        let mut state = automaton.initial_state();
        eprintln!("\nInitial state: {}", state);

        // H2 Optimization: Pre-compute character vector
        let word_chars: Vec<char> = word.chars().collect();

        for (i, ch) in input.chars().enumerate() {
            eprintln!(
                "\n--- Processing char {} ('{}') at position {} ---",
                i + 1,
                ch,
                i + 1
            );

            let subword = automaton.relevant_subword(word, i + 1);
            eprintln!("Relevant subword: '{}'", subword);

            let bit_vector = CharacteristicVector::new(ch, &subword);
            eprintln!("Bit vector length: {}", bit_vector.len());

            match state.transition(
                &automaton.operations,
                &bit_vector,
                word,
                Some(&word_chars),
                &subword,
                ch,
                i + 1,
            ) {
                Some(next) => {
                    eprintln!("Next state: {}", next);
                    state = next;
                }
                None => {
                    eprintln!("Transition failed - no successor state!");
                    panic!("Should not fail at position {}", i + 1);
                }
            }
        }

        eprintln!("\n--- Final state check ---");
        eprintln!("Final state: {}", state);
        eprintln!("Word length: {}", word.len());
        eprintln!("Input length: {}", input.len());

        // Check is_accepting manually for each position
        let n = automaton.max_distance() as i32;
        eprintln!("\nChecking acceptance for each position:");

        use crate::transducer::generalized::GeneralizedPosition;
        for pos in state.positions() {
            match pos {
                GeneralizedPosition::MFinal { offset, errors } => {
                    eprintln!("\nM-type position: offset={}, errors={}", offset, errors);
                    eprintln!(
                        "  M-type accepting? offset <= 0 && errors <= {}: {} <= 0 && {} <= {} = {}",
                        n,
                        offset,
                        errors,
                        n,
                        *offset <= 0 && *errors <= n as u8
                    );
                }
                GeneralizedPosition::INonFinal { offset, errors } => {
                    let current_word_pos = input.len() as i32 + offset;
                    let remaining_chars = word.len() as i32 - current_word_pos;
                    let remaining_errors = n - (*errors as i32);

                    eprintln!("\nI-type position: offset={}, errors={}", offset, errors);
                    eprintln!(
                        "  current_word_pos = {} + {} = {}",
                        input.len(),
                        offset,
                        current_word_pos
                    );
                    eprintln!(
                        "  remaining_chars = {} - {} = {}",
                        word.len(),
                        current_word_pos,
                        remaining_chars
                    );
                    eprintln!(
                        "  remaining_errors = {} - {} = {}",
                        n, errors, remaining_errors
                    );
                    eprintln!(
                        "  Accept? {} >= 0 && {} <= {} = {}",
                        remaining_chars,
                        remaining_chars,
                        remaining_errors,
                        remaining_chars >= 0 && remaining_chars <= remaining_errors
                    );
                }
                // Phase 2d: Debug output for transposing/splitting positions
                GeneralizedPosition::ITransposing { offset, errors } => {
                    eprintln!("\nI-type transposing: offset={}, errors={} (not accepting - intermediate state)", offset, errors);
                }
                GeneralizedPosition::MTransposing { offset, errors } => {
                    eprintln!("\nM-type transposing: offset={}, errors={} (not accepting - intermediate state)", offset, errors);
                }
                GeneralizedPosition::ISplitting { offset, errors, .. } => {
                    eprintln!("\nI-type splitting: offset={}, errors={} (not accepting - intermediate state)", offset, errors);
                }
                GeneralizedPosition::MSplitting { offset, errors, .. } => {
                    eprintln!("\nM-type splitting: offset={}, errors={} (not accepting - intermediate state)", offset, errors);
                }
            }
        }

        let result = automaton.accepts(word, input);
        eprintln!("\n=== Final result: {} ===", result);
        eprintln!("Expected: true");

        assert!(result, "Should accept deletion in middle");
    }

    #[test]
    fn test_max_distance_one() {
        let automaton = GeneralizedAutomaton::new(1);
        assert!(automaton.accepts("test", "text")); // 1 substitution
        assert!(automaton.accepts("test", "tst")); // 1 deletion
        assert!(!automaton.accepts("test", "tx")); // distance 2
    }

    // Phase 2d.3: Transposition tests

    #[test]
    fn test_transposition_distance_zero() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(0, ops);

        assert!(automaton.accepts("test", "test")); // Exact match
        assert!(!automaton.accepts("test", "tset")); // Requires 1 error
        assert!(!automaton.accepts("test", "etst")); // Requires 1 error
    }

    #[test]
    fn test_transposition_adjacent_swap_middle() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "test" → "tset" (swap 'e' and 's')
        assert!(automaton.accepts("test", "tset"));
    }

    #[test]
    fn test_transposition_adjacent_swap_start() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "test" → "etst" (swap 't' and 'e')
        assert!(automaton.accepts("test", "etst"));
    }

    #[test]
    fn test_transposition_adjacent_swap_end() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "test" → "tets" (swap 's' and 't')
        assert!(automaton.accepts("test", "tets"));
    }

    #[test]
    fn test_transposition_longer_words() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "algorithm" → "lagorithm" (swap 'a' and 'l')
        assert!(automaton.accepts("algorithm", "lagorithm"));

        // "programming" → "porgramming" (swap 'r' and 'o')
        assert!(automaton.accepts("programming", "porgramming"));
    }

    #[test]
    fn test_transposition_rejects_non_adjacent() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "test" → "tsta" (non-adjacent swap) requires 2 errors
        // (Cannot swap 'e' and 's' if they're not adjacent)
        assert!(!automaton.accepts("test", "tsta"));

        // "abc" → "cba" (swap non-adjacent) requires 2 errors
        assert!(!automaton.accepts("abc", "cba"));
    }

    #[test]
    fn test_transposition_multiple_swaps() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // "abcd" → "badc" (two adjacent swaps)
        assert!(automaton.accepts("abcd", "badc"));

        // "test" → "etst" → "etts" (two swaps)
        assert!(automaton.accepts("test", "etts"));
    }

    #[test]
    fn test_transposition_with_standard_operations() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // Combine transposition with substitution
        // "test" → "txst" (substitute e→x) → "tsxt" (transpose)
        assert!(automaton.accepts("test", "tsxt"));

        // Transpose + deletion
        // "test" → "tset" (transpose) → "set" (delete 't')
        assert!(automaton.accepts("test", "set"));
    }

    #[test]
    fn test_transposition_empty_and_single() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(1, ops.clone());

        // Empty strings
        assert!(automaton.accepts("", ""));

        // Single character (no transposition possible, but substitution works)
        assert!(automaton.accepts("a", "a"));
        assert!(automaton.accepts("a", "b")); // Accepted via substitution (not transposition)

        // Verify distance 0 rejects difference
        let strict_automaton = GeneralizedAutomaton::with_operations(0, ops);
        assert!(!strict_automaton.accepts("a", "b")); // No errors allowed
    }

    #[test]
    fn test_transposition_two_char_words() {
        let ops = crate::transducer::OperationSet::with_transposition();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Two characters - simple swap
        assert!(automaton.accepts("ab", "ba"));
        assert!(automaton.accepts("xy", "yx"));

        // Should also work with identical chars
        assert!(automaton.accepts("aa", "aa"));
    }

    // Phase 2d.4: Merge tests

    #[test]
    fn test_merge_simple() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "a" → "ab" (merge two input chars "ab" into one word char "a")
        // Note: Direction is word → input, so we're checking if input "ab" matches word "a"
        assert!(automaton.accepts("a", "ab"));
    }

    #[test]
    fn test_merge_at_start() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "ac" → "abc" (merge "ab" into "a", then match "c")
        assert!(automaton.accepts("ac", "abc"));

        // "test" → "teest" (merge "te" into "t", then match "est")
        assert!(automaton.accepts("test", "teest"));
    }

    #[test]
    fn test_merge_at_end() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "xa" → "xab" (match "x", merge "ab" into "a")
        assert!(automaton.accepts("xa", "xab"));

        // "testa" → "testab" (match "test", merge "ab" into "a")
        assert!(automaton.accepts("testa", "testab"));
    }

    #[test]
    fn test_merge_middle() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "cat" → "cabt" (match "c", merge "ab" into "a", match "t")
        assert!(automaton.accepts("cat", "cabt"));
    }

    #[test]
    fn test_merge_distance_zero() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(0, ops);

        // Distance 0: no merge allowed
        assert!(automaton.accepts("test", "test"));
        assert!(!automaton.accepts("test", "teest"));
        assert!(!automaton.accepts("a", "ab"));
    }

    #[test]
    fn test_merge_with_standard_operations() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // Merge + substitution
        // "test" → "texst" (merge "te" into "t", substitute e→x, match "st")
        assert!(automaton.accepts("test", "texst"));

        // Merge + deletion
        // "test" → "teest" (merge) → "eest" (delete first t)
        assert!(automaton.accepts("test", "eest"));
    }

    #[test]
    fn test_merge_empty_and_edge_cases() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Empty strings
        assert!(automaton.accepts("", ""));

        // Single character (merge requires 2 input chars)
        assert!(automaton.accepts("a", "a"));
        assert!(automaton.accepts("a", "ab")); // Merge "ab" → "a"

        // Two char word
        assert!(automaton.accepts("ab", "ab"));
        assert!(automaton.accepts("ab", "abb")); // Merge "ab" → "a", match "b"
    }

    // Phase 2d.5: Split tests

    #[test]
    fn test_split_simple() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "ab" → "a" (split one input char "a" into two word chars "ab")
        // Note: Direction is word → input, so we're checking if input "a" matches word "ab"
        assert!(automaton.accepts("ab", "a"));
    }

    #[test]
    fn test_split_at_start() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "abc" → "ac" (split "a" into "ab", then match "c")
        assert!(automaton.accepts("abc", "ac"));

        // "acd" → "acd" or "acd" → "ad" (split "a" into "ab", match "c", delete "d")
        assert!(automaton.accepts("abcd", "acd"));
    }

    #[test]
    fn test_split_at_end() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "xab" → "xa" (match "x", split "a" into "ab")
        assert!(automaton.accepts("xab", "xa"));

        // "testab" → "testa" (match "test", split "a" into "ab")
        assert!(automaton.accepts("testab", "testa"));
    }

    #[test]
    fn test_split_middle() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "ct" → "cct" (match 'c', then split: word 'c' matches input 'cc', then match 't')
        // Wait, that's wrong - we already consumed word 'c'. Let me use different chars.
        // "cat" → "caat" (match 'c', split: word 'a' matches input 'aa', match 't')
        assert!(automaton.accepts("cat", "caat"));
    }

    #[test]
    fn test_split_distance_zero() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(0, ops);

        // Distance 0: no split allowed
        assert!(automaton.accepts("test", "test"));
        assert!(!automaton.accepts("ttest", "test"));
        assert!(!automaton.accepts("ab", "a"));
    }

    #[test]
    fn test_split_with_standard_operations() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // Split + substitution
        // "test" → "txst" (split "t" into "te", substitute e→x, match "st")
        assert!(automaton.accepts("test", "txst"));

        // Split + deletion
        // "test" → "ttest" (split) → "test" (delete extra t)
        assert!(automaton.accepts("test", "test"));
    }

    #[test]
    fn test_split_empty_and_edge_cases() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Empty strings
        assert!(automaton.accepts("", ""));

        // Single character (split produces 2 chars from 1)
        assert!(automaton.accepts("a", "a"));
        assert!(automaton.accepts("ab", "a")); // Split "a" → "ab"

        // Two char word
        assert!(automaton.accepts("ab", "ab"));
        assert!(automaton.accepts("abb", "ab")); // Split "a" → "ab", match "b"
    }

    #[test]
    fn test_split_and_merge_combined() {
        let ops = crate::transducer::OperationSet::with_merge_split();
        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // Both split and merge in same string
        // "abc" → "ac" (split "a" → "ab") → "abc" (merge "bc" → "c")
        assert!(automaton.accepts("abc", "ac"));

        // Complex: "test" → "ttest" (split "t") → "test" (merge "tt" → "t")
        assert!(automaton.accepts("test", "test"));
    }

    // ============================================================================
    // Phase 2d.6: Integration Tests - Mixed Operations and Complex Cases
    // ============================================================================

    #[test]
    fn test_all_multichar_operations_combined() {
        // Create operation set with all multi-character operations
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        let automaton = GeneralizedAutomaton::with_operations(3, ops);

        // Combines transpose, merge, and split
        // "abc" → "abbc" (split 'b') → "acbc" (transpose "bb" → "cb")
        assert!(automaton.accepts("abc", "acbc"));

        // Complex transformation using all operation types
        // "hello" → "heello" (split 'e') → "hello" (merge "ee" → "e")
        assert!(automaton.accepts("hello", "hello"));
    }

    #[test]
    fn test_multichar_with_distance_constraints() {
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        // Distance 1: only one multi-char operation allowed
        let automaton1 = GeneralizedAutomaton::with_operations(1, ops.clone());
        assert!(automaton1.accepts("ab", "ba")); // transpose
        assert!(automaton1.accepts("a", "aa")); // split
        assert!(!automaton1.accepts("ab", "bba")); // would need transpose + split (2 errors)

        // Distance 2: two operations allowed
        let automaton2 = GeneralizedAutomaton::with_operations(2, ops.clone());
        assert!(automaton2.accepts("abc", "baca")); // transpose + split

        // Distance 3: three operations allowed
        let automaton3 = GeneralizedAutomaton::with_operations(3, ops);
        assert!(automaton3.accepts("abc", "bbcaa")); // multiple splits and transpose
    }

    #[test]
    fn test_multichar_operations_at_string_boundaries() {
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // Transpose at start
        assert!(automaton.accepts("ab", "ba"));

        // Transpose at end
        assert!(automaton.accepts("cab", "cba"));

        // Split at start
        assert!(automaton.accepts("abc", "aabc"));

        // Split at end
        assert!(automaton.accepts("abc", "abcc"));

        // Merge at start (input has merge at start)
        assert!(automaton.accepts("abc", "bc")); // delete or merge "ab" → "b" + keep c

        // Merge at end
        assert!(automaton.accepts("abc", "ab")); // delete 'c' or merge "bc" → "b"
    }

    #[test]
    fn test_repeated_multichar_operations() {
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        // Distance 2: two transposes
        let automaton2 = GeneralizedAutomaton::with_operations(2, ops.clone());
        assert!(automaton2.accepts("abcd", "badc")); // transpose "ab" and "cd"

        // Distance 2: two splits
        assert!(automaton2.accepts("ab", "aabb")); // split 'a' and 'b'

        // Distance 3: three operations
        let automaton3 = GeneralizedAutomaton::with_operations(3, ops);
        assert!(automaton3.accepts("abc", "aabbcc")); // split all three chars
    }

    #[test]
    fn test_multichar_with_standard_operations_complex() {
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        let automaton = GeneralizedAutomaton::with_operations(3, ops);

        // Transpose + insert + delete
        assert!(automaton.accepts("abc", "bac")); // transpose (1 error)
        assert!(automaton.accepts("abc", "bacd")); // transpose + insert (2 errors)
        assert!(automaton.accepts("abcd", "bac")); // transpose + delete (2 errors)

        // Split + substitute
        assert!(automaton.accepts("abc", "aabc")); // split 'a' (1 error)
        assert!(automaton.accepts("abc", "aabx")); // split 'a' + substitute c→x (2 errors)

        // Merge + insert
        assert!(automaton.accepts("abc", "bc")); // delete 'a' (1 error)
        assert!(automaton.accepts("abc", "bcd")); // delete 'a' + insert 'd' (2 errors)
    }

    #[test]
    fn test_multichar_edge_cases() {
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // Empty strings (already covered but let's be explicit)
        assert!(automaton.accepts("", ""));
        assert!(automaton.accepts("a", "")); // delete
        assert!(automaton.accepts("", "a")); // insert

        // Single character (no transpose possible)
        assert!(automaton.accepts("a", "a"));
        assert!(automaton.accepts("a", "aa")); // split

        // Two characters (minimal transpose)
        assert!(automaton.accepts("ab", "ba")); // transpose
        assert!(automaton.accepts("ab", "aab")); // split first
        assert!(automaton.accepts("ab", "abb")); // split second

        // Identical strings (no operations needed)
        assert!(automaton.accepts("test", "test"));
        assert!(automaton.accepts("hello", "hello"));
    }

    #[test]
    fn test_multichar_pathological_cases() {
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        let automaton = GeneralizedAutomaton::with_operations(5, ops);

        // All same character
        assert!(automaton.accepts("aaaa", "aaaa"));
        assert!(automaton.accepts("aaaa", "aaaaaaaa")); // split all (4 errors)
        assert!(automaton.accepts("aaaa", "aa")); // merge/delete (2 errors)

        // Alternating pattern
        assert!(automaton.accepts("abab", "baba")); // transpose pairs
        assert!(automaton.accepts("abab", "aabbab")); // split 'a's

        // Reversed string (requires multiple transposes)
        assert!(automaton.accepts("abc", "cba")); // transpose + transpose
    }

    #[test]
    fn test_multichar_operations_respect_invariants() {
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        // Distance 0: no operations allowed
        let automaton0 = GeneralizedAutomaton::with_operations(0, ops.clone());
        assert!(automaton0.accepts("test", "test"));
        assert!(!automaton0.accepts("test", "tset")); // no transpose
        assert!(!automaton0.accepts("test", "teest")); // no split
        assert!(!automaton0.accepts("test", "tes")); // no delete/merge

        // Distance 1: exactly one operation
        let automaton1 = GeneralizedAutomaton::with_operations(1, ops.clone());
        assert!(automaton1.accepts("test", "tset")); // transpose
        assert!(automaton1.accepts("test", "teest")); // split
        assert!(!automaton1.accepts("test", "tseest")); // transpose + split (2 errors)

        // Verify operations don't succeed beyond max distance
        assert!(!automaton1.accepts("abc", "aabbcc")); // would need 3 splits
    }

    #[test]
    fn test_multichar_subsumption_correctness() {
        // This test verifies that subsumption works correctly with intermediate states
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // These should all work and produce minimal state sets
        assert!(automaton.accepts("ab", "ba")); // transpose
        assert!(automaton.accepts("abc", "bac")); // transpose
        assert!(automaton.accepts("test", "tset")); // transpose

        // Verify split subsumption
        assert!(automaton.accepts("a", "aa")); // split
        assert!(automaton.accepts("ab", "aab")); // split first
        assert!(automaton.accepts("ab", "aabb")); // split both

        // Complex cases that test subsumption during multi-char operations
        assert!(automaton.accepts("abcd", "bacd")); // transpose at start
        assert!(automaton.accepts("abcd", "abdc")); // transpose at end
    }

    #[test]
    fn test_multichar_operation_ordering() {
        // Verify that different orderings of operations work correctly
        let ops = crate::transducer::OperationSetBuilder::new()
            .with_standard_ops()
            .with_transposition()
            .with_merge()
            .with_split()
            .build();

        let automaton = GeneralizedAutomaton::with_operations(3, ops);

        // transpose then split
        assert!(automaton.accepts("abc", "baac")); // transpose "ab", then split 'a'

        // split then transpose
        assert!(automaton.accepts("abc", "abba")); // split 'b', then transpose... wait, this doesn't make sense

        // Let me use clearer examples:
        // "ab" → "ba" (transpose) → "baa" (split second 'a')
        assert!(automaton.accepts("ab", "baa"));

        // "ab" → "aab" (split first 'a') → "aba" (transpose)
        // Actually, the input is fixed, so let me think differently

        // The point is: regardless of the order operations are discovered,
        // the same transformations should be accepted
        assert!(automaton.accepts("abc", "aabc")); // split or insert
        assert!(automaton.accepts("abc", "bac")); // transpose
        assert!(automaton.accepts("abc", "ab")); // delete
    }

    // ============================================================================
    // Phase 3: Phonetic Operations Tests
    // ============================================================================

    #[test]
    fn test_phonetic_debug_simple() {
        // Simple debug test for phonetic operations
        let ops = crate::transducer::phonetic::consonant_digraphs();

        // Check operation set has the right operations
        eprintln!("Operation set has {} operations", ops.operations().len());
        for op in ops.operations() {
            eprintln!(
                "  Operation: consume_x={}, consume_y={}, weight={}",
                op.consume_x(),
                op.consume_y(),
                op.weight()
            );
        }

        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Test simplest case: "ph" → "f"
        eprintln!("\n=== Testing 'ph' → 'f' ===");

        // Debug: check what relevant_subword looks like
        let subword = automaton.relevant_subword("ph", 1);
        eprintln!("Relevant subword at position 1: '{}'", subword);
        eprintln!("Subword chars: {:?}", subword.chars().collect::<Vec<_>>());

        let result = automaton.accepts("ph", "f");
        eprintln!("Result: {}", result);
        assert!(result, "Expected 'ph' → 'f' to be accepted");
    }

    #[test]
    fn test_phonetic_digraph_2to1_ch_to_k() {
        // Test phonetic operation: "ch" → "k" (⟨2,1,0.15⟩)
        // Need standard ops for matching non-phonetic characters
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "church" can match "kurk" via "ch"→"k" digraph operations
        assert!(automaton.accepts("church", "kurk"));

        // "chair" can match "kair" via "ch"→"k"
        assert!(automaton.accepts("chair", "kair"));

        // Distance 0: exact match only (with standard match operation)
        let ops0 = crate::transducer::OperationSet::default();
        let automaton0 = GeneralizedAutomaton::with_operations(0, ops0);
        assert!(automaton0.accepts("church", "church")); // exact match
        assert!(!automaton0.accepts("church", "kurk")); // requires phonetic ops
    }

    #[test]
    fn test_phonetic_digraph_2to1_ph_to_f() {
        // Test phonetic operation: "ph" → "f" (⟨2,1,0.15⟩)
        // Need standard ops for matching non-phonetic characters
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "phone" can match "fone" via "ph"→"f"
        assert!(automaton.accepts("phone", "fone"));

        // "graph" can match "graf" via "ph"→"f"
        assert!(automaton.accepts("graph", "graf"));
    }

    #[test]
    fn test_phonetic_digraph_2to1_sh_to_s() {
        // Test phonetic operation: "sh" → "s" (⟨2,1,0.15⟩)
        // Need standard ops for matching non-phonetic characters
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "ship" can match "sip" via "sh"→"s"
        assert!(automaton.accepts("ship", "sip"));

        // "wash" can match "was" via "sh"→"s"
        assert!(automaton.accepts("wash", "was"));
    }

    #[test]
    fn test_phonetic_digraph_2to1_th_to_t() {
        // Test phonetic operation: "th" → "t" (⟨2,1,0.15⟩)
        // Need standard ops for matching non-phonetic characters
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "think" can match "tink" via "th"→"t"
        assert!(automaton.accepts("think", "tink"));

        // "bath" can match "bat" via "th"→"t"
        assert!(automaton.accepts("bath", "bat"));
    }

    #[test]
    fn test_phonetic_digraph_multiple_in_word() {
        // Test multiple phonetic operations in same word
        // Need standard ops for matching non-phonetic characters
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // "church" has two "ch" digraphs, both can convert: "kurk"
        // But we only have distance 1 per "ch"→"k", so we need distance 2
        assert!(automaton.accepts("church", "kurc")); // one ch→k
        assert!(automaton.accepts("church", "churk")); // one ch→k
    }

    #[test]
    fn test_phonetic_with_standard_ops() {
        // Test phonetic operations combined with standard operations
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();

        // Add all phonetic operations
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();

        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // "phone" → "fone" (ph→f) + extra char
        assert!(automaton.accepts("phone", "fones")); // ph→f + insert 's'

        // "chair" → "kair" (ch→k) + substitute
        assert!(automaton.accepts("chair", "kair")); // ch→k
        assert!(automaton.accepts("chair", "kair")); // ch→k
    }

    #[test]
    fn test_phonetic_distance_constraints() {
        // Verify phonetic operations respect distance limits
        // Need standard ops for matching non-phonetic characters and deletions
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();

        // Distance 1: allows one phonetic operation
        let automaton1 = GeneralizedAutomaton::with_operations(1, ops.clone());
        assert!(automaton1.accepts("phone", "fone")); // one ph→f
        assert!(!automaton1.accepts("phone", "fo")); // would need ph→f + delete (2 ops)

        // Distance 2: allows two operations
        let automaton2 = GeneralizedAutomaton::with_operations(2, ops);
        assert!(automaton2.accepts("phone", "fo")); // ph→f + delete 'ne'
    }

    // ==================== Cross-Validation Tests ====================
    // Compare Generalized Automaton with Universal Automaton
    // to ensure correctness of phonetic operations

    #[test]
    fn test_cross_validate_standard_operations() {
        // Test that generalized automaton matches universal automaton
        // for standard Levenshtein operations
        use crate::transducer::universal::Standard;
        use crate::transducer::universal::UniversalAutomaton;

        let test_cases = vec![
            ("kitten", "sitting", 3, true),
            ("kitten", "sitting", 2, false),
            ("saturday", "sunday", 3, true),
            ("saturday", "sunday", 2, false),
            ("test", "test", 0, true),
            ("test", "tast", 1, true),
            ("", "", 0, true),
            ("a", "b", 1, true),
            ("abc", "def", 3, true),
        ];

        for (word, input, distance, expected) in test_cases {
            let gen_auto = GeneralizedAutomaton::new(distance);
            let univ_auto = UniversalAutomaton::<Standard>::new(distance);

            let gen_result = gen_auto.accepts(word, input);
            let univ_result = univ_auto.accepts(word, input);

            assert_eq!(
                gen_result, univ_result,
                "Mismatch for ('{}', '{}', {}): gen={}, univ={}",
                word, input, distance, gen_result, univ_result
            );
            assert_eq!(
                gen_result, expected,
                "Expected {} for ('{}', '{}', {}), got {}",
                expected, word, input, distance, gen_result
            );
        }
    }

    #[test]
    fn test_cross_validate_phonetic_merge_simple() {
        // Cross-validate phonetic merge operations
        // Generalized automaton should accept same strings as manual validation
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // These should all be accepted
        let accept_cases = vec![
            ("phone", "fone"),  // ph→f
            ("graph", "graf"),  // ph→f
            ("ship", "sip"),    // sh→s
            ("think", "tink"),  // th→t
            ("church", "kurc"), // first ch→k
            ("chair", "kair"),  // ch→k
        ];

        for (word, input) in accept_cases {
            assert!(
                automaton.accepts(word, input),
                "Should accept ('{}', '{}')",
                word,
                input
            );
        }

        // These should all be rejected (require > distance 1)
        let reject_cases = vec![
            ("phone", "fo"),   // ph→f + delete (needs distance 2)
            ("church", "urk"), // ch→k + delete (needs distance 2)
        ];

        for (word, input) in reject_cases {
            assert!(
                !automaton.accepts(word, input),
                "Should reject ('{}', '{}') at distance 1",
                word,
                input
            );
        }
    }

    #[test]
    fn test_cross_validate_fractional_weights() {
        // Verify fractional weights (0.15) are treated as "free" operations
        // Multiple phonetic operations should be possible at distance 1
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();

        // With distance 1, multiple fractional-weight operations should work
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "church" → "kurk" requires 2× ch→k operations
        // Each operation has weight 0.15, truncates to 0
        // Both should succeed at distance 1
        assert!(
            automaton.accepts("church", "kurk"),
            "Two phonetic operations (2×0.15=0 errors) should work at distance 1"
        );

        // "church" → "kurks" requires 2× ch→k + 1 insert = 1 standard error
        // Should still work at distance 1
        assert!(
            automaton.accepts("church", "kurks"),
            "Two phonetic + one standard operation (total 1 error) should work at distance 1"
        );

        // But two standard operations should fail
        assert!(
            !automaton.accepts("church", "korks"),
            "Two phonetic + two standard operations should fail at distance 1"
        );
    }

    // Phase 3b: Phonetic split ⟨1,2⟩ tests
    #[test]
    fn test_phonetic_split_k_to_ch() {
        // Test "k"→"ch" split operation
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "ark" → "arch" via k→ch split
        assert!(
            automaton.accepts("ark", "arch"),
            "Split k→ch should work at distance 1"
        );

        // "back" → "bach" via k→ch split
        assert!(
            automaton.accepts("back", "bach"),
            "Split k→ch at word end should work"
        );

        // "kan" → "chan" via k→ch split at start
        assert!(
            automaton.accepts("kan", "chan"),
            "Split k→ch at word start should work"
        );
    }

    #[test]
    fn test_phonetic_split_f_to_ph() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "graf" → "graph" via f→ph split
        assert!(
            automaton.accepts("graf", "graph"),
            "Split f→ph should work at distance 1"
        );

        // "foto" → "photo" via f→ph split at start
        assert!(
            automaton.accepts("foto", "photo"),
            "Split f→ph at word start should work"
        );
    }

    #[test]
    fn test_phonetic_split_s_to_sh() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "sip" → "ship" via s→sh split
        assert!(
            automaton.accepts("sip", "ship"),
            "Split s→sh should work at distance 1"
        );

        // "sell" → "shell" via s→sh split at start
        assert!(
            automaton.accepts("sell", "shell"),
            "Split s→sh at word start should work"
        );
    }

    #[test]
    fn test_phonetic_split_t_to_th() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "bat" → "bath" via t→th split
        assert!(
            automaton.accepts("bat", "bath"),
            "Split t→th should work at distance 1"
        );

        // "tin" → "thin" via t→th split at start
        assert!(
            automaton.accepts("tin", "thin"),
            "Split t→th at word start should work"
        );
    }

    #[test]
    fn test_phonetic_split_multiple() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();

        // Multiple splits require higher distance
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Single split at distance 1 (fractional weight = 0)
        assert!(
            automaton.accepts("kair", "chair"),
            "Single k→ch split should work at distance 1"
        );

        // But two splits need to check if fractional weights allow it
        // Each split has weight 0.15, both truncate to 0
        // So two splits should work at distance 1
        assert!(
            automaton.accepts("kat", "chath"),
            "Two splits (k→ch, t→th) with fractional weights should work at distance 1"
        );
    }

    #[test]
    fn test_phonetic_split_with_standard_ops() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(2, ops);

        // Split + standard operations
        // "graf" → "grape" via f→ph split (0 errors) + e→a substitute (1 error) = 1 total
        assert!(
            automaton.accepts("graf", "graphe"),
            "Split f→ph + insert 'e' should work at distance 1"
        );

        // Multiple standard operations
        // "bak" → "batch" via k→ch split (0) + insert 't' (1) = 1 total
        assert!(
            automaton.accepts("bak", "batch"),
            "Split k→ch + insert should work at distance 1"
        );
    }

    #[test]
    fn test_phonetic_split_distance_constraints() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(0, ops.clone());

        // At distance 0, no operations should work
        assert!(
            !automaton.accepts("ark", "arch"),
            "Split should not work at distance 0"
        );

        // At distance 1, fractional-weight split should work
        let automaton = GeneralizedAutomaton::with_operations(1, ops);
        assert!(
            automaton.accepts("ark", "arch"),
            "Split should work at distance 1"
        );
    }

    // Phase 3b: Phonetic transpose ⟨2,2⟩ tests
    #[test]
    fn test_phonetic_transpose_qu_to_kw() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "queen" → "kween" via qu→kw transpose
        assert!(
            automaton.accepts("queen", "kween"),
            "Transpose qu→kw should work at distance 1"
        );

        // "quick" → "kwick" via qu→kw transpose
        assert!(
            automaton.accepts("quick", "kwick"),
            "Transpose qu→kw at word start should work"
        );

        // "quit" → "kwit" via qu→kw transpose
        assert!(
            automaton.accepts("quit", "kwit"),
            "Transpose qu→kw should work"
        );
    }

    #[test]
    fn test_phonetic_transpose_kw_to_qu() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // "kween" → "queen" via kw→qu transpose (reverse)
        assert!(
            automaton.accepts("kween", "queen"),
            "Transpose kw→qu should work at distance 1"
        );

        // "kwik" → "quik" via kw→qu transpose
        assert!(
            automaton.accepts("kwik", "quik"),
            "Transpose kw→qu should work"
        );
    }

    #[test]
    fn test_phonetic_transpose_multiple() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();

        // Multiple transposes with fractional weights
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Single transpose at distance 1
        assert!(
            automaton.accepts("queen", "kween"),
            "Single transpose should work at distance 1"
        );

        // Two transposes would need multiple qu/kw in the word
        // "ququ" → "kwkw" (two transposes, both fractional = 0)
        assert!(
            automaton.accepts("ququ", "kwkw"),
            "Two transposes with fractional weights should work at distance 1"
        );
    }

    #[test]
    fn test_phonetic_transpose_with_standard_ops() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Transpose + standard operations
        // "queen" → "kweens" via qu→kw (0) + insert 's' (1) = 1 total
        assert!(
            automaton.accepts("queen", "kweens"),
            "Transpose + insert should work at distance 1"
        );
    }

    #[test]
    fn test_phonetic_transpose_distance_constraints() {
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(0, ops.clone());

        // At distance 0, no operations should work
        assert!(
            !automaton.accepts("queen", "kween"),
            "Transpose should not work at distance 0"
        );

        // At distance 1, fractional-weight transpose should work
        let automaton = GeneralizedAutomaton::with_operations(1, ops);
        assert!(
            automaton.accepts("queen", "kween"),
            "Transpose should work at distance 1"
        );
    }

    #[test]
    fn test_phonetic_mixed_merge_split_transpose() {
        // Test combining all phonetic operation types
        let phonetic_ops = crate::transducer::phonetic::consonant_digraphs();
        let mut builder = crate::transducer::OperationSetBuilder::new().with_standard_ops();
        for op in phonetic_ops.operations() {
            builder = builder.with_operation(op.clone());
        }
        let ops = builder.build();
        let automaton = GeneralizedAutomaton::with_operations(1, ops);

        // Merge: "phone" → "fone" (ph→f)
        assert!(
            automaton.accepts("phone", "fone"),
            "Merge operation should work"
        );

        // Split: "graf" → "graph" (f→ph)
        assert!(
            automaton.accepts("graf", "graph"),
            "Split operation should work"
        );

        // Transpose: "queen" → "kween" (qu→kw)
        assert!(
            automaton.accepts("queen", "kween"),
            "Transpose operation should work"
        );

        // All three have fractional weights, all work at distance 1
        // Each operation independently truncates to 0 errors
    }
}