anno 0.10.0

NER, coreference resolution, relation extraction, PII detection, and zero-shot entity types
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
//! Discourse-level entities for abstract anaphora resolution.
//!
//! # Why This Matters
//!
//! Standard coreference systems link noun phrases: "John" → "he" → "the CEO".
//! But natural language routinely refers to **abstract objects**—events,
//! propositions, facts, situations—that aren't noun phrases at all:
//!
//! ```text
//! "Russia invaded Ukraine. This shocked the world."
//!                          ^^^^
//!                          What is "this"? Not a person, place, or thing.
//!                          It's the *event* of the invasion.
//! ```
//!
//! Current NER/coref systems fail catastrophically on abstract anaphora because
//! they only look for nominal antecedents. This module provides the types needed
//! to represent and resolve these discourse-level references.
//!
//! # The Core Problem
//!
//! Standard NER extracts **nominal** entities (Person, Org, Location).
//! Abstract anaphora requires extracting **discourse referents**:
//!
//! | Type | Example Source | Anaphor | What's Referenced |
//! |------|---------------|---------|-------------------|
//! | **Event** | "Russia invaded Ukraine" | "This shocked..." | The invasion |
//! | **Proposition** | "She might resign" | "This worries me" | The possibility |
//! | **Fact** | "Water boils at 100°C" | "This is well-known" | The established fact |
//! | **Situation** | "Prices rose while wages fell" | "This was unsustainable" | The state of affairs |
//!
//! These are not noun phrases but *discourse segments* that can serve as
//! antecedents for shell nouns ("this problem") and demonstratives ("This").
//!
//! # Theoretical Foundation: Higher-Order Unification
//!
//! Following Dalrymple, Shieber & Pereira (1991), we can view abstract anaphora
//! resolution as solving an equation:
//!
//! ```text
//! P(s₁, s₂, ..., sₙ) = s
//! ```
//!
//! Where `s` is the source clause interpretation, `sᵢ` are parallel elements,
//! and `P` is the property/relation being predicated. The resolved property
//! is then applied to parallel elements in the target clause.
//!
//! **Key insights from this view:**
//!
//! 1. **Multiple readings emerge** from a single unambiguous source—the
//!    ambiguity is in *how we abstract*, not in the source itself
//! 2. **Strict/sloppy distinctions** arise from which occurrences get abstracted
//! 3. **Deeply embedded antecedents** can license sloppy readings (contra c-command)
//! 4. **Cascaded anaphora** chains resolve correctly and order-free
//! 5. **Shell noun classes act as type constraints** on the property P
//!
//! # Key Papers
//!
//! - Dalrymple, Shieber & Pereira (1991): "Ellipsis and Higher-Order Unification"
//! - Kolhatkar & Hirst (2012): "Resolving 'this-issue' anaphors"
//! - Marasović et al. (2017): "A Mention-Ranking Model for Abstract Anaphora Resolution"
//! - Schmid (2000): ~670 shell nouns in English
//! - Asher (1993): "Reference to Abstract Objects in Discourse"
//!
//! # Example
//!
//! ```rust
//! use anno::discourse::{DiscourseReferent, ReferentType, EventMention};
//!
//! let text = "Russia invaded Ukraine in 2022. This caused inflation.";
//!
//! // The event mention
//! let event = EventMention::new("invaded", 7, 14)
//!     .with_trigger_type("attack")
//!     .with_arguments(vec![("Agent", "Russia"), ("Patient", "Ukraine")]);
//!
//! // The full discourse referent (spans the whole clause)
//! let referent = DiscourseReferent::new(ReferentType::Event, 0, 30)
//!     .with_event(event)
//!     .with_label("Russia's invasion of Ukraine");
//!
//! assert!(referent.referent_type.is_abstract());
//! ```

use crate::{Confidence, Entity};
use serde::{Deserialize, Serialize};

// =============================================================================
// Discourse Referent Types
// =============================================================================

/// Type of discourse referent (what kind of "thing" can be referred to).
///
/// # Why This Taxonomy?
///
/// Not all things that can be referred to are "things" in the ordinary sense.
/// When someone says "This surprised me," the referent might be:
/// - A person (nominal) → standard coreference
/// - An event ("the crash") → needs event extraction
/// - A fact ("that he won") → needs propositional analysis
/// - A situation ("the ongoing crisis") → needs discourse modeling
///
/// The type constrains what predicates are felicitous:
/// - Events can "happen," "occur," be "witnessed"
/// - Facts can be "known," "believed," "denied"
/// - Propositions can be "true," "false," "uncertain"
///
/// # Research Background
///
/// This taxonomy follows Asher (1993) "Reference to Abstract Objects in Discourse"
/// and Nedoluzhko & Lapshinova-Koltunski (2022) survey.
///
/// ## Ontological Distinctions
///
/// - **Nominal**: Standard NER entities (Person, Org, etc.)
/// - **Event**: Something that happened at a specific time/place
/// - **Fact**: A true proposition (can be asserted, denied)
/// - **Proposition**: A potential truth value (can be believed, doubted)
/// - **Situation**: A state of affairs (ongoing, not instantaneous)
///
/// ## Connection to Higher-Order Unification
///
/// When resolving anaphora to abstract referents, we solve `P(source) = interpretation`
/// where the *type* of P is constrained by the referent type:
///
/// | Referent Type | Property Domain | Example Predicate |
/// |---------------|-----------------|-------------------|
/// | Event | event → truth | "shocked everyone" |
/// | Fact | fact → truth | "is undeniable" |
/// | Proposition | prop → truth | "worries me" |
/// | Situation | situation → truth | "was unsustainable" |
///
/// Shell nouns (see [`ShellNounClass`]) further constrain which referent types
/// are valid—this acts as a type constraint on the unification variable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[non_exhaustive]
pub enum ReferentType {
    /// Standard nominal entity (Person, Org, Location)
    #[default]
    Nominal,

    // === Abstract Types ===
    /// Event: happened at a specific time/place
    /// Example: "The earthquake struck" → EVENT
    Event,

    /// Fact: a true proposition that can be asserted
    /// Example: "Water boils at 100C" → FACT
    Fact,

    /// Proposition: a potential truth value
    /// Example: "She might resign" → PROPOSITION (not yet true/false)
    Proposition,

    /// Situation: ongoing state of affairs
    /// Example: "Prices are rising" → SITUATION
    Situation,

    /// Manner: how something was done
    /// Example: "He spoke softly" → MANNER (the softness)
    Manner,

    /// Discourse segment: a larger chunk of text
    /// Example: "The preceding paragraph" → SEGMENT
    Segment,
}

