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
//! Discourse Deixis Resolution.
//!
//! # Overview
//!
//! Discourse deixis refers to expressions that point to propositions, facts,
//! or discourse segments rather than entities. Unlike entity coreference,
//! the antecedent is not a noun phrase but a clause, sentence, or discourse unit.
//!
//! # Example
//!
//! ```text
//! "The stock crashed 40%. That was unexpected."
//! ^^^^ discourse deictic
//! antecedent: "The stock crashed 40%" (event/proposition)
//! ```
//!
//! # Discourse Deixis vs Entity Coreference
//!
//! | Aspect | Entity Coreference | Discourse Deixis |
//! |--------|-------------------|------------------|
//! | Antecedent | NP (entity) | Clause/proposition |
//! | Anaphor | pronouns, definite NPs | "this", "that", "it" |
//! | Semantic | Identity | Reference to content |
//!
//! # ARRAU Annotation
//!
//! ARRAU is one of the few resources that explicitly annotates discourse deixis
//! alongside identity coreference and bridging.
//!
//! # References
//!
//! - Webber (1991): "Structure and Ostension in the Interpretation of Discourse Deixis"
//! - Poesio et al. (2024): "ARRAU 3.0"
use anno::offset::TextSpan;
use serde::{Deserialize, Serialize};
/// Type of discourse deictic expression.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum DeicticType {
/// Demonstrative pronoun: "this", "that"
#[default]
Demonstrative,
/// Pronoun "it" with propositional antecedent
It,
/// "So" in constructions like "I think so"
So,
/// Null complement (elided clause)
NullComplement,
/// Other deictic expression
Other(String),
}
/// Type of antecedent for discourse deixis.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum DiscourseAntecedentType {
/// Single clause
#[default]
Clause,
/// Full sentence
Sentence,
/// Multiple sentences
MultiSentence,
/// Verb phrase
VerbPhrase,
/// Event description
Event,
/// Proposition/fact
Proposition,
/// Abstract entity (e.g., "the situation")
AbstractEntity,
/// Implicit (must be inferred from context)
Implicit,
}
/// A discourse deictic expression.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscourseDeictic {
/// The deictic expression (e.g., "that")
pub text: String,
/// Start offset
pub start: usize,
/// End offset
pub end: usize,
/// Type of deictic
pub deictic_type: DeicticType,
/// Sentence index containing the deictic
pub sentence_idx: Option<usize>,
}
impl DiscourseDeictic {
/// Create a new discourse deictic.
pub fn new(text: &str, start: usize, end: usize) -> Self {
let deictic_type = match text.to_lowercase().as_str() {
"this" | "that" | "these" | "those" => DeicticType::Demonstrative,
"it" => DeicticType::It,
"so" => DeicticType::So,
_ => DeicticType::Other(text.to_string()),
};
Self {
text: text.to_string(),
start,
end,
deictic_type,
sentence_idx: None,
}
}
/// Set the sentence index.
pub fn with_sentence(mut self, idx: usize) -> Self {
self.sentence_idx = Some(idx);
self
}
}
/// Antecedent for discourse deixis (typically a clause or proposition).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscourseAntecedent {
/// The antecedent text
pub text: String,
/// Start offset
pub start: usize,
/// End offset
pub end: usize,
/// Type of antecedent
pub antecedent_type: DiscourseAntecedentType,
/// Sentence indices covered (for multi-sentence antecedents)
pub sentence_indices: Vec<usize>,
}
impl DiscourseAntecedent {
/// Create a new antecedent.
pub fn new(text: &str, start: usize, end: usize) -> Self {
Self {
text: text.to_string(),
start,
end,
antecedent_type: DiscourseAntecedentType::Clause,
sentence_indices: Vec::new(),
}
}
/// Set the antecedent type.
pub fn with_type(mut self, ant_type: DiscourseAntecedentType) -> Self {
self.antecedent_type = ant_type;
self
}
/// Set sentence indices.
pub fn with_sentences(mut self, indices: Vec<usize>) -> Self {
self.sentence_indices = indices;
self
}
}
/// A resolved discourse deixis link.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscourseDeicticLink {
/// The deictic expression
pub deictic: DiscourseDeictic,
/// The antecedent (discourse segment)
pub antecedent: DiscourseAntecedent,
/// Confidence in this resolution
pub confidence: f64,
}
impl DiscourseDeicticLink {
/// Create a new link.
pub fn new(deictic: DiscourseDeictic, antecedent: DiscourseAntecedent) -> Self {
Self {
deictic,
antecedent,
confidence: 1.0,
}
}
/// Set confidence.
pub fn with_confidence(mut self, confidence: f64) -> Self {
self.confidence = confidence;
self
}
/// Check if this is a demonstrative deixis.
pub fn is_demonstrative(&self) -> bool {
matches!(self.deictic.deictic_type, DeicticType::Demonstrative)
}
/// Check if antecedent spans multiple sentences.
pub fn is_multi_sentence(&self) -> bool {
matches!(
self.antecedent.antecedent_type,
DiscourseAntecedentType::MultiSentence
) || self.antecedent.sentence_indices.len() > 1
}
}
/// Document with discourse deixis annotations.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DiscourseDeicticDocument {
/// Document ID
pub id: String,
/// Document text
pub text: String,
/// Discourse deixis links
pub links: Vec<DiscourseDeicticLink>,
}
impl DiscourseDeicticDocument {
/// Create a new document.
pub fn new(id: &str, text: &str) -> Self {
Self {
id: id.to_string(),
text: text.to_string(),
links: Vec::new(),
}
}
/// Add a link.
pub fn add_link(&mut self, link: DiscourseDeicticLink) {
self.links.push(link);
}
/// Number of links.
pub fn len(&self) -> usize {
self.links.len()
}
/// Check if empty.
pub fn is_empty(&self) -> bool {
self.links.is_empty()
}
/// Get demonstrative deixis links.
pub fn demonstratives(&self) -> Vec<&DiscourseDeicticLink> {
self.links.iter().filter(|l| l.is_demonstrative()).collect()
}
/// Get multi-sentence antecedent links.
pub fn multi_sentence(&self) -> Vec<&DiscourseDeicticLink> {
self.links
.iter()
.filter(|l| l.is_multi_sentence())
.collect()
}
}
/// Simple rule-based discourse deixis detector.
pub struct DiscourseDeicticDetector {
/// Patterns that indicate propositional "it"
propositional_it_contexts: Vec<&'static str>,
}
impl Default for DiscourseDeicticDetector {
fn default() -> Self {
Self::new()
}
}
impl DiscourseDeicticDetector {
/// Create a new detector.
pub fn new() -> Self {
Self {
propositional_it_contexts: vec![
"it is clear that",
"it seems that",
"it appears that",
"it is obvious that",
"it is surprising that",
"it is important that",
"it is true that",
"it is a fact that",
"it follows that",
"it means that",
],
}
}
/// Detect potential discourse deictics in text.
///
/// Note: This is a heuristic detector. Full resolution requires
/// syntactic parsing and semantic analysis.
pub fn detect(&self, text: &str) -> Vec<DiscourseDeictic> {
let mut deictics = Vec::new();
// Detect demonstratives that likely refer to propositions
// Pattern: "That + verb" at sentence start or after punctuation
let sentence_initial_that =
regex::Regex::new(r"(?i)(?:^|[.!?]\s+)(that)\s+(?:was|is|seems|appears|means|shows)")
.ok();
if let Some(re) = sentence_initial_that {
for cap in re.captures_iter(text) {
if let Some(m) = cap.get(1) {
let span = TextSpan::from_bytes(text, m.start(), m.end());
let original = span.extract(text);
deictics.push(DiscourseDeictic::new(
original,
span.char_start,
span.char_end,
));
}
}
}
// Detect "this" that refers to prior discourse
// Pattern: "This + verb" (not followed by noun)
let this_propositional =
regex::Regex::new(r"(?i)\b(this)\s+(?:is|was|means|suggests|shows|indicates|explains)")
.ok();
if let Some(re) = this_propositional {
for cap in re.captures_iter(text) {
if let Some(m) = cap.get(1) {
let span = TextSpan::from_bytes(text, m.start(), m.end());
let original = span.extract(text);
deictics.push(DiscourseDeictic::new(
original,
span.char_start,
span.char_end,
));
}
}
}
// Detect propositional "it"
for pattern in &self.propositional_it_contexts {
let pattern_len = pattern.len();
if pattern_len == 0 {
continue;
}
// ASCII-only patterns; use index-preserving ASCII case-insensitive scan.
for (idx, _) in text.char_indices() {
let Some(hay) = text.get(idx..idx + pattern_len) else {
continue;
};
if !hay.eq_ignore_ascii_case(pattern) {
continue;
}
// Find "it" within the pattern (byte offsets; "it" is ASCII)
if let Some(it_pos) = pattern.find("it") {
let it_start = idx + it_pos;
let it_end = it_start + 2;
let span = TextSpan::from_bytes(text, it_start, it_end);
let original = span.extract(text);
deictics.push(DiscourseDeictic::new(
original,
span.char_start,
span.char_end,
));
}
}
}
// Sort by position and deduplicate
deictics.sort_by_key(|d| d.start);
deictics.dedup_by(|a, b| a.start == b.start);
deictics
}
}
/// Evaluation metrics for discourse deixis resolution.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DiscourseDeicticMetrics {
/// Precision
pub precision: f64,
/// Recall
pub recall: f64,
/// F1 score
pub f1: f64,
/// Accuracy on deictic type classification
pub type_accuracy: f64,
}
#[cfg(test)]
mod tests {
use super::*;
use anno::offset::TextSpan;
#[test]
fn test_deictic_type() {
let deictic = DiscourseDeictic::new("that", 0, 4);
assert_eq!(deictic.deictic_type, DeicticType::Demonstrative);
let deictic = DiscourseDeictic::new("it", 0, 2);
assert_eq!(deictic.deictic_type, DeicticType::It);
}
#[test]
fn test_discourse_antecedent() {
let antecedent = DiscourseAntecedent::new("The stock crashed 40%", 0, 21)
.with_type(DiscourseAntecedentType::Event)
.with_sentences(vec![0]);
assert_eq!(antecedent.antecedent_type, DiscourseAntecedentType::Event);
assert_eq!(antecedent.sentence_indices, vec![0]);
}
#[test]
fn test_discourse_deictic_link() {
let deictic = DiscourseDeictic::new("That", 23, 27).with_sentence(1);
let antecedent = DiscourseAntecedent::new("The stock crashed 40%", 0, 21)
.with_type(DiscourseAntecedentType::Event);
let link = DiscourseDeicticLink::new(deictic, antecedent);
assert!(link.is_demonstrative());
assert!(!link.is_multi_sentence());
}
#[test]
fn test_detector() {
let detector = DiscourseDeicticDetector::new();
let text = "The company went bankrupt. That was unexpected.";
let deictics = detector.detect(text);
// Should detect "That" as likely propositional
assert!(!deictics.is_empty());
}
#[test]
fn test_document() {
let mut doc = DiscourseDeicticDocument::new("doc1", "Event happened. That was surprising.");
let deictic = DiscourseDeictic::new("That", 16, 20);
let antecedent = DiscourseAntecedent::new("Event happened", 0, 14);
doc.add_link(DiscourseDeicticLink::new(deictic, antecedent));
assert_eq!(doc.len(), 1);
assert_eq!(doc.demonstratives().len(), 1);
}
#[test]
fn test_detector_offsets_are_character_offsets_on_unicode_text() {
// Mixed-script text ensures we don't accidentally treat byte offsets as char offsets.
let text = "東京で事件が起きた。That was unexpected. Müller noted: This means delays.";
let detector = DiscourseDeicticDetector::new();
let deictics = detector.detect(text);
assert!(!deictics.is_empty(), "expected at least one deictic");
for d in deictics {
let extracted = TextSpan::from_chars(text, d.start, d.end).extract(text);
assert_eq!(
extracted, d.text,
"deictic span should round-trip via char offsets"
);
}
}
}