euhadra 0.2.0

A programmable voice input framework — ASR, LLM refinement, and OS integration as composable adapters
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
use async_trait::async_trait;

use crate::types::ContextSnapshot;

// ---------------------------------------------------------------------------
// TextProcessor trait
// ---------------------------------------------------------------------------

/// Structural text correction applied between TextFilter and LlmRefiner.
///
/// Handles punctuation insertion, capitalization, self-correction detection,
/// and list formatting using lightweight models or rules — no LLM required.
///
/// Multiple processors can be chained.
#[async_trait]
pub trait TextProcessor: Send + Sync {
    async fn process(
        &self,
        text: &str,
        context: &ContextSnapshot,
    ) -> Result<ProcessResult, ProcessError>;
}

/// What a [`TextProcessor`] did to the text.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ProcessResult {
    /// The text after processing.
    pub text: String,
    /// The corrections that were applied, for diagnostics.
    pub corrections: Vec<Correction>,
}

/// One correction a [`TextProcessor`] made.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Correction {
    pub kind: CorrectionKind,
    pub original: String,
    pub replacement: String,
}

/// The kind of change a [`Correction`] represents.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CorrectionKind {
    PunctuationInserted,
    Capitalized,
    SelfCorrectionRemoved,
    ListFormatted,
    DictionaryMatch,
    NumeralNormalized,
    SpokenFormNormalized,
    /// A named entity was recognised. Metadata only — the processor
    /// that reports this leaves the text unchanged. `original` holds the
    /// entity surface and `replacement` repeats it, since nothing was
    /// replaced. The structured form, with offsets and a typed label,
    /// is `onnx_processing::Entity` (`onnx` feature).
    EntityDetected,
}

/// Why a [`TextProcessor`] could not process its input.
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum ProcessError {
    /// A model or data file the processor needs could not be loaded.
    #[error("processor resource unavailable: {0}")]
    Unavailable(String),

    /// Inference failed inside the processor.
    #[error("processor inference failed: {0}")]
    Inference(String),

    /// The processor ran but could not complete.
    #[error("processor failed: {0}")]
    Failed(String),
}

// ---------------------------------------------------------------------------
// SelfCorrectionDetector
// ---------------------------------------------------------------------------

/// Detects and removes self-corrections (disfluency repairs) in ASR output.
///
/// Pattern: "I want to go to Boston, to Denver" → "I want to go to Denver"
///
/// Uses a sliding window to find repeated structural patterns (reparandum →
/// repair), where the repair "overwrites" the reparandum.  Detection is based
/// on the observation that self-corrections typically produce a "rough copy":
/// the repair reuses the same or similar words in roughly the same order.
pub struct SelfCorrectionDetector {
    /// Minimum number of shared words between reparandum and repair to trigger.
    min_shared_words: usize,
    /// Correction cue words that signal a self-correction boundary.
    correction_cues_en: Vec<String>,
    correction_cues_ja: Vec<String>,
    correction_cues_es: Vec<String>,
    correction_cues_zh: Vec<String>,
    correction_cues_ko: Vec<String>,
}

impl SelfCorrectionDetector {
    pub fn new() -> Self {
        // Cues are sorted longest-first because the detector uses
        // `str::find(cue)` and a shorter cue that is a substring of a
        // longer one would otherwise win. Concretely: without this
        // sort, "ていうか" (4 chars) matches before "っていうか" (5 chars)
        // inside "鈴木課長、っていうか佐藤課長です", causing the detector
        // to treat just "っ" as the reparandum instead of the full
        // "鈴木課長" preceding the cue. Same hazard exists for en
        // ("no" / "no wait", "rather" / "or rather").
        let mut correction_cues_en: Vec<String> = vec![
            "no",
            "wait",
            "sorry",
            "i mean",
            "actually",
            "rather",
            "no wait",
            "or rather",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        correction_cues_en.sort_by_key(|c| std::cmp::Reverse(c.chars().count()));

        let mut correction_cues_ja: Vec<String> = vec![
            "いや",
            "じゃなくて",
            "じゃなく",
            "ではなく",
            "ていうか",
            "っていうか",
            "じゃない",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        correction_cues_ja.sort_by_key(|c| std::cmp::Reverse(c.chars().count()));

        // Spanish closed-set cues. Bare `no` is the most ambiguous
        // (clashes with the negation use of the same word) so we
        // require a sentence-internal comma context for it (handled
        // in `detect_spanish`); the rest are unambiguous markers
        // attested in the Val.Es.Co + CSJ-style disfluency literature.
        // Sorted longest-first: `mejor dicho` must outrank `mejor`,
        // `quiero decir` outrank `digo`, `no es` outrank `no`.
        let mut correction_cues_es: Vec<String> = vec![
            "mejor dicho",
            "quiero decir",
            "o sea",
            "perdón",
            "mejor",
            "digo",
            "no es",
            "no",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        correction_cues_es.sort_by_key(|c| std::cmp::Reverse(c.chars().count()));

        // Chinese cues — tokenisation by 、 / , (no whitespace
        // between words), parallel to Japanese. Sorted longest-first
        // so 我的意思是 outranks 我是说 inside utterances that contain
        // both fragments.
        let mut correction_cues_zh: Vec<String> = vec![
            "我的意思是",
            "确切地说",
            "应该说",
            "我是说",
            "不对",
            "不是",
            "算了",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        correction_cues_zh.sort_by_key(|c| std::cmp::Reverse(c.chars().count()));

        // Korean cues — eojeol-tokenised (whitespace) like en / es.
        // Sorted longest-first so multi-eojeol cues (그게 아니라,
        // 잘못 말했네) outrank their shorter prefixes (아니, 그게).
        // The `아니` family overlaps with sentence-final negation
        // particles (X-아니에요 = "is not X"); the word-boundary
        // check in detect_korean handles that disambiguation.
        let mut correction_cues_ko: Vec<String> = vec![
            "그게 아니라",
            "그게 아니고",
            "잘못 말했다",
            "잘못 말했네",
            "아 잠깐",
            "잠깐만",
            "아니에요",
            "아니라",
            "아니야",
            "아니",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        correction_cues_ko.sort_by_key(|c| std::cmp::Reverse(c.chars().count()));

        Self {
            min_shared_words: 1,
            correction_cues_en,
            correction_cues_ja,
            correction_cues_es,
            correction_cues_zh,
            correction_cues_ko,
        }
    }

    /// Find self-correction patterns in English text.
    ///
    /// Looks for correction cues and removes the reparandum (the part before
    /// the cue that the speaker intended to discard).
    fn detect_english(&self, text: &str) -> Option<(String, Correction)> {
        let lower = text.to_lowercase();

        for cue in &self.correction_cues_en {
            if let Some(cue_pos) = lower.find(cue.as_str()) {
                let before_cue = &text[..cue_pos].trim_end();
                let after_cue = &text[cue_pos + cue.len()..].trim_start();

                if after_cue.is_empty() {
                    continue;
                }

                // Find the overlap: walk backwards from the cue to find where
                // the reparandum starts.  We look for the point where the
                // words before the cue start repeating structure found after it.
                let before_words: Vec<&str> = before_cue.split_whitespace().collect();
                let after_words: Vec<&str> = after_cue.split_whitespace().collect();

                if after_words.is_empty() {
                    continue;
                }

                // Find how many trailing words of `before` share a common
                // prefix with `after` (the "rough copy" pattern).
                let shared = Self::count_shared_prefix_from_end(&before_words, &after_words);

                if shared >= self.min_shared_words {
                    // Remove the reparandum: keep words before the shared
                    // prefix, then append the repair (after cue).
                    let keep_count = before_words.len() - shared;
                    let kept: Vec<&str> = before_words[..keep_count].to_vec();
                    let result = if kept.is_empty() {
                        after_cue.to_string()
                    } else {
                        format!("{} {}", kept.join(" "), after_cue)
                    };

                    let original_rm = before_words[keep_count..].join(" ");
                    return Some((
                        result,
                        Correction {
                            kind: CorrectionKind::SelfCorrectionRemoved,
                            original: format!("{} {}", original_rm, cue),
                            replacement: String::new(),
                        },
                    ));
                }
            }
        }
        None
    }

    /// Find self-correction patterns in Japanese text.
    fn detect_japanese(&self, text: &str) -> Option<(String, Correction)> {
        for cue in &self.correction_cues_ja {
            if let Some(cue_pos) = text.find(cue.as_str()) {
                let before = text[..cue_pos].trim_end_matches('').trim_end();
                let after = text[cue_pos + cue.len()..]
                    .trim_start_matches('')
                    .trim_start();

                if after.is_empty() || before.is_empty() {
                    continue;
                }

                // In Japanese, self-corrections often have the form:
                // "AAAいやBBB" where BBB replaces AAA.
                // We find the last comma-delimited segment before the cue
                // and remove it, keeping everything before and the repair after.
                let segments: Vec<&str> = before.split('').collect();
                if segments.is_empty() {
                    continue;
                }

                let reparandum = segments.last().unwrap().trim();
                let kept_before = if segments.len() > 1 {
                    segments[..segments.len() - 1].join("")
                } else {
                    String::new()
                };

                let result = if kept_before.is_empty() {
                    after.to_string()
                } else {
                    format!("{}{}", kept_before, after)
                };

                return Some((
                    result,
                    Correction {
                        kind: CorrectionKind::SelfCorrectionRemoved,
                        original: format!("{}{}", reparandum, cue),
                        replacement: String::new(),
                    },
                ));
            }
        }
        None
    }

    /// Find self-correction patterns in Spanish text.
    ///
    /// Spanish disfluencies follow the same `<reparandum> <cue>
    /// <repair>` structure as English (whitespace tokenisation,
    /// shared-word overlap between the trailing edge of the
    /// reparandum and the leading edge of the repair). Punctuation
    /// around the cue (commas, periods) is stripped before the
    /// shared-prefix check so utterances like "voy mañana, no, voy
    /// hoy" hit the same path as "voy mañana no voy hoy".
    ///
    /// Bare `no` is intentionally treated as a correction cue here
    /// even though Spanish uses the same word for negation; the
    /// shared-prefix requirement (`min_shared_words = 1`) keeps
    /// pure negations like "el gato no come pescado" un-flagged
    /// (no overlapping content word follows the cue).
    fn detect_spanish(&self, text: &str) -> Option<(String, Correction)> {
        let lower = text.to_lowercase();

        for cue in &self.correction_cues_es {
            // Only match the cue as a standalone word — without
            // boundary checks, "no" would fire inside "no es" or
            // tokens like "Mariano" / "minoría". Find with a sliding
            // search so each candidate position can be vetted.
            let mut from = 0usize;
            while let Some(rel) = lower[from..].find(cue.as_str()) {
                let cue_pos = from + rel;
                let cue_end = cue_pos + cue.len();

                // Standalone-word boundary: the chars on each side
                // must be either out-of-bounds or a non-alphabetic
                // ASCII delimiter (whitespace, comma, period, etc.).
                let left_ok = cue_pos == 0
                    || !lower
                        .as_bytes()
                        .get(cue_pos - 1)
                        .map(|b| b.is_ascii_alphabetic())
                        .unwrap_or(false);
                let right_ok = cue_end >= lower.len()
                    || !lower
                        .as_bytes()
                        .get(cue_end)
                        .map(|b| b.is_ascii_alphabetic())
                        .unwrap_or(false);
                if !(left_ok && right_ok) {
                    from = cue_pos + cue.chars().next().map_or(1, |c| c.len_utf8());
                    continue;
                }

                // Strip surrounding punctuation + whitespace so the
                // shared-prefix check operates on word tokens only.
                let trim_chars: &[char] = &[',', '.', ';', ':', '!', '?', ' ', '\t'];
                let before_cue = text[..cue_pos].trim_end_matches(trim_chars);
                let after_cue = text[cue_end..].trim_start_matches(trim_chars);

                if after_cue.is_empty() || before_cue.is_empty() {
                    from = cue_end;
                    continue;
                }

                let before_words: Vec<&str> = before_cue.split_whitespace().collect();
                let after_words: Vec<&str> = after_cue.split_whitespace().collect();
                if after_words.is_empty() || before_words.is_empty() {
                    from = cue_end;
                    continue;
                }

                let shared = Self::count_shared_prefix_from_end(&before_words, &after_words);

                if shared >= self.min_shared_words {
                    let keep_count = before_words.len() - shared;
                    let kept: Vec<&str> = before_words[..keep_count].to_vec();
                    let result = if kept.is_empty() {
                        after_cue.to_string()
                    } else {
                        format!("{} {}", kept.join(" "), after_cue)
                    };
                    let original_rm = before_words[keep_count..].join(" ");
                    return Some((
                        result,
                        Correction {
                            kind: CorrectionKind::SelfCorrectionRemoved,
                            original: format!("{} {}", original_rm, cue),
                            replacement: String::new(),
                        },
                    ));
                }
                from = cue_end;
            }
        }
        None
    }

    /// Find self-correction patterns in Chinese text.
    ///
    /// Chinese, like Japanese, lacks inter-word whitespace and
    /// commonly uses 、 / , as clause separators in ASR output.
    /// Detection mirrors `detect_japanese`: locate the cue, take
    /// the last comma-segment of the pre-cue text as the
    /// reparandum, and emit `<surviving prefix>,<repair>`.
    fn detect_chinese(&self, text: &str) -> Option<(String, Correction)> {
        for cue in &self.correction_cues_zh {
            if let Some(cue_pos) = text.find(cue.as_str()) {
                let trim_clause: &[char] = &['', '', ',', ' '];
                let before = text[..cue_pos].trim_end_matches(trim_clause).trim_end();
                let after = text[cue_pos + cue.len()..]
                    .trim_start_matches(trim_clause)
                    .trim_start();

                if after.is_empty() || before.is_empty() {
                    continue;
                }

                // Split before-text on either Chinese clause comma,
                // since utterance ASR may emit either form.
                let segments: Vec<&str> = before.split(['', '']).collect();
                if segments.is_empty() {
                    continue;
                }

                let reparandum = segments.last().unwrap().trim();
                let kept_before = if segments.len() > 1 {
                    segments[..segments.len() - 1].join("")
                } else {
                    String::new()
                };

                let result = if kept_before.is_empty() {
                    after.to_string()
                } else {
                    format!("{}{}", kept_before, after)
                };

                return Some((
                    result,
                    Correction {
                        kind: CorrectionKind::SelfCorrectionRemoved,
                        original: format!("{}{}", reparandum, cue),
                        replacement: String::new(),
                    },
                ));
            }
        }
        None
    }

    /// Find self-correction patterns in Korean text.
    ///
    /// Korean ASR output is eojeol-tokenised (whitespace-separated)
    /// but rarely carries inter-clause commas, so the en / es
    /// shared-prefix overlap heuristic only catches a fraction of
    /// the natural correction patterns. The closer fit is the
    /// Japanese comma-segmentation strategy: locate the cue, take
    /// the last `,` / `.` / `?` / `!` segment of the pre-cue text
    /// as the reparandum (the whole pre-cue text when there are no
    /// commas — the typical FLEURS-ko / SenseVoice case), and emit
    /// `<surviving prefix>, <repair>`.
    ///
    /// Eojeol-boundary check on the cue prevents `아니` from firing
    /// inside `아니에요` (sentence-final negation predicate) or
    /// other Hangul compounds.
    fn detect_korean(&self, text: &str) -> Option<(String, Correction)> {
        for cue in &self.correction_cues_ko {
            let mut from = 0usize;
            while let Some(rel) = text[from..].find(cue.as_str()) {
                let cue_pos = from + rel;
                let cue_end = cue_pos + cue.len();

                // Eojeol boundary check: chars on each side must be
                // whitespace, punctuation, or out-of-bounds. A Hangul
                // / alphanumeric char on either side means we are
                // inside a larger word.
                let left_ok = cue_pos == 0
                    || text[..cue_pos]
                        .chars()
                        .last()
                        .map(|c| !c.is_alphanumeric())
                        .unwrap_or(true);
                let right_ok = cue_end == text.len()
                    || text[cue_end..]
                        .chars()
                        .next()
                        .map(|c| !c.is_alphanumeric())
                        .unwrap_or(true);
                if !(left_ok && right_ok) {
                    from = cue_pos + cue.chars().next().map_or(1, |c| c.len_utf8());
                    continue;
                }

                let trim_chars: &[char] = &[',', '.', ';', ':', '!', '?', ' ', '\t'];
                let before_cue = text[..cue_pos].trim_end_matches(trim_chars);
                let after_cue = text[cue_end..].trim_start_matches(trim_chars);

                if after_cue.is_empty() || before_cue.is_empty() {
                    from = cue_end;
                    continue;
                }

                // Mirror detect_japanese: split the pre-cue text on
                // sentence-internal punctuation, treat the last
                // segment as the reparandum, keep the rest. For
                // FLEURS-ko / SenseVoice utterances (no internal
                // commas) the whole pre-cue is one segment → drop
                // it entirely → output is just the repair.
                let segments: Vec<&str> = before_cue.split([',', '.', '!', '?', ';']).collect();
                if segments.is_empty() {
                    from = cue_end;
                    continue;
                }

                let reparandum = segments.last().unwrap().trim();
                let kept_before = if segments.len() > 1 {
                    segments[..segments.len() - 1].join(",")
                } else {
                    String::new()
                };

                let result = if kept_before.is_empty() {
                    after_cue.to_string()
                } else {
                    format!("{}, {}", kept_before.trim_end(), after_cue)
                };

                return Some((
                    result,
                    Correction {
                        kind: CorrectionKind::SelfCorrectionRemoved,
                        original: format!("{} {}", reparandum, cue),
                        replacement: String::new(),
                    },
                ));
            }
        }
        None
    }

    /// Count how many words at the end of `before` match the beginning of
    /// `after` when lowercased.
    fn count_shared_prefix_from_end(before: &[&str], after: &[&str]) -> usize {
        let mut count = 0;
        let max_check = before.len().min(after.len()).min(5); // limit search window

        for offset in 1..=max_check {
            let b_idx = before.len() - offset;
            if before[b_idx].to_lowercase() == after[0].to_lowercase() {
                // Check if subsequent words also match
                let mut matches = 1;
                for j in 1..offset.min(after.len()) {
                    if before[b_idx + j].to_lowercase() == after[j].to_lowercase() {
                        matches += 1;
                    } else {
                        break;
                    }
                }
                if matches >= 1 {
                    count = count.max(offset);
                }
            }
        }
        count
    }
}

impl Default for SelfCorrectionDetector {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl TextProcessor for SelfCorrectionDetector {
    async fn process(
        &self,
        text: &str,
        _context: &ContextSnapshot,
    ) -> Result<ProcessResult, ProcessError> {
        // Order matters when a cue word exists in two languages.
        // We try the most specific / least-ambiguous detectors first:
        //   1. Korean — Hangul syllables, no Latin overlap, eojeol
        //      tokenisation. Cues (아니, 그게 아니라, 잠깐만, 잘못
        //      말했다) don't appear in any other language we cover.
        //   2. Chinese — Hanzi, comma-segmented. Cues (我是说, 不对,
        //      我的意思是) are clean of Hangul / Latin overlap.
        //   3. Japanese — Hiragana / Kanji, comma-segmented like zh
        //      but with a different cue set (いや, ていうか).
        //   4. Spanish — Latin, restricted closed-class cues
        //      (perdón, digo, mejor dicho). Bare `no` overlaps with
        //      English; the Spanish detector's stricter shared-prefix
        //      check beats the English detector for genuinely
        //      Spanish input.
        //   5. English — fallback, widest cue set, runs last.
        if let Some((result, correction)) = self.detect_korean(text) {
            return Ok(ProcessResult {
                text: result,
                corrections: vec![correction],
            });
        }
        if let Some((result, correction)) = self.detect_chinese(text) {
            return Ok(ProcessResult {
                text: result,
                corrections: vec![correction],
            });
        }
        if let Some((result, correction)) = self.detect_japanese(text) {
            return Ok(ProcessResult {
                text: result,
                corrections: vec![correction],
            });
        }
        if let Some((result, correction)) = self.detect_spanish(text) {
            return Ok(ProcessResult {
                text: result,
                corrections: vec![correction],
            });
        }
        if let Some((result, correction)) = self.detect_english(text) {
            return Ok(ProcessResult {
                text: result,
                corrections: vec![correction],
            });
        }

        Ok(ProcessResult {
            text: text.to_string(),
            corrections: vec![],
        })
    }
}

// ---------------------------------------------------------------------------
// BasicPunctuationRestorer — rule-based heuristic
// ---------------------------------------------------------------------------

/// Lightweight rule-based punctuation insertion.
///
/// This is a stopgap until a proper CNN-BiLSTM ONNX model is integrated.
/// It handles the most common cases:
/// - Capitalize first word of text (Latin scripts only)
/// - Ensure text ends with sentence-final punctuation
/// - Capitalize after sentence-ending punctuation (Latin scripts)
///
/// Terminal punctuation choice is script-aware:
/// - Latin (en / es / mixed): trailing `.`
/// - Hangul (ko): trailing `.` (Korean conventionally uses the
///   Western period in formal writing)
/// - Hanzi (zh): trailing `。` (CJK fullwidth period)
/// - Hiragana / Katakana / Kanji-only (ja): trailing `。`
pub struct BasicPunctuationRestorer;

/// Dominant-script classification for terminal-punctuation choice.
/// Determined by counting characters in each Unicode block; the
/// thresholds are deliberately permissive so a pure-Hangul utterance
/// with one stray English brand name still classifies as Hangul.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DominantScript {
    Latin,
    Hangul,
    Hanzi,
    Kana,
    Other,
}

fn classify_script(text: &str) -> DominantScript {
    let mut latin = 0usize;
    let mut hangul = 0usize;
    let mut hanzi = 0usize;
    let mut kana = 0usize;
    for c in text.chars() {
        if c.is_ascii_alphabetic() {
            latin += 1;
        } else if matches!(c as u32, 0xAC00..=0xD7A3 | 0x1100..=0x11FF | 0x3130..=0x318F) {
            hangul += 1;
        } else if matches!(c as u32, 0x4E00..=0x9FFF | 0x3400..=0x4DBF) {
            hanzi += 1;
        } else if matches!(c as u32, 0x3040..=0x30FF) {
            kana += 1;
        }
    }
    // Hiragana / Katakana presence dominates over Hanzi (since
    // pure-Hanzi utterances have zero kana and Japanese always
    // mixes the two). For Korean, Hangul presence dominates.
    if kana > 0 && kana + hanzi >= latin && kana + hanzi >= hangul {
        return DominantScript::Kana;
    }
    if hangul > latin && hangul > hanzi {
        return DominantScript::Hangul;
    }
    if hanzi > latin && hanzi > hangul {
        return DominantScript::Hanzi;
    }
    if latin > 0 {
        return DominantScript::Latin;
    }
    DominantScript::Other
}

#[async_trait]
impl TextProcessor for BasicPunctuationRestorer {
    async fn process(
        &self,
        text: &str,
        _context: &ContextSnapshot,
    ) -> Result<ProcessResult, ProcessError> {
        if text.is_empty() {
            return Ok(ProcessResult {
                text: String::new(),
                corrections: vec![],
            });
        }

        let script = classify_script(text);
        let capitalise = matches!(script, DominantScript::Latin);

        let mut result = String::with_capacity(text.len() + 8);
        let mut corrections = Vec::new();
        let mut capitalize_next = capitalise;

        for ch in text.chars() {
            if capitalize_next && ch.is_alphabetic() {
                let upper: String = ch.to_uppercase().collect();
                if upper != ch.to_string() {
                    corrections.push(Correction {
                        kind: CorrectionKind::Capitalized,
                        original: ch.to_string(),
                        replacement: upper.clone(),
                    });
                }
                result.push_str(&upper);
                capitalize_next = false;
            } else {
                result.push(ch);
                if capitalise && (ch == '.' || ch == '!' || ch == '?') {
                    capitalize_next = true;
                } else if ch == '' {
                    // Japanese / Chinese sentence-ending — next sentence starts
                    capitalize_next = false; // no capitalization in CJK
                }
            }
        }

        // Ensure terminal punctuation, picking the right form for
        // the dominant script. Skip Other (mixed / non-script-class
        // text — e.g. pure digits or punctuation-only) since we
        // don't know what to append.
        let trimmed = result.trim_end();
        let last_char = trimmed.chars().last();
        let want_terminal = match script {
            DominantScript::Latin | DominantScript::Hangul => Some('.'),
            DominantScript::Hanzi | DominantScript::Kana => Some(''),
            DominantScript::Other => None,
        };
        if let Some(terminal) = want_terminal {
            if let Some(last) = last_char {
                let already_terminated = match terminal {
                    '.' => matches!(last, '.' | '!' | '?' | ')' | '"' | '\'' | ''),
                    '' => matches!(last, '' | '' | '' | '.' | '!' | '?'),
                    _ => true,
                };
                if !already_terminated {
                    result = format!("{}{}", trimmed, terminal);
                    corrections.push(Correction {
                        kind: CorrectionKind::PunctuationInserted,
                        original: String::new(),
                        replacement: terminal.to_string(),
                    });
                }
            }
        }

        Ok(ProcessResult {
            text: result,
            corrections,
        })
    }
}

// ---------------------------------------------------------------------------
// InverseTextNormalizer
// ---------------------------------------------------------------------------

/// Inverse text normalization: rewrites spoken-form numerals into
/// written form ("twenty five" → "25", "二十五" → "25") using the
/// `text-processing-rs` crate. A non-LLM Tier-2 processor.
///
/// Language support tracks the upstream crate's *sentence-level* ITN
/// API, which today covers only en / ja / zh:
///
/// - `en` — `normalize_sentence` (English sentence scanner).
/// - `ja` / `zh` — `normalize_with_lang` (CJK in-place span scanner).
/// - everything else, incl. `es` and `ko` — pass through unchanged.
///   `es` has no sentence-level entry point upstream yet, and `ko` is
///   a pending upstream PR (FluidInference/text-processing-rs). When
///   either lands, add its arm to [`InverseTextNormalizer::new`].
pub struct InverseTextNormalizer {
    backend: ItnBackend,
}

enum ItnBackend {
    /// English sentence scanner (`normalize_sentence`).
    EnglishSentence,
    /// CJK sentence scanner (`normalize_with_lang`) for the given code.
    Lang(&'static str),
    /// Language not yet supported upstream — pass through unchanged.
    Passthrough,
}

impl InverseTextNormalizer {
    /// Build a normalizer for `lang` (a BCP-47-ish code like "en").
    /// Unsupported languages produce a passthrough normalizer.
    pub fn new(lang: &str) -> Self {
        let backend = match lang {
            "en" => ItnBackend::EnglishSentence,
            "ja" => ItnBackend::Lang("ja"),
            "zh" => ItnBackend::Lang("zh"),
            _ => ItnBackend::Passthrough,
        };
        Self { backend }
    }
}

#[async_trait]
impl TextProcessor for InverseTextNormalizer {
    async fn process(
        &self,
        text: &str,
        _context: &ContextSnapshot,
    ) -> Result<ProcessResult, ProcessError> {
        let normalized = match self.backend {
            ItnBackend::EnglishSentence => text_processing_rs::normalize_sentence(text),
            ItnBackend::Lang(lang) => text_processing_rs::normalize_with_lang(text, lang),
            ItnBackend::Passthrough => {
                return Ok(ProcessResult {
                    text: text.to_string(),
                    corrections: vec![],
                });
            }
        };

        let corrections = if normalized != text {
            vec![Correction {
                kind: CorrectionKind::NumeralNormalized,
                original: text.to_string(),
                replacement: normalized.clone(),
            }]
        } else {
            vec![]
        };

        Ok(ProcessResult {
            text: normalized,
            corrections,
        })
    }
}

// ---------------------------------------------------------------------------
// SpokenFormNormalizer
// ---------------------------------------------------------------------------

/// Rewrites colloquial spoken-form reductions into their written form
/// ("gonna" → "going to", "lemme" → "let me"). A non-LLM Tier-2
/// processor (issue #72).
///
/// This is a rule-based stopgap — a curated dictionary of unambiguous
/// one-to-one reductions — pending an edit-tagging ONNX model that can
/// resolve context-dependent cases. Only English has a rule set today;
/// other languages pass through unchanged. To add a language, write a
/// `<lang>_spoken_form` lookup and wire it in [`SpokenFormNormalizer::new`].
///
/// The dictionary is deliberately conservative: entries like "gonna" /
/// "wanna" technically also have a "going a" / "want a" reading, but
/// the "going to" / "want to" sense dominates spoken usage, so the
/// dominant mapping is applied. Genuinely ambiguous reductions
/// ("ain't", bare "cause", "gotcha") are left out.
pub struct SpokenFormNormalizer {
    lookup: Option<fn(&str) -> Option<&'static str>>,
}

impl SpokenFormNormalizer {
    /// Build a normalizer for `lang`. Unsupported languages produce a
    /// passthrough normalizer.
    pub fn new(lang: &str) -> Self {
        let lookup = match lang {
            "en" => Some(en_spoken_form as fn(&str) -> Option<&'static str>),
            _ => None,
        };
        Self { lookup }
    }
}

/// English colloquial reductions → written form. Conservative,
/// one-to-one, unambiguous-in-the-dominant-sense entries only.
fn en_spoken_form(token: &str) -> Option<&'static str> {
    match token {
        "gonna" => Some("going to"),
        "wanna" => Some("want to"),
        "gotta" => Some("got to"),
        "hafta" => Some("have to"),
        "oughta" => Some("ought to"),
        "tryna" => Some("trying to"),
        "gimme" => Some("give me"),
        "lemme" => Some("let me"),
        "kinda" => Some("kind of"),
        "sorta" => Some("sort of"),
        "outta" => Some("out of"),
        "lotta" => Some("lot of"),
        "dunno" => Some("don't know"),
        "c'mon" => Some("come on"),
        "'cause" => Some("because"),
        "cuz" => Some("because"),
        "y'all" => Some("you all"),
        _ => None,
    }
}

/// Apply the leading-capitalization of `original` to `replacement`.
fn match_leading_case(original: &str, replacement: &str) -> String {
    if original.chars().next().is_some_and(|c| c.is_uppercase()) {
        let mut chars = replacement.chars();
        match chars.next() {
            Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
            None => String::new(),
        }
    } else {
        replacement.to_string()
    }
}

#[async_trait]
impl TextProcessor for SpokenFormNormalizer {
    async fn process(
        &self,
        text: &str,
        _context: &ContextSnapshot,
    ) -> Result<ProcessResult, ProcessError> {
        let Some(lookup) = self.lookup else {
            return Ok(ProcessResult {
                text: text.to_string(),
                corrections: vec![],
            });
        };

        let mut out: Vec<String> = Vec::new();
        let mut corrections = Vec::new();

        for token in text.split_whitespace() {
            // Split a trailing run of sentence punctuation so "gonna."
            // still matches; a leading apostrophe is kept (it is part
            // of words like "'cause").
            let trail_start = token
                .char_indices()
                .rev()
                .take_while(|(_, c)| matches!(c, '.' | ',' | '!' | '?' | ';' | ':'))
                .last()
                .map(|(i, _)| i)
                .unwrap_or(token.len());
            let (core, trailing) = token.split_at(trail_start);

            let lower = core.to_lowercase();
            match lookup(&lower) {
                Some(replacement) if !core.is_empty() => {
                    let cased = match_leading_case(core, replacement);
                    corrections.push(Correction {
                        kind: CorrectionKind::SpokenFormNormalized,
                        original: core.to_string(),
                        replacement: cased.clone(),
                    });
                    out.push(format!("{}{}", cased, trailing));
                }
                _ => out.push(token.to_string()),
            }
        }

        if corrections.is_empty() {
            // Nothing matched — return the input untouched so clean text
            // is byte-identical (no whitespace re-normalization).
            return Ok(ProcessResult {
                text: text.to_string(),
                corrections,
            });
        }

        Ok(ProcessResult {
            text: out.join(" "),
            corrections,
        })
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn empty_context() -> ContextSnapshot {
        ContextSnapshot::default()
    }

    // --- SelfCorrectionDetector tests ---

    #[tokio::test]
    async fn self_correction_with_no_wait() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("I want to go to Boston no wait to Denver", &empty_context())
            .await
            .unwrap();
        assert!(
            result.text.contains("Denver"),
            "repair should be kept: {}",
            result.text
        );
        assert!(
            !result.text.contains("Boston"),
            "reparandum should be removed: {}",
            result.text
        );
        assert_eq!(result.corrections.len(), 1);
        assert_eq!(
            result.corrections[0].kind,
            CorrectionKind::SelfCorrectionRemoved
        );
    }

    #[tokio::test]
    async fn self_correction_with_i_mean() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process(
                "the meeting is tomorrow i mean the meeting is on Friday",
                &empty_context(),
            )
            .await
            .unwrap();
        assert!(
            result.text.contains("Friday"),
            "repair should be kept: {}",
            result.text
        );
    }

    #[tokio::test]
    async fn no_self_correction_in_clean_text() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process(
                "I want to go to Denver for the conference",
                &empty_context(),
            )
            .await
            .unwrap();
        assert_eq!(result.text, "I want to go to Denver for the conference");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn japanese_self_correction() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("明日、いや明後日に会議があります", &empty_context())
            .await
            .unwrap();
        assert!(
            result.text.contains("明後日"),
            "repair should be kept: {}",
            result.text
        );
        assert!(
            !result.text.contains("明日"),
            "reparandum should be removed: {}",
            result.text
        );
    }

    #[tokio::test]
    async fn japanese_self_correction_with_janakute() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("東京駅、じゃなくて品川駅で待ち合わせ", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("品川駅"), "repair: {}", result.text);
        assert!(
            !result.text.contains("東京駅"),
            "reparandum: {}",
            result.text
        );
    }

    // --- SelfCorrectionDetector — Spanish ---

    #[tokio::test]
    async fn spanish_self_correction_with_no() {
        // Bare `no` as a correction cue with shared content word.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("voy mañana no voy hoy", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("hoy"), "repair: {}", result.text);
        assert!(
            !result.text.contains("mañana"),
            "reparandum: {}",
            result.text
        );
        assert_eq!(result.corrections.len(), 1);
    }

    #[tokio::test]
    async fn spanish_self_correction_with_perdon() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("voy a Madrid perdón a Barcelona", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("Barcelona"), "repair: {}", result.text);
        assert!(
            !result.text.contains("Madrid"),
            "reparandum: {}",
            result.text
        );
    }

    #[tokio::test]
    async fn spanish_self_correction_with_digo() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("el presidente digo el ex-presidente", &empty_context())
            .await
            .unwrap();
        assert!(
            result.text.contains("ex-presidente"),
            "repair: {}",
            result.text
        );
    }

    #[tokio::test]
    async fn spanish_self_correction_with_mejor_dicho() {
        // `mejor dicho` must outrank bare `mejor` (sorted longest-first).
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("salgo a las cinco mejor dicho a las seis", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("seis"), "repair: {}", result.text);
        assert!(
            !result.text.contains("cinco"),
            "reparandum: {}",
            result.text
        );
    }

    #[tokio::test]
    async fn spanish_self_correction_with_o_sea() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("llamo a Juan o sea a Pedro", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("Pedro"), "repair: {}", result.text);
        assert!(!result.text.contains("Juan"), "reparandum: {}", result.text);
    }

    #[tokio::test]
    async fn spanish_negation_without_overlap_is_not_correction() {
        // Pure negation — `no` is not followed by a content word
        // that overlaps with the reparandum, so the shared-prefix
        // check fails and the detector leaves the text untouched.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("el gato no come pescado", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "el gato no come pescado");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn spanish_no_self_correction_in_clean_text() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("la conferencia es mañana en Barcelona", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "la conferencia es mañana en Barcelona");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn spanish_self_correction_with_commas() {
        // The cue is surrounded by punctuation in the wild; the
        // detector strips it before computing the shared prefix.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("voy a Madrid, perdón, a Barcelona", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("Barcelona"), "repair: {}", result.text);
        assert!(
            !result.text.contains("Madrid"),
            "reparandum: {}",
            result.text
        );
    }

    #[tokio::test]
    async fn spanish_no_does_not_match_inside_word() {
        // `no` as a substring of `Mariano` / `Antonio` / `minoría`
        // must not trigger correction. The word-boundary check in
        // detect_spanish guards against this.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("Mariano y Antonio fueron al cine", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "Mariano y Antonio fueron al cine");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn spanish_quiero_decir_outranks_digo() {
        // `quiero decir` is the longer form of `digo`; longest-first
        // sort puts it first so it gets matched as a single span.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("voy a Madrid quiero decir a Barcelona", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("Barcelona"), "repair: {}", result.text);
        assert!(
            !result.text.contains("Madrid"),
            "reparandum: {}",
            result.text
        );
    }

    // --- BasicPunctuationRestorer tests ---

    #[tokio::test]
    async fn capitalize_first_word() {
        let proc = BasicPunctuationRestorer;
        let result = proc.process("hello world", &empty_context()).await.unwrap();
        assert!(result.text.starts_with('H'));
    }

    #[tokio::test]
    async fn add_terminal_period() {
        let proc = BasicPunctuationRestorer;
        let result = proc.process("hello world", &empty_context()).await.unwrap();
        assert!(result.text.ends_with('.'));
    }

    #[tokio::test]
    async fn punctuation_appends_period_for_korean() {
        // Hangul-dominant text gets a Western period (ko convention).
        // Capitalisation does not apply to Hangul.
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("내일 오후 세 시에 만나기로 했습니다", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "내일 오후 세 시에 만나기로 했습니다.");
    }

    #[tokio::test]
    async fn punctuation_appends_fullwidth_period_for_chinese() {
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("会议在下午三点开始", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "会议在下午三点开始。");
    }

    #[tokio::test]
    async fn punctuation_appends_fullwidth_period_for_japanese() {
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("今日は天気がいい", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "今日は天気がいい。");
    }

    #[tokio::test]
    async fn punctuation_does_not_double_terminal_for_korean() {
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("이것은 정말 좋은 책입니다.", &empty_context())
            .await
            .unwrap();
        // Already terminated, no second period.
        assert_eq!(result.text, "이것은 정말 좋은 책입니다.");
    }

    #[tokio::test]
    async fn punctuation_does_not_double_terminal_for_chinese() {
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("这是一本很好的书。", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "这是一本很好的书。");
    }

    #[tokio::test]
    async fn punctuation_skips_capitalisation_for_hangul() {
        // No Latin alpha → capitalize_next disabled. Hangul chars
        // pass through unchanged.
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("안녕하세요 반갑습니다", &empty_context())
            .await
            .unwrap();
        // No capitalisation correction emitted.
        assert!(
            !result
                .corrections
                .iter()
                .any(|c| matches!(c.kind, CorrectionKind::Capitalized)),
            "{:?}",
            result.corrections
        );
    }

    #[tokio::test]
    async fn preserve_existing_punctuation() {
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("Hello world!", &empty_context())
            .await
            .unwrap();
        assert!(result.text.ends_with('!'));
        assert!(!result.text.ends_with("!."));
    }

    #[tokio::test]
    async fn capitalize_after_period() {
        let proc = BasicPunctuationRestorer;
        let result = proc
            .process("hello. world", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "Hello. World.");
    }

    #[tokio::test]
    async fn japanese_no_terminal_period_added() {
        let proc = BasicPunctuationRestorer;
        // Pure Japanese text should not get an English period appended
        let result = proc
            .process("お江戸を発って二十里上方", &empty_context())
            .await
            .unwrap();
        assert!(
            !result.text.ends_with('.'),
            "Japanese should not get English period: {}",
            result.text
        );
    }

    #[tokio::test]
    async fn empty_text() {
        let proc = BasicPunctuationRestorer;
        let result = proc.process("", &empty_context()).await.unwrap();
        assert_eq!(result.text, "");
        assert!(result.corrections.is_empty());
    }

    /// Regression: when the input contains `っていうか`, the detector
    /// used to find the shorter `ていうか` substring first (because
    /// cues were iterated in declared order and `ていうか` precedes
    /// `っていうか` in the list). That treated just `っ` as the
    /// reparandum and left the real reparandum (e.g. `鈴木課長`) in
    /// the output. Sorting cues longest-first at construction time
    /// fixes this, surfaced by the L3 self-correction direct F1
    /// evaluation against the committed ja annotations.
    #[tokio::test]
    async fn ja_self_correction_with_tte_iu_ka() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("鈴木課長、っていうか佐藤課長です", &empty_context())
            .await
            .unwrap();
        assert!(
            !result.text.contains("鈴木課長"),
            "reparandum should be removed: {}",
            result.text,
        );
        assert!(
            result.text.contains("佐藤課長"),
            "repair should be kept: {}",
            result.text,
        );
        assert_eq!(result.text, "佐藤課長です");
    }

    /// Regression: same shape as `っていうか`/`ていうか` but for English
    /// (`no wait` containing `no` as a prefix). Without longest-first
    /// cue sorting, `no` could match first inside `no wait` and the
    /// detector would skip the full reparandum.
    #[tokio::test]
    async fn en_self_correction_no_wait_prefers_long_cue() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("I want to go to Boston no wait to Denver", &empty_context())
            .await
            .unwrap();
        assert!(!result.text.contains("Boston"), "{}", result.text);
        assert!(result.text.contains("Denver"), "{}", result.text);
    }

    // -----------------------------------------------------------------
    // Chinese self-correction — same comma-segmented pattern as ja.
    // -----------------------------------------------------------------

    #[tokio::test]
    async fn chinese_self_correction_with_bu_dui() {
        // 不对 = "no, wrong" — drops the trailing comma-segment
        // before the cue.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("我们明天开会,不对,后天开会", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("后天"), "repair: {}", result.text);
        assert!(!result.text.contains("明天"), "reparandum: {}", result.text);
    }

    #[tokio::test]
    async fn chinese_self_correction_with_wo_shi_shuo() {
        // 我是说 = "I mean".
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("会议室在三楼,我是说,在四楼", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("四楼"), "{}", result.text);
        assert!(!result.text.contains("三楼"), "{}", result.text);
    }

    #[tokio::test]
    async fn chinese_self_correction_with_bu_shi() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("我去上海,不是,我去北京", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("北京"), "{}", result.text);
    }

    #[tokio::test]
    async fn chinese_no_self_correction_in_clean_text() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("会议在下午三点开始", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "会议在下午三点开始");
        assert_eq!(result.corrections.len(), 0);
    }

    // -----------------------------------------------------------------
    // Korean self-correction — eojeol-tokenised with 1-eojeol fallback.
    // -----------------------------------------------------------------

    #[tokio::test]
    async fn korean_self_correction_single_eojeol_repair() {
        // `8시 아니 9시에 만나자` — single-eojeol reparandum,
        // 1-eojeol-fallback path.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("8시 아니 9시에 만나자", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("9시"), "repair: {}", result.text);
        assert!(!result.text.contains("8시"), "reparandum: {}", result.text);
    }

    #[tokio::test]
    async fn korean_self_correction_with_geuge_anira() {
        // `그게 아니라` = "that's not it, but…" — multi-eojeol cue
        // outranks bare `아니` via longest-first sort.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("내가 갈게 그게 아니라 네가 갈게", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("네가"), "{}", result.text);
        assert!(!result.text.contains("내가"), "{}", result.text);
    }

    #[tokio::test]
    async fn korean_self_correction_with_jamkkanman() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("회의실은 삼층 잠깐만 사층입니다", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("사층"), "{}", result.text);
    }

    #[tokio::test]
    async fn korean_self_correction_with_shared_prefix_overlap() {
        // En / es style shared-prefix path. The first word of the
        // repair (`오늘`) appears in the reparandum (`오늘 갈게`),
        // so count_shared_prefix_from_end returns 2 → drop both
        // trailing eojeol from before, keep the after verbatim.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("오늘 갈게 아니 오늘 안 갈게", &empty_context())
            .await
            .unwrap();
        assert!(result.text.contains("안 갈게"), "{}", result.text);
    }

    #[tokio::test]
    async fn korean_no_self_correction_in_clean_text() {
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("내일 오후 세 시에 만나기로 했습니다", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "내일 오후 세 시에 만나기로 했습니다");
        assert_eq!(result.corrections.len(), 0);
    }

    #[tokio::test]
    async fn korean_ani_inside_word_does_not_trigger() {
        // `아니에요` is a sentence-final negation predicate
        // ("is not"), not a cue. The 아니에요 cue itself IS in our
        // list (so a bare "X 아니에요 Y" form would fire), but
        // `이것은 아니에요` (= "this is not") is a clean sentence
        // because there's no Y after the cue.
        let detector = SelfCorrectionDetector::new();
        let result = detector
            .process("이것은 아니에요", &empty_context())
            .await
            .unwrap();
        // Cue at end of utterance → after_cue is empty → detector
        // skips this position.
        assert_eq!(result.text, "이것은 아니에요");
    }

    // --- InverseTextNormalizer tests ---

    #[tokio::test]
    async fn itn_english_sentence_normalizes_numerals() {
        let itn = InverseTextNormalizer::new("en");
        let result = itn
            .process("i have twenty five dollars", &empty_context())
            .await
            .unwrap();
        assert!(
            result.text.contains("$25"),
            "expected written-form currency: {}",
            result.text
        );
        assert_eq!(result.corrections.len(), 1);
        assert_eq!(
            result.corrections[0].kind,
            CorrectionKind::NumeralNormalized
        );
    }

    #[tokio::test]
    async fn itn_chinese_sentence_normalizes_numerals() {
        let itn = InverseTextNormalizer::new("zh");
        let result = itn
            .process("我有二十五块钱", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "我有25块钱");
    }

    #[tokio::test]
    async fn itn_japanese_sentence_normalizes_numerals() {
        let itn = InverseTextNormalizer::new("ja");
        let result = itn
            .process("そこに鳥一羽がいます", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "そこに鳥1羽がいます");
    }

    #[tokio::test]
    async fn itn_clean_text_emits_no_correction() {
        let itn = InverseTextNormalizer::new("en");
        let result = itn
            .process("the meeting is on friday", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "the meeting is on friday");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn itn_spanish_passes_through_until_upstream_support() {
        // es has no sentence-level ITN entry point upstream yet, so the
        // normalizer is a no-op rather than silently mangling text.
        let itn = InverseTextNormalizer::new("es");
        let result = itn
            .process("tengo veinticinco años", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "tengo veinticinco años");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn itn_korean_passes_through_until_upstream_support() {
        // ko support is a pending upstream PR; until it lands the
        // normalizer leaves Korean text untouched.
        let itn = InverseTextNormalizer::new("ko");
        let result = itn
            .process("이천이십육년에 만났다", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "이천이십육년에 만났다");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn itn_unknown_language_passes_through() {
        let itn = InverseTextNormalizer::new("xx");
        let result = itn
            .process("arbitrary text", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "arbitrary text");
        assert!(result.corrections.is_empty());
    }

    // --- SpokenFormNormalizer tests ---

    #[tokio::test]
    async fn spoken_form_expands_english_reductions() {
        let sfn = SpokenFormNormalizer::new("en");
        let result = sfn
            .process("i'm gonna grab a coffee", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "i'm going to grab a coffee");
        assert_eq!(result.corrections.len(), 1);
        assert_eq!(
            result.corrections[0].kind,
            CorrectionKind::SpokenFormNormalized
        );
    }

    #[tokio::test]
    async fn spoken_form_handles_multiple_and_punctuation() {
        let sfn = SpokenFormNormalizer::new("en");
        let result = sfn
            .process("lemme know, i kinda forgot.", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "let me know, i kind of forgot.");
        assert_eq!(result.corrections.len(), 2);
    }

    #[tokio::test]
    async fn spoken_form_preserves_leading_capitalization() {
        let sfn = SpokenFormNormalizer::new("en");
        let result = sfn
            .process("Gonna head out now", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "Going to head out now");
    }

    #[tokio::test]
    async fn spoken_form_keeps_apostrophe_words() {
        let sfn = SpokenFormNormalizer::new("en");
        let result = sfn
            .process("c'mon we leave 'cause it's late", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "come on we leave because it's late");
    }

    #[tokio::test]
    async fn spoken_form_clean_text_is_untouched() {
        let sfn = SpokenFormNormalizer::new("en");
        let result = sfn
            .process("the meeting  is on friday", &empty_context())
            .await
            .unwrap();
        // No match → input returned verbatim, including the double space.
        assert_eq!(result.text, "the meeting  is on friday");
        assert!(result.corrections.is_empty());
    }

    #[tokio::test]
    async fn spoken_form_unsupported_language_passes_through() {
        let sfn = SpokenFormNormalizer::new("ja");
        let result = sfn
            .process("これはテストです", &empty_context())
            .await
            .unwrap();
        assert_eq!(result.text, "これはテストです");
        assert!(result.corrections.is_empty());
    }
}