impl ReferentType {
    /// Is this an abstract (non-nominal) referent type?
    #[must_use]
    pub const fn is_abstract(&self) -> bool {
        !matches!(self, ReferentType::Nominal)
    }

    /// Human-readable label.
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            ReferentType::Nominal => "nominal",
            ReferentType::Event => "event",
            ReferentType::Fact => "fact",
            ReferentType::Proposition => "proposition",
            ReferentType::Situation => "situation",
            ReferentType::Manner => "manner",
            ReferentType::Segment => "segment",
        }
    }

    /// Can this referent type be the antecedent of "this"?
    #[must_use]
    pub const fn can_be_this_antecedent(&self) -> bool {
        // All abstract types can be referred to by "this"
        self.is_abstract()
    }

    /// Can this referent type be the antecedent of "it"?
    #[must_use]
    pub const fn can_be_it_antecedent(&self) -> bool {
        // "it" can refer to events and situations, but less naturally to facts/propositions
        matches!(
            self,
            ReferentType::Nominal | ReferentType::Event | ReferentType::Situation
        )
    }
}

// =============================================================================
// Event Mention
// =============================================================================

/// An event mention extracted from text.
///
/// Events are a key type of abstract antecedent. They have:
/// - A **trigger** word/phrase (usually a verb or event noun)
/// - **Arguments** (agent, patient, location, time, etc.)
/// - **Type** classification (attack, movement, creation, etc.)
///
/// # Research Background
///
/// Event extraction is a well-studied task (ACE, TAC-KBP, etc.).
/// We use a simplified model focused on what's needed for anaphora resolution.
///
/// # Example
///
/// Text: "Russia invaded Ukraine in February 2022"
///
/// ```rust
/// use anno::discourse::EventMention;
///
/// let event = EventMention::new("invaded", 7, 14)
///     .with_trigger_type("attack")
///     .with_arguments(vec![
///         ("Agent", "Russia"),
///         ("Patient", "Ukraine"),
///         ("Time", "February 2022"),
///     ]);
///
/// assert_eq!(event.trigger, "invaded");
/// assert_eq!(event.trigger_type.as_deref(), Some("attack"));
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventMention {
    /// The trigger word/phrase (e.g., "invaded", "announced")
    pub trigger: String,
    /// Start character offset of trigger
    pub trigger_start: usize,
    /// End character offset of trigger
    pub trigger_end: usize,
    /// Event type (e.g., "attack", "movement", "communication")
    pub trigger_type: Option<String>,
    /// Event arguments (role -> entity text)
    /// Common roles: Agent, Patient, Location, Time, Instrument
    pub arguments: Vec<(String, String)>,
    /// Confidence score for this event extraction
    pub confidence: Confidence,
    /// Event polarity (positive, negative, uncertain)
    pub polarity: EventPolarity,
    /// Event tense/aspect
    pub tense: Option<EventTense>,
}

/// Polarity of an event mention.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum EventPolarity {
    /// The event did/will happen (default)
    #[default]
    Positive,
    /// The event did not / will not happen
    Negative,
    /// Unknown whether the event happened
    Uncertain,
}

/// Tense of an event mention.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EventTense {
    /// Event occurred in the past
    Past,
    /// Event is occurring now
    Present,
    /// Event will occur in the future
    Future,
    /// Event is hypothetical/conditional
    Hypothetical,
}

impl EventMention {
    /// Create a new event mention.
    #[must_use]
    pub fn new(trigger: impl Into<String>, start: usize, end: usize) -> Self {
        Self {
            trigger: trigger.into(),
            trigger_start: start,
            trigger_end: end,
            trigger_type: None,
            arguments: Vec::new(),
            confidence: Confidence::ONE,
            polarity: EventPolarity::default(),
            tense: None,
        }
    }

    /// Set the trigger type (event classification).
    #[must_use]
    pub fn with_trigger_type(mut self, trigger_type: impl Into<String>) -> Self {
        self.trigger_type = Some(trigger_type.into());
        self
    }

    /// Add arguments to this event.
    #[must_use]
    pub fn with_arguments<S: Into<String>>(mut self, args: Vec<(&str, S)>) -> Self {
        self.arguments = args
            .into_iter()
            .map(|(role, text)| (role.to_string(), text.into()))
            .collect();
        self
    }

    /// Set confidence score.
    #[must_use]
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = Confidence::new(confidence);
        self
    }

    /// Set polarity.
    #[must_use]
    pub fn with_polarity(mut self, polarity: EventPolarity) -> Self {
        self.polarity = polarity;
        self
    }

    /// Set tense.
    #[must_use]
    pub fn with_tense(mut self, tense: EventTense) -> Self {
        self.tense = Some(tense);
        self
    }

    /// Get argument by role.
    #[must_use]
    pub fn get_argument(&self, role: &str) -> Option<&str> {
        self.arguments
            .iter()
            .find(|(r, _)| r.eq_ignore_ascii_case(role))
            .map(|(_, text)| text.as_str())
    }
}

// =============================================================================
// Discourse Referent
// =============================================================================

/// A discourse referent - something that can be referred to by an anaphor.
///
/// This is the key data structure for abstract anaphora resolution.
/// Unlike `Entity` which captures noun phrases, `DiscourseReferent` can
/// represent entire clauses, events, propositions, etc.
///
/// # Example
///
/// ```rust
/// use anno::discourse::{DiscourseReferent, ReferentType, EventMention};
///
/// // "Russia invaded Ukraine in 2022. This caused inflation."
/// let event = EventMention::new("invaded", 7, 14);
/// let referent = DiscourseReferent::new(ReferentType::Event, 0, 30)
///     .with_event(event)
///     .with_label("Russian invasion");
///
/// assert_eq!(referent.span(), (0, 30));
/// assert!(referent.is_abstract());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscourseReferent {
    /// What kind of referent this is
    pub referent_type: ReferentType,
    /// Start character offset (of the full discourse segment)
    pub start: usize,
    /// End character offset (of the full discourse segment)
    pub end: usize,
    /// Human-readable label for this referent
    pub label: Option<String>,
    /// The full text of this referent (optional, for debugging)
    pub text: Option<String>,
    /// If this is an event, the event mention details
    pub event: Option<EventMention>,
    /// Coreference cluster ID (shared with entities that refer to this)
    pub canonical_id: Option<crate::CanonicalId>,
    /// Confidence that this is a valid discourse referent
    pub confidence: Confidence,
    /// Discourse depth (how nested this referent is)
    /// 0 = main clause, 1 = subordinate, 2 = embedded subordinate, etc.
    pub depth: u32,
}

impl DiscourseReferent {
    /// Create a new discourse referent.
    #[must_use]
    pub fn new(referent_type: ReferentType, start: usize, end: usize) -> Self {
        Self {
            referent_type,
            start,
            end,
            label: None,
            text: None,
            event: None,
            canonical_id: None,
            confidence: Confidence::ONE,
            depth: 0,
        }
    }

    /// Create a nominal referent from an Entity.
    #[must_use]
    pub fn from_entity(entity: &Entity) -> Self {
        Self {
            referent_type: ReferentType::Nominal,
            start: entity.start(),
            end: entity.end(),
            label: Some(entity.text.clone()),
            text: Some(entity.text.clone()),
            event: None,
            canonical_id: entity.canonical_id,
            confidence: entity.confidence,
            depth: 0,
        }
    }

    /// Set a human-readable label.
    #[must_use]
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Set the text.
    #[must_use]
    pub fn with_text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self
    }

    /// Attach an event mention.
    #[must_use]
    pub fn with_event(mut self, event: EventMention) -> Self {
        self.event = Some(event);
        self
    }

    /// Set the canonical ID.
    #[must_use]
    pub fn with_canonical_id(mut self, id: impl Into<crate::CanonicalId>) -> Self {
        self.canonical_id = Some(id.into());
        self
    }

    /// Set confidence.
    #[must_use]
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = Confidence::new(confidence);
        self
    }

    /// Set discourse depth.
    #[must_use]
    pub fn with_depth(mut self, depth: u32) -> Self {
        self.depth = depth;
        self
    }

    /// Get the span as (start, end).
    #[must_use]
    pub const fn span(&self) -> (usize, usize) {
        (self.start, self.end)
    }

    /// Get span length.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.end.saturating_sub(self.start)
    }

    /// Check if span is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.end <= self.start
    }

    /// Is this an abstract referent (not a nominal entity)?
    #[must_use]
    pub const fn is_abstract(&self) -> bool {
        self.referent_type.is_abstract()
    }

    /// Get the display text (label or text or type name).
    #[must_use]
    pub fn display_text(&self) -> &str {
        self.label
            .as_deref()
            .or(self.text.as_deref())
            .unwrap_or(self.referent_type.as_str())
    }
}

// =============================================================================
// Shell Noun
// =============================================================================

/// A shell noun that can refer to abstract antecedents.
///
/// Shell nouns are abstract nouns like "problem", "issue", "fact" that
/// serve as "conceptual shells" for complex information.
///
/// # Research Background
///
/// Schmid (2000) identified ~670 shell nouns in English, organized into
/// six semantic classes: factual, linguistic, mental, modal, eventive,
/// circumstantial.
///
/// # Example
///
/// ```rust
/// use anno::discourse::{ShellNoun, ShellNounClass};
///
/// let shell = ShellNoun::new("problem", ShellNounClass::Factual)
///     .with_determiner("this")
///     .at_span(32, 44);
///
/// assert!(shell.is_demonstrative());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellNoun {
    /// The shell noun lemma (e.g., "problem", "issue", "fact")
    pub lemma: String,
    /// Semantic class of this shell noun
    pub class: ShellNounClass,
    /// The determiner used (e.g., "this", "the", "such")
    pub determiner: Option<String>,
    /// Start character offset
    pub start: usize,
    /// End character offset
    pub end: usize,
    /// The full NP text (e.g., "this problem")
    pub full_text: Option<String>,
}

/// Semantic classes of shell nouns (from Schmid 2000).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ShellNounClass {
    /// Factual: fact, reason, evidence, proof, point
    Factual,
    /// Linguistic: claim, statement, argument, answer, question
    Linguistic,
    /// Mental: idea, belief, thought, view, opinion
    Mental,
    /// Modal: possibility, chance, ability, need, requirement
    Modal,
    /// Eventive: event, incident, action, step, move
    Eventive,
    /// Circumstantial: situation, context, case, circumstance, condition
    Circumstantial,
}

impl ShellNounClass {
    /// Human-readable label.
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            ShellNounClass::Factual => "factual",
            ShellNounClass::Linguistic => "linguistic",
            ShellNounClass::Mental => "mental",
            ShellNounClass::Modal => "modal",
            ShellNounClass::Eventive => "eventive",
            ShellNounClass::Circumstantial => "circumstantial",
        }
    }

    /// What referent types does this shell noun class typically refer to?
    #[must_use]
    pub fn typical_antecedent_types(&self) -> &[ReferentType] {
        match self {
            ShellNounClass::Factual => &[ReferentType::Fact, ReferentType::Event],
            ShellNounClass::Linguistic => &[ReferentType::Proposition],
            ShellNounClass::Mental => &[ReferentType::Proposition, ReferentType::Fact],
            ShellNounClass::Modal => &[ReferentType::Proposition],
            ShellNounClass::Eventive => &[ReferentType::Event, ReferentType::Situation],
            ShellNounClass::Circumstantial => &[ReferentType::Situation],
        }
    }
}

impl ShellNoun {
    /// Create a new shell noun.
    #[must_use]
    pub fn new(lemma: impl Into<String>, class: ShellNounClass) -> Self {
        Self {
            lemma: lemma.into(),
            class,
            determiner: None,
            start: 0,
            end: 0,
            full_text: None,
        }
    }

    /// Set the determiner.
    #[must_use]
    pub fn with_determiner(mut self, det: impl Into<String>) -> Self {
        self.determiner = Some(det.into());
        self
    }

    /// Set the span.
    #[must_use]
    pub fn at_span(mut self, start: usize, end: usize) -> Self {
        self.start = start;
        self.end = end;
        self
    }

    /// Set full text.
    #[must_use]
    pub fn with_full_text(mut self, text: impl Into<String>) -> Self {
        self.full_text = Some(text.into());
        self
    }

    /// Is this a demonstrative shell noun (e.g., "this problem")?
    #[must_use]
    pub fn is_demonstrative(&self) -> bool {
        self.determiner
            .as_ref()
            .map(|d| {
                matches!(
                    d.to_lowercase().as_str(),
                    "this" | "that" | "these" | "those"
                )
            })
            .unwrap_or(false)
    }

    /// Get the typical antecedent types for this shell noun.
    #[must_use]
    pub fn typical_antecedent_types(&self) -> &[ReferentType] {
        self.class.typical_antecedent_types()
    }
}

// =============================================================================
// Common Shell Nouns Lexicon
// =============================================================================

/// Get the semantic class for a known shell noun.
///
/// Based on Schmid (2000) taxonomy. Returns `None` for unknown nouns.
#[must_use]
pub fn classify_shell_noun(lemma: &str) -> Option<ShellNounClass> {
    match lemma.to_lowercase().as_str() {
        // Factual
        "fact" | "reason" | "evidence" | "proof" | "point" | "truth" | "result" | "outcome"
        | "consequence" | "effect" | "cause" => Some(ShellNounClass::Factual),

        // Linguistic
        "claim" | "statement" | "argument" | "answer" | "question" | "response" | "reply"
        | "assertion" | "allegation" | "announcement" | "explanation" | "suggestion"
        | "recommendation" | "proposal" | "promise" | "warning" | "threat" => {
            Some(ShellNounClass::Linguistic)
        }

        // Mental
        "idea" | "belief" | "thought" | "view" | "opinion" | "impression" | "feeling" | "sense"
        | "notion" | "assumption" | "understanding" | "knowledge" | "memory" | "expectation"
        | "hope" | "fear" | "worry" | "concerno" => Some(ShellNounClass::Mental),

        // Modal
        "possibility" | "chance" | "ability" | "need" | "requirement" | "necessity"
        | "obligation" | "duty" | "right" | "permission" | "opportunity" | "risk" | "danger"
        | "likelihood" | "probability" => Some(ShellNounClass::Modal),

        // Eventive
        "event" | "incident" | "action" | "step" | "move" | "development" | "change"
        | "process" | "procedure" | "activity" | "behavior" | "decision" | "choice" | "attempt"
        | "effort" | "achievement" | "success" | "failure" => Some(ShellNounClass::Eventive),

        // Circumstantial
        "situation" | "context" | "case" | "circumstance" | "condition" | "state" | "position"
        | "environment" | "scenario" | "aspect" | "factor" | "issue" | "problem" | "difficulty"
        | "challenge" | "crisis" | "dilemma" => Some(ShellNounClass::Circumstantial),

        _ => None,
    }
}

/// Check if a word is a known shell noun.
#[must_use]
pub fn is_shell_noun(word: &str) -> bool {
    classify_shell_noun(word).is_some()
}

// =============================================================================
// Discourse Scope Tracking
// =============================================================================

/// A simple clause/sentence boundary detector for discourse scope analysis.
///
/// # Why Bounded Context?
///
/// Abstract anaphora resolution requires finding antecedents in preceding
/// discourse. But how far back should we look?
///
/// **Empirical finding**: Window size n=2-3 preceding clauses outperforms
/// max-length concatenation by ~6% F1. Larger windows add noise without
/// improving recall. This motivates bounded `preceding_clauses(offset, n)`.
///
/// # Antecedent Distance Distribution
///
/// For abstract anaphora, the antecedent is typically:
/// - **Immediately preceding clause** (~60%): "X happened. This..."
/// - **Same sentence, different clause** (~20%): "When X, this..."
/// - **Previous sentence** (~15%): "X. Y. This..."
/// - **2+ sentences back** (~5%): Rare, usually with explicit markers
///
/// # Theoretical Background (Dalrymple et al. 1991)
///
/// The equational view frames resolution as finding P such that
/// `P(parallel_elements) = source_interpretation`. Crucially, parallelism
/// need not be purely syntactic—semantic and pragmatic parallelism also
/// license resolution (Section 5.1).
///
/// This means:
/// 1. Syntactic distance isn't the only constraint
/// 2. Active/passive, logical subjects, and pragmatic factors affect parallelism
/// 3. The `candidate_antecedent_spans` method returns spans in preference order,
///    but *semantic* parallelism must be checked by higher-level resolution logic
///
/// # Example
///
/// ```rust
/// use anno::discourse::DiscourseScope;
///
/// let text = "Russia invaded Ukraine in 2022. This caused inflation. It affected everyone.";
/// let scope = DiscourseScope::analyze(text);
///
/// // For "This" at position 32, the immediately preceding clause is preferred
/// let candidates = scope.preceding_clauses(32, 2);
/// // Returns spans for "Russia invaded Ukraine in 2022" and potentially more
/// ```
///
/// # Character vs Byte Offsets
///
/// All offsets in `DiscourseScope` are **character offsets**, not byte offsets.
/// This is critical for Unicode text where characters may be multi-byte:
///
/// ```rust
/// use anno::discourse::DiscourseScope;
///
/// let text = "日本語。英語。"; // Japanese with periods
/// let scope = DiscourseScope::analyze(text);
///
/// // Character-based: each kanji is 1 character (but 3 bytes)
/// assert!(scope.sentence_count() >= 1);
/// ```
///
/// Use `extract_span` to safely extract text from character offsets.
#[derive(Debug, Clone)]
pub struct DiscourseScope {
    /// Sentence boundaries (character offsets, NOT byte offsets)
    pub sentence_boundaries: Vec<usize>,
    /// Clause boundaries (character offsets, more fine-grained)
    pub clause_boundaries: Vec<usize>,
    /// Mapping from char offsets to byte offsets for extraction
    char_to_byte: Vec<usize>,
}

impl DiscourseScope {
    /// Analyze text for discourse boundaries.
    ///
    /// # Example
    /// ```rust
    /// use anno::discourse::DiscourseScope;
    ///
    /// let text = "Russia invaded Ukraine. This caused inflation.";
    /// let scope = DiscourseScope::analyze(text);
    ///
    /// assert_eq!(scope.sentence_count(), 2);
    /// ```
    #[must_use]
    pub fn analyze(text: &str) -> Self {
        // Build char-to-byte mapping for later extraction
        let char_to_byte = Self::build_char_to_byte_map(text);

        let sentence_boundaries = Self::find_sentence_boundaries(text);
        let clause_boundaries = Self::find_clause_boundaries(text);

        Self {
            sentence_boundaries,
            clause_boundaries,
            char_to_byte,
        }
    }

    /// Build mapping from character index to byte index.
    fn build_char_to_byte_map(text: &str) -> Vec<usize> {
        let char_count = text.chars().count();
        let mut map = Vec::with_capacity(char_count + 1);

        for (byte_idx, _ch) in text.char_indices() {
            map.push(byte_idx);
        }
        map.push(text.len()); // End position

        map
    }

    /// Convert character offset to byte offset.
    fn char_to_byte_offset(&self, char_offset: usize) -> usize {
        self.char_to_byte
            .get(char_offset)
            .copied()
            .unwrap_or_else(|| self.char_to_byte.last().copied().unwrap_or(0))
    }

    /// Find sentence boundaries using punctuation heuristics.
    ///
    /// Returns CHARACTER offsets (not byte offsets).
    fn find_sentence_boundaries(text: &str) -> Vec<usize> {
        let mut boundaries = vec![0]; // Start of text
        let chars: Vec<char> = text.chars().collect();
        let char_count = chars.len();

        for (i, &c) in chars.iter().enumerate() {
            // Sentence-ending punctuation
            if matches!(c, '.' | '!' | '?' | '' | '' | '') {
                // Check it's not an abbreviation (followed by lowercase)
                let next_char = chars.get(i + 1).or(chars.get(i + 2));
                let after_space = chars.get(i + 2);

                // If followed by space and uppercase (or end of text), it's a sentence boundary
                let boundary_ok = match next_char {
                    None => true,
                    Some(&nc) => nc.is_whitespace() || nc == '"' || nc == '\'',
                };
                let after_ok = match after_space {
                    None => true,
                    Some(&ac) => ac.is_uppercase() || ac == '"',
                };
                if boundary_ok && after_ok {
                    // Return character offset (i+1 is after the punctuation)
                    boundaries.push(i + 1);
                }
            }
        }

        // End of text (character count)
        if boundaries.last() != Some(&char_count) {
            boundaries.push(char_count);
        }

        boundaries
    }

    /// Find clause boundaries using punctuation and connectors.
    ///
    /// Returns CHARACTER offsets (not byte offsets).
    fn find_clause_boundaries(text: &str) -> Vec<usize> {
        let mut boundaries = vec![0];

        // Clause-separating punctuation and words
        let clause_markers = [
            ", and ",
            ", but ",
            ", or ",
            ", so ",
            ", yet ",
            "; ",
            ": ",
            " -- ",
            "",
            " while ",
            " although ",
            " because ",
            " since ",
            " when ",
            " whereas ",
            " unless ",
            " if ",
            // CJK clause markers
            "", // Japanese/Chinese comma
            "", // Chinese comma
        ];

        // Convert text to lowercase for matching, but track character positions
        let text_lower = text.to_lowercase();

        for marker in &clause_markers {
            let marker_lower = marker.to_lowercase();

            // Find byte positions in lowercase text, then convert to char positions
            let mut search_from_byte = 0;
            while let Some(byte_pos) = text_lower[search_from_byte..].find(&marker_lower) {
                let absolute_byte_pos = search_from_byte + byte_pos + marker.len();

                // Convert byte position to character position
                let char_pos = text[..absolute_byte_pos.min(text.len())].chars().count();

                boundaries.push(char_pos);
                search_from_byte = absolute_byte_pos;
            }
        }

        // Also add sentence boundaries
        boundaries.extend(Self::find_sentence_boundaries(text));

        boundaries.sort();
        boundaries.dedup();
        boundaries
    }

    /// Number of sentences detected.
    #[must_use]
    pub fn sentence_count(&self) -> usize {
        self.sentence_boundaries.len().saturating_sub(1)
    }

    /// Number of clauses detected.
    #[must_use]
    pub fn clause_count(&self) -> usize {
        self.clause_boundaries.len().saturating_sub(1)
    }

    /// Get the sentence containing a character offset.
    #[must_use]
    pub fn sentence_at(&self, offset: usize) -> Option<(usize, usize)> {
        for window in self.sentence_boundaries.windows(2) {
            if offset >= window[0] && offset < window[1] {
                return Some((window[0], window[1]));
            }
        }
        None
    }

    /// Get the clause containing a character offset.
    #[must_use]
    pub fn clause_at(&self, offset: usize) -> Option<(usize, usize)> {
        for window in self.clause_boundaries.windows(2) {
            if offset >= window[0] && offset < window[1] {
                return Some((window[0], window[1]));
            }
        }
        None
    }

    /// Get the N preceding clauses from an offset.
    #[must_use]
    pub fn preceding_clauses(&self, offset: usize, n: usize) -> Vec<(usize, usize)> {
        let mut clauses = Vec::new();

        // Find which clause we're in
        let mut current_idx = None;
        for (i, window) in self.clause_boundaries.windows(2).enumerate() {
            if offset >= window[0] && offset < window[1] {
                current_idx = Some(i);
                break;
            }
        }

        if let Some(idx) = current_idx {
            // Collect preceding clauses
            for i in (0..idx).rev().take(n) {
                if i + 1 < self.clause_boundaries.len() {
                    clauses.push((self.clause_boundaries[i], self.clause_boundaries[i + 1]));
                }
            }
        }

        clauses
    }

    /// Get text for a span (character offsets).
    ///
    /// Converts character offsets to byte offsets for safe extraction.
    ///
    /// # Arguments
    /// * `text` - The original text (must match the text passed to `analyze`)
    /// * `start` - Start character offset (inclusive)
    /// * `end` - End character offset (exclusive)
    ///
    /// # Example
    /// ```rust
    /// use anno::discourse::DiscourseScope;
    ///
    /// let text = "日本語。英語。";
    /// let scope = DiscourseScope::analyze(text);
    ///
    /// // Extract first sentence (chars 0-4 = "日本語。")
    /// if let Some((start, end)) = scope.sentence_at(0) {
    ///     let extracted = scope.extract_span(text, start, end);
    ///     assert!(!extracted.is_empty());
    /// }
    /// ```
    #[must_use]
    pub fn extract_span<'a>(&self, text: &'a str, start: usize, end: usize) -> &'a str {
        let byte_start = self.char_to_byte_offset(start);
        let byte_end = self.char_to_byte_offset(end);
        text.get(byte_start..byte_end).unwrap_or("")
    }

    /// For an anaphor at a given offset, find candidate antecedent spans.
    ///
    /// Returns spans in order of preference (nearest first).
    #[must_use]
    pub fn candidate_antecedent_spans(&self, anaphor_offset: usize) -> Vec<(usize, usize)> {
        let mut candidates = Vec::new();

        // 1. Immediately preceding clause (highest priority for "This")
        let preceding = self.preceding_clauses(anaphor_offset, 3);
        candidates.extend(preceding);

        // 2. Preceding sentences (for longer-distance)
        if let Some((sent_start, _)) = self.sentence_at(anaphor_offset) {
            // Find the previous sentence
            for window in self.sentence_boundaries.windows(2) {
                if window[1] <= sent_start {
                    candidates.push((window[0], window[1]));
                }
            }
        }

        candidates.sort_by_key(|&(start, _)| std::cmp::Reverse(start));
        candidates.dedup();
        candidates
    }
}

// =============================================================================
// Event Coreference Resolution
// =============================================================================

/// A cluster of coreferent event mentions.
///
/// # Research Background
///
/// Event coreference (Ahmed & Martin, 2021) links mentions of the same
/// real-world event across a document. Unlike entity coreference, event
/// coreference must consider:
/// - Trigger words (verbs, nominalizations)
/// - Arguments (who did what to whom)
/// - Temporal/spatial constraints
/// - Event subtypes (attacks, meetings, etc.)
///
/// # Example
///
/// ```rust
/// use anno::discourse::{EventMention, EventCluster};
///
/// let mentions = vec![
///     EventMention::new("invasion", 10, 18)
///         .with_trigger_type("attack"),
///     EventMention::new("invaded", 50, 57)
///         .with_trigger_type("attack"),
///     EventMention::new("attack", 100, 106)
///         .with_trigger_type("attack"),
/// ];
///
/// let cluster = EventCluster::new(mentions);
/// assert_eq!(cluster.len(), 3);
/// assert_eq!(cluster.canonical_trigger(), "invasion");
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct EventCluster {
    /// All event mentions in this cluster
    pub mentions: Vec<EventMention>,
    /// Cluster ID
    pub id: u64,
    /// Canonical event type (e.g., "attack")
    pub event_type: Option<String>,
    /// Confidence in this clustering
    pub confidence: Confidence,
}

impl EventCluster {
    /// Create a new event cluster.
    #[must_use]
    pub fn new(mentions: Vec<EventMention>) -> Self {
        let event_type = mentions
            .iter()
            .filter_map(|m| m.trigger_type.clone())
            .next();

        Self {
            mentions,
            id: 0,
            event_type,
            confidence: Confidence::ONE,
        }
    }

    /// Number of mentions.
    #[must_use]
    pub fn len(&self) -> usize {
        self.mentions.len()
    }

    /// Is the cluster empty?
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.mentions.is_empty()
    }

    /// Get the canonical trigger (first mention's trigger).
    #[must_use]
    pub fn canonical_trigger(&self) -> &str {
        self.mentions
            .first()
            .map(|m| m.trigger.as_str())
            .unwrap_or("")
    }

    /// Set cluster ID.
    #[must_use]
    pub fn with_id(mut self, id: u64) -> Self {
        self.id = id;
        self
    }

    /// Add a mention to the cluster.
    pub fn add(&mut self, mention: EventMention) {
        self.mentions.push(mention);
    }
}

/// Simple event coreference resolver.
///
/// Uses heuristics based on:
/// - Trigger lemma matching (same verb stem)
/// - Event type matching
/// - Argument overlap
///
/// # Limitations
///
/// This rule-based approach is simplistic. For production, use:
/// - BERT-based event coref (Ahmed & Martin, 2021)
/// - Cross-document event coref with LSH blocking
#[derive(Debug, Clone, Default)]
pub struct EventCorefResolver {
    /// Require matching event types
    pub require_type_match: bool,
    /// Minimum argument overlap ratio (0.0 = no requirement)
    pub min_arg_overlap: f64,
}

impl EventCorefResolver {
    /// Create a new resolver with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            require_type_match: true,
            min_arg_overlap: 0.3,
        }
    }

    /// Resolve event coreference, grouping mentions into clusters.
    #[must_use]
    pub fn resolve(&self, mentions: &[EventMention]) -> Vec<EventCluster> {
        if mentions.is_empty() {
            return vec![];
        }

        let mut clusters: Vec<EventCluster> = Vec::new();
        let mut assigned: Vec<bool> = vec![false; mentions.len()];

        for i in 0..mentions.len() {
            if assigned[i] {
                continue;
            }

            // Start a new cluster
            let mut cluster_mentions = vec![mentions[i].clone()];
            assigned[i] = true;

            // Find all mentions that corefer with this one
            for j in (i + 1)..mentions.len() {
                if assigned[j] {
                    continue;
                }

                if self.should_corefer(&mentions[i], &mentions[j]) {
                    cluster_mentions.push(mentions[j].clone());
                    assigned[j] = true;
                }
            }

            clusters.push(EventCluster::new(cluster_mentions).with_id(clusters.len() as u64));
        }

        clusters
    }

    /// Check if two event mentions should corefer.
    fn should_corefer(&self, a: &EventMention, b: &EventMention) -> bool {
        // 1. Check event type match
        if self.require_type_match {
            match (&a.trigger_type, &b.trigger_type) {
                (Some(ta), Some(tb)) if ta != tb => return false,
                _ => {} // OK if either is None or they match
            }
        }

        // 2. Check trigger lemma similarity
        let trigger_match = self.triggers_match(&a.trigger, &b.trigger);
        if !trigger_match {
            return false;
        }

        // 3. Check argument overlap
        if self.min_arg_overlap > 0.0 {
            let overlap = self.compute_arg_overlap(a, b);
            if overlap < self.min_arg_overlap {
                return false;
            }
        }

        true
    }

    /// Very simple stemmer for event triggers.
    ///
    /// Handles common verb-to-noun transformations:
    /// - invade / invaded / invasion -> invad
    /// - attack / attacked / attacking -> attack
    fn simple_stem(&self, word: &str) -> String {
        let mut s = word.to_string();

        // Handle nominalizations first (-ation, -tion, -sion)
        // invasion -> invas, creation -> creat, discussion -> discus
        if s.ends_with("ation") {
            s = s.trim_end_matches("ation").to_string();
            // Add back 'e' for words like create -> creation
            if !s.is_empty() && s.chars().last().map(|c| c.is_alphabetic()).unwrap_or(false) {
                // Don't add e back, just use the stem
            }
        } else if s.ends_with("tion") || s.ends_with("sion") {
            s = s.trim_end_matches("ion").to_string();
        } else if s.ends_with("ing") {
            s = s.trim_end_matches("ing").to_string();
        } else if s.ends_with("ed") && s.len() > 3 {
            s = s.trim_end_matches("ed").to_string();
        } else if s.ends_with("s") && s.len() > 2 && !s.ends_with("ss") {
            s = s.trim_end_matches('s').to_string();
        }

        // Handle doubled consonants (running -> run, invadde -> invad)
        let bytes = s.as_bytes();
        if bytes.len() > 2 && bytes[bytes.len() - 1] == bytes[bytes.len() - 2] {
            s.pop();
        }

        s
    }

    /// Make method public for testing
    pub fn triggers_match(&self, a: &str, b: &str) -> bool {
        let a_lower = a.to_lowercase();
        let b_lower = b.to_lowercase();

        // Exact match
        if a_lower == b_lower {
            return true;
        }

        // Simple stemming
        let stem_a = self.simple_stem(&a_lower);
        let stem_b = self.simple_stem(&b_lower);

        stem_a == stem_b
    }

    /// Compute argument overlap ratio between two event mentions.
    fn compute_arg_overlap(&self, a: &EventMention, b: &EventMention) -> f64 {
        if a.arguments.is_empty() && b.arguments.is_empty() {
            return 1.0; // No arguments = compatible
        }

        let total = a.arguments.len().max(b.arguments.len());
        if total == 0 {
            return 1.0;
        }

        let mut matches = 0;
        for (role_a, val_a) in &a.arguments {
            for (role_b, val_b) in &b.arguments {
                // Same role with similar value
                if role_a == role_b && self.values_similar(val_a, val_b) {
                    matches += 1;
                    break;
                }
            }
        }

        matches as f64 / total as f64
    }

    /// Check if two argument values are similar.
    fn values_similar(&self, a: &str, b: &str) -> bool {
        let a_lower = a.to_lowercase();
        let b_lower = b.to_lowercase();

        a_lower == b_lower || a_lower.contains(&b_lower) || b_lower.contains(&a_lower)
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_referent_types() {
        assert!(!ReferentType::Nominal.is_abstract());
        assert!(ReferentType::Event.is_abstract());
        assert!(ReferentType::Fact.is_abstract());
        assert!(ReferentType::Proposition.is_abstract());
        assert!(ReferentType::Situation.is_abstract());
    }

    #[test]
    fn test_event_mention() {
        let event = EventMention::new("invaded", 7, 14)
            .with_trigger_type("attack")
            .with_arguments(vec![("Agent", "Russia"), ("Patient", "Ukraine")]);

        assert_eq!(event.trigger, "invaded");
        assert_eq!(event.trigger_type.as_deref(), Some("attack"));
        assert_eq!(event.get_argument("Agent"), Some("Russia"));
        assert_eq!(event.get_argument("Patient"), Some("Ukraine"));
        assert_eq!(event.get_argument("Location"), None);
    }

    #[test]
    fn test_discourse_referent() {
        let event = EventMention::new("invaded", 7, 14);
        let referent = DiscourseReferent::new(ReferentType::Event, 0, 30)
            .with_event(event)
            .with_label("Russian invasion");

        assert_eq!(referent.span(), (0, 30));
        assert_eq!(referent.len(), 30);
        assert!(referent.is_abstract());
        assert_eq!(referent.display_text(), "Russian invasion");
    }

    #[test]
    fn test_shell_noun_classification() {
        assert_eq!(
            classify_shell_noun("problem"),
            Some(ShellNounClass::Circumstantial)
        );
        assert_eq!(classify_shell_noun("fact"), Some(ShellNounClass::Factual));
        assert_eq!(classify_shell_noun("idea"), Some(ShellNounClass::Mental));
        assert_eq!(
            classify_shell_noun("possibility"),
            Some(ShellNounClass::Modal)
        );
        assert_eq!(classify_shell_noun("event"), Some(ShellNounClass::Eventive));
        assert_eq!(
            classify_shell_noun("claim"),
            Some(ShellNounClass::Linguistic)
        );
        assert_eq!(classify_shell_noun("foobar"), None);
    }

    #[test]
    fn test_shell_noun_demonstrative() {
        let shell = ShellNoun::new("problem", ShellNounClass::Circumstantial)
            .with_determiner("this")
            .at_span(32, 44);

        assert!(shell.is_demonstrative());

        let shell_the =
            ShellNoun::new("problem", ShellNounClass::Circumstantial).with_determiner("the");

        assert!(!shell_the.is_demonstrative());
    }

    #[test]
    fn test_shell_noun_typical_antecedents() {
        let shell = ShellNoun::new("event", ShellNounClass::Eventive);
        let types = shell.typical_antecedent_types();
        assert!(types.contains(&ReferentType::Event));
        assert!(types.contains(&ReferentType::Situation));
    }

    #[test]
    fn test_from_entity() {
        let entity = Entity::new("Russia", EntityType::Location, 0, 6, 0.95);
        let referent = DiscourseReferent::from_entity(&entity);

        assert_eq!(referent.referent_type, ReferentType::Nominal);
        assert_eq!(referent.start, 0);
        assert_eq!(referent.end, 6);
        assert!(!referent.is_abstract());
    }

    #[test]
    fn test_discourse_scope_sentences() {
        let text = "Russia invaded Ukraine. This caused inflation. The crisis deepened.";
        let scope = DiscourseScope::analyze(text);

        assert_eq!(scope.sentence_count(), 3);
    }

    #[test]
    fn test_discourse_scope_clauses() {
        let text = "Prices rose, and wages fell. This was unsustainable.";
        let scope = DiscourseScope::analyze(text);

        // Should detect: "Prices rose", "and wages fell", sentence boundary, "This was..."
        assert!(scope.clause_count() >= 2);
    }

    #[test]
    fn test_discourse_scope_preceding() {
        let text = "Russia invaded Ukraine. This caused inflation.";
        let scope = DiscourseScope::analyze(text);

        // "This" is at position 24
        let preceding = scope.preceding_clauses(24, 2);
        assert!(!preceding.is_empty(), "Should find preceding clauses");
    }

    #[test]
    fn test_candidate_antecedent_spans() {
        let text = "Russia invaded Ukraine in 2022. This caused a global energy crisis.";
        let scope = DiscourseScope::analyze(text);

        // "This" is at position 32
        let candidates = scope.candidate_antecedent_spans(32);
        assert!(!candidates.is_empty(), "Should find candidate spans");

        // The first sentence should be a candidate
        let first_sentence = scope.extract_span(text, candidates[0].0, candidates[0].1);
        assert!(
            first_sentence.contains("invaded"),
            "First candidate should include the invasion"
        );
    }

    // Event coreference tests

    #[test]
    fn test_event_cluster_creation() {
        let mentions = vec![
            EventMention::new("invasion", 10, 18).with_trigger_type("attack"),
            EventMention::new("invaded", 50, 57).with_trigger_type("attack"),
        ];

        let cluster = EventCluster::new(mentions);
        assert_eq!(cluster.len(), 2);
        assert_eq!(cluster.canonical_trigger(), "invasion");
        assert_eq!(cluster.event_type, Some("attack".to_string()));
    }

    #[test]
    fn test_event_coref_resolver_simple() {
        let resolver = EventCorefResolver::new();

        let mentions = vec![
            // Two attack events (same trigger stem, should cluster)
            EventMention::new("attacked", 10, 18)
                .with_trigger_type("attack")
                .with_arguments(vec![("Agent", "Russia"), ("Patient", "Ukraine")]),
            EventMention::new("attack", 50, 56)
                .with_trigger_type("attack")
                .with_arguments(vec![("Agent", "Russia")]),
            // One meeting event (different type, should not cluster with attacks)
            EventMention::new("meeting", 100, 107)
                .with_trigger_type("meeting")
                .with_arguments(vec![("Participant", "leaders")]),
        ];

        let clusters = resolver.resolve(&mentions);

        // Should have 2 clusters: attack events + meeting event
        assert_eq!(clusters.len(), 2, "Expected 2 clusters");

        // First cluster should have the attack mentions
        let attack_cluster = &clusters[0];
        assert_eq!(
            attack_cluster.len(),
            2,
            "Attack cluster should have 2 mentions"
        );

        // Second cluster should have the meeting mention
        let meeting_cluster = &clusters[1];
        assert_eq!(
            meeting_cluster.len(),
            1,
            "Meeting cluster should have 1 mention"
        );
    }

    #[test]
    fn test_event_coref_trigger_matching() {
        let resolver = EventCorefResolver::new();

        // These should match (same stem or exact)
        assert!(resolver.triggers_match("attack", "attack"));
        assert!(resolver.triggers_match("attack", "attacks"));
        assert!(resolver.triggers_match("attack", "attacked"));
        assert!(resolver.triggers_match("attack", "attacking"));

        // These should not match (different events)
        assert!(!resolver.triggers_match("attack", "meeting"));
        assert!(!resolver.triggers_match("invade", "defend"));
    }

    // =================================================================
    // Additional Edge Case Tests
    // =================================================================

    #[test]
    fn test_empty_text_discourse_scope() {
        let scope = DiscourseScope::analyze("");
        assert_eq!(scope.sentence_count(), 0);
        assert_eq!(scope.clause_count(), 0);
    }

    #[test]
    fn test_single_word_text() {
        let scope = DiscourseScope::analyze("Hello");
        // Single word with no period should still have structure
        assert!(scope.sentence_boundaries.len() >= 2); // start and end
    }

    #[test]
    fn test_abbreviation_handling() {
        let text = "Dr. Smith went to the U.S. embassy. He met with officials.";
        let scope = DiscourseScope::analyze(text);
        // Should detect 2 sentences, not split on Dr. or U.S.
        // (Note: our simple heuristic may not handle all abbreviations)
        assert!(scope.sentence_count() >= 1);
    }

    #[test]
    fn test_shell_noun_case_insensitive() {
        assert_eq!(classify_shell_noun("FACT"), Some(ShellNounClass::Factual));
        assert_eq!(
            classify_shell_noun("Problem"),
            Some(ShellNounClass::Circumstantial)
        );
        assert_eq!(classify_shell_noun("IDEA"), Some(ShellNounClass::Mental));
    }

    #[test]
    fn test_event_mention_empty_arguments() {
        let event = EventMention::new("attacked", 0, 8);
        assert!(event.arguments.is_empty());
        assert_eq!(event.get_argument("Agent"), None);
    }

    #[test]
    fn test_discourse_referent_empty_span() {
        let referent = DiscourseReferent::new(ReferentType::Event, 5, 5);
        assert!(referent.is_empty());
        assert_eq!(referent.len(), 0);
    }

    #[test]
    fn test_event_polarity_variants() {
        let positive = EventMention::new("attacked", 0, 8).with_polarity(EventPolarity::Positive);
        let negative = EventMention::new("attacked", 0, 8).with_polarity(EventPolarity::Negative);
        let uncertain = EventMention::new("attacked", 0, 8).with_polarity(EventPolarity::Uncertain);

        assert_eq!(positive.polarity, EventPolarity::Positive);
        assert_eq!(negative.polarity, EventPolarity::Negative);
        assert_eq!(uncertain.polarity, EventPolarity::Uncertain);
    }

    #[test]
    fn test_event_tense_variants() {
        let past = EventMention::new("attacked", 0, 8).with_tense(EventTense::Past);
        let future = EventMention::new("will attack", 0, 11).with_tense(EventTense::Future);

        assert_eq!(past.tense, Some(EventTense::Past));
        assert_eq!(future.tense, Some(EventTense::Future));
    }

    #[test]
    fn test_event_cluster_empty() {
        let cluster = EventCluster::new(vec![]);
        assert!(cluster.is_empty());
        assert_eq!(cluster.len(), 0);
        assert_eq!(cluster.canonical_trigger(), "");
    }

    #[test]
    fn test_event_coref_empty_input() {
        let resolver = EventCorefResolver::new();
        let clusters = resolver.resolve(&[]);
        assert!(clusters.is_empty());
    }

    #[test]
    fn test_event_coref_single_mention() {
        let resolver = EventCorefResolver::new();
        let mentions = vec![EventMention::new("attacked", 0, 8).with_trigger_type("attack")];
        let clusters = resolver.resolve(&mentions);
        assert_eq!(clusters.len(), 1);
        assert_eq!(clusters[0].len(), 1);
    }

    #[test]
    fn test_referent_type_can_be_antecedent() {
        // "this" can refer to all abstract types
        assert!(ReferentType::Event.can_be_this_antecedent());
        assert!(ReferentType::Fact.can_be_this_antecedent());
        assert!(ReferentType::Proposition.can_be_this_antecedent());
        assert!(!ReferentType::Nominal.can_be_this_antecedent());

        // "it" is more restricted
        assert!(ReferentType::Event.can_be_it_antecedent());
        assert!(ReferentType::Nominal.can_be_it_antecedent());
        assert!(!ReferentType::Fact.can_be_it_antecedent());
    }

    #[test]
    fn test_discourse_scope_sentence_at() {
        let text = "First sentence. Second sentence. Third.";
        let scope = DiscourseScope::analyze(text);

        // Check sentence_at for different positions
        let sent1 = scope.sentence_at(5); // "First"
        assert!(sent1.is_some());

        let sent2 = scope.sentence_at(20); // "Second"
        assert!(sent2.is_some());
    }

    #[test]
    fn test_discourse_scope_clause_at() {
        let text = "Prices rose, and wages fell.";
        let scope = DiscourseScope::analyze(text);

        let clause = scope.clause_at(5); // "Prices"
        assert!(clause.is_some());
    }

    #[test]
    fn test_shell_noun_all_classes() {
        // Test at least one noun from each class
        let tests = vec![
            ("fact", ShellNounClass::Factual),
            ("claim", ShellNounClass::Linguistic),
            ("idea", ShellNounClass::Mental),
            ("possibility", ShellNounClass::Modal),
            ("event", ShellNounClass::Eventive),
            ("situation", ShellNounClass::Circumstantial),
        ];

        for (noun, expected_class) in tests {
            let result = classify_shell_noun(noun);
            assert_eq!(result, Some(expected_class), "Failed for noun: {}", noun);
        }
    }
}