scirs2-text 0.4.3

Text processing module for SciRS2 (scirs2-text)
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
//! Temporal relation extraction and TIMEX3-style normalisation.
//!
//! This module detects time expressions in text, classifies them using a
//! TIMEX3-like taxonomy, normalises them to ISO 8601, and infers ordering
//! relations between pairs of expressions.
//!
//! # Overview
//!
//! - [`TimeType`] — DATE / TIME / DURATION / SET
//! - [`TemporalRelation`] — BEFORE / AFTER / INCLUDES / IS_INCLUDED /
//!   SIMULTANEOUS / VAGUE
//! - [`TimeExpression`] — a located time expression with parsed value
//! - [`extract_time_expressions`] — TIMEX3-like extraction from raw text
//! - [`normalize_timex`] — normalise to ISO 8601 / TIMEX3 value string
//! - [`temporal_ordering`] — classify the relation between two expressions

use crate::error::{Result, TextError};
use crate::temporal::temporal_patterns::{
    month_name_to_number, word_to_number, AbsoluteDatePattern, DurationPattern, FrequencyPattern,
    RelativeTimePattern,
};
use regex::Regex;
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// TimeType
// ---------------------------------------------------------------------------

/// TIMEX3-style coarse classification of a temporal expression.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TimeType {
    /// A calendar date, possibly partial (year-only, month-year, etc.)
    Date,
    /// A time of day (clock time, AM/PM, etc.)
    Time,
    /// A span of time (e.g. "three weeks", "for two hours")
    Duration,
    /// A recurring / habitual time reference (e.g. "every Monday")
    Set,
}

impl std::fmt::Display for TimeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TimeType::Date => write!(f, "DATE"),
            TimeType::Time => write!(f, "TIME"),
            TimeType::Duration => write!(f, "DURATION"),
            TimeType::Set => write!(f, "SET"),
        }
    }
}

// ---------------------------------------------------------------------------
// TemporalRelation
// ---------------------------------------------------------------------------

/// Allen interval algebra (simplified) relation between two time expressions.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TemporalRelation {
    /// t1 ends before t2 begins.
    Before,
    /// t1 begins after t2 ends.
    After,
    /// t1 contains t2 (t1.start ≤ t2.start ∧ t1.end ≥ t2.end).
    Includes,
    /// t1 is contained within t2.
    IsIncluded,
    /// t1 and t2 overlap or co-occur.
    Simultaneous,
    /// Relation cannot be determined from available information.
    Vague,
}

impl std::fmt::Display for TemporalRelation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TemporalRelation::Before => write!(f, "BEFORE"),
            TemporalRelation::After => write!(f, "AFTER"),
            TemporalRelation::Includes => write!(f, "INCLUDES"),
            TemporalRelation::IsIncluded => write!(f, "IS_INCLUDED"),
            TemporalRelation::Simultaneous => write!(f, "SIMULTANEOUS"),
            TemporalRelation::Vague => write!(f, "VAGUE"),
        }
    }
}

// ---------------------------------------------------------------------------
// TimeExpression
// ---------------------------------------------------------------------------

/// A time expression detected in source text, with its span, type, and value.
#[derive(Debug, Clone, PartialEq)]
pub struct TimeExpression {
    /// Raw text as it appears in the source.
    pub text: String,
    /// Byte offset of the start of the match.
    pub start: usize,
    /// Byte offset of the end (exclusive) of the match.
    pub end: usize,
    /// Coarse TIMEX3 category.
    pub time_type: TimeType,
    /// Normalised ISO 8601 / TIMEX3 value string (may be empty if unknown).
    pub value: String,
    /// Optional confidence in [0, 1].
    pub confidence: f64,
}

impl TimeExpression {
    /// Create a new `TimeExpression`.
    pub fn new(
        text: impl Into<String>,
        start: usize,
        end: usize,
        time_type: TimeType,
        value: impl Into<String>,
        confidence: f64,
    ) -> Self {
        TimeExpression {
            text: text.into(),
            start,
            end,
            time_type,
            value: value.into(),
            confidence,
        }
    }
}

// ---------------------------------------------------------------------------
// Internal extraction state
// ---------------------------------------------------------------------------

struct Extractor {
    abs: AbsoluteDatePattern,
    rel: RelativeTimePattern,
    dur: DurationPattern,
    freq: FrequencyPattern,
    time_of_day: Regex,
}

impl Extractor {
    fn new() -> Result<Self> {
        let time_of_day = Regex::new(
            r"(?i)\b((\d{1,2}):(\d{2})(\s*(?:AM|PM|am|pm))?|noon|midnight|dawn|dusk|sunrise|sunset|midday|morning|afternoon|evening|night)\b",
        )
        .map_err(|e| TextError::ProcessingError(format!("Regex compile error: {e}")))?;
        Ok(Extractor {
            abs: AbsoluteDatePattern::new()?,
            rel: RelativeTimePattern::new()?,
            dur: DurationPattern::new()?,
            freq: FrequencyPattern::new()?,
            time_of_day,
        })
    }
}

// ---------------------------------------------------------------------------
// extract_time_expressions
// ---------------------------------------------------------------------------

/// Extract TIMEX3-like time expressions from `text`.
///
/// Returns a list of [`TimeExpression`] instances sorted by start offset,
/// with overlapping spans de-duplicated (longer match wins).
///
/// # Example
///
/// ```rust
/// use scirs2_text::temporal::temporal_relations::extract_time_expressions;
///
/// let exprs = extract_time_expressions("The conference was held on 15 March 2024 for two days.");
/// assert!(!exprs.is_empty());
/// ```
pub fn extract_time_expressions(text: &str) -> Vec<TimeExpression> {
    let ext = match Extractor::new() {
        Ok(e) => e,
        Err(_) => return Vec::new(),
    };

    let mut results: Vec<TimeExpression> = Vec::new();

    // --- Absolute dates ---
    for m in ext.abs.find_all(text) {
        let value = normalise_absolute_date(&m.matched_text);
        results.push(TimeExpression::new(
            &m.matched_text,
            m.start,
            m.end,
            TimeType::Date,
            value,
            0.9,
        ));
    }

    // --- Relative references ---
    for m in ext.rel.find_all(text) {
        results.push(TimeExpression::new(
            &m.matched_text,
            m.start,
            m.end,
            TimeType::Date,
            format!("RELATIVE:{}", m.pattern_name),
            0.75,
        ));
    }

    // --- Durations ---
    for m in ext.dur.find_all(text) {
        let value = normalise_duration(&m.matched_text);
        results.push(TimeExpression::new(
            &m.matched_text,
            m.start,
            m.end,
            TimeType::Duration,
            value,
            0.8,
        ));
    }

    // --- Frequencies (SET) ---
    for m in ext.freq.find_all(text) {
        let value = normalise_frequency(&m.matched_text);
        results.push(TimeExpression::new(
            &m.matched_text,
            m.start,
            m.end,
            TimeType::Set,
            value,
            0.8,
        ));
    }

    // --- Time of day ---
    for m in ext.time_of_day.find_iter(text) {
        results.push(TimeExpression::new(
            m.as_str(),
            m.start(),
            m.end(),
            TimeType::Time,
            normalise_time_of_day(m.as_str()),
            0.85,
        ));
    }

    // Sort by start, prefer longer match on tie.
    results.sort_by(|a, b| a.start.cmp(&b.start).then_with(|| b.end.cmp(&a.end)));

    // De-overlap: keep the first (longest) in each cluster.
    let mut deduped: Vec<TimeExpression> = Vec::new();
    let mut last_end = 0usize;
    for expr in results {
        if expr.start >= last_end {
            last_end = expr.end;
            deduped.push(expr);
        }
    }
    deduped
}

// ---------------------------------------------------------------------------
// normalize_timex
// ---------------------------------------------------------------------------

/// Normalise a [`TimeExpression`] to an ISO 8601 / TIMEX3 value string.
///
/// For expressions that already carry a normalised `value` field the stored
/// value is returned.  For relative references the `reference_date` (format
/// `YYYY-MM-DD`) is used as the anchor.
///
/// # Example
///
/// ```rust
/// use scirs2_text::temporal::temporal_relations::{extract_time_expressions, normalize_timex};
///
/// let exprs = extract_time_expressions("The event happened on 2024-06-01.");
/// let norm = normalize_timex(&exprs[0], "2024-01-01");
/// assert!(!norm.is_empty());
/// ```
pub fn normalize_timex(expr: &TimeExpression, reference_date: &str) -> String {
    // If already normalised to ISO form, return as-is.
    if looks_like_iso(&expr.value) {
        return expr.value.clone();
    }

    // Handle RELATIVE: tags.
    if expr.value.starts_with("RELATIVE:") {
        return resolve_relative(&expr.text, reference_date);
    }

    // Duration / frequency — the stored value is the TIMEX3 duration string.
    if expr.time_type == TimeType::Duration || expr.time_type == TimeType::Set {
        if !expr.value.is_empty() {
            return expr.value.clone();
        }
    }

    // Fallback: return raw text.
    expr.text.clone()
}

// ---------------------------------------------------------------------------
// temporal_ordering
// ---------------------------------------------------------------------------

/// Classify the temporal relation between two time expressions.
///
/// Uses a simple rule-based approach: if both values are ISO-8601 date strings
/// they are compared lexicographically.  Otherwise a heuristic is applied.
///
/// # Example
///
/// ```rust
/// use scirs2_text::temporal::temporal_relations::{
///     extract_time_expressions, temporal_ordering, TemporalRelation,
/// };
///
/// let exprs = extract_time_expressions("First in 2020, then in 2022.");
/// if exprs.len() >= 2 {
///     let rel = temporal_ordering(&exprs[0], &exprs[1]);
///     assert_ne!(rel, TemporalRelation::Vague);
/// }
/// ```
pub fn temporal_ordering(a: &TimeExpression, b: &TimeExpression) -> TemporalRelation {
    // Both normalised to ISO dates → direct comparison.
    if looks_like_iso(&a.value) && looks_like_iso(&b.value) {
        return compare_iso_dates(&a.value, &b.value);
    }

    // Duration contained in a date range.
    if a.time_type == TimeType::Duration && b.time_type == TimeType::Date {
        return TemporalRelation::IsIncluded;
    }
    if a.time_type == TimeType::Date && b.time_type == TimeType::Duration {
        return TemporalRelation::Includes;
    }

    // Frequency / set is considered simultaneous with its anchor period.
    if a.time_type == TimeType::Set || b.time_type == TimeType::Set {
        return TemporalRelation::Simultaneous;
    }

    // Time-of-day within the same date → simultaneous.
    if a.time_type == TimeType::Time && b.time_type == TimeType::Time {
        return TemporalRelation::Simultaneous;
    }

    TemporalRelation::Vague
}

// ---------------------------------------------------------------------------
// Internal normalisation helpers
// ---------------------------------------------------------------------------

fn looks_like_iso(s: &str) -> bool {
    // Match YYYY, YYYY-MM, or YYYY-MM-DD.
    let re_result = Regex::new(r"^\d{4}(-\d{2}(-\d{2})?)?$");
    match re_result {
        Ok(re) => re.is_match(s),
        Err(_) => false,
    }
}

fn compare_iso_dates(a: &str, b: &str) -> TemporalRelation {
    // Pad short forms (YYYY → YYYY-01-01) to make them comparable.
    let pad = |s: &str| -> String {
        if s.len() == 4 {
            format!("{}-01-01", s)
        } else if s.len() == 7 {
            format!("{}-01", s)
        } else {
            s.to_owned()
        }
    };
    let a_padded = pad(a);
    let b_padded = pad(b);
    match a_padded.cmp(&b_padded) {
        std::cmp::Ordering::Less => TemporalRelation::Before,
        std::cmp::Ordering::Greater => TemporalRelation::After,
        std::cmp::Ordering::Equal => TemporalRelation::Simultaneous,
    }
}

fn normalise_absolute_date(text: &str) -> String {
    // ISO 8601 passthrough.
    let iso_re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").expect("static pattern");
    if iso_re.is_match(text) {
        return text.to_owned();
    }

    // Year only.
    let year_only = Regex::new(r"^\d{4}$").expect("static pattern");
    if year_only.is_match(text) {
        return text.to_owned();
    }

    // US long format: "March 15, 2024"
    let us_re = Regex::new(
        r"(?i)^(January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\.?\s+(\d{1,2}),?\s+(\d{4})$",
    )
    .expect("static pattern");
    if let Some(caps) = us_re.captures(text) {
        let month = caps.get(1).map_or("", |m| m.as_str());
        let day = caps.get(2).map_or("", |m| m.as_str());
        let year = caps.get(3).map_or("", |m| m.as_str());
        if let Some(m) = month_name_to_number(month) {
            return format!("{}-{:02}-{:02}", year, m, day.parse::<u8>().unwrap_or(1));
        }
    }

    // EU long format: "15 March 2024"
    let eu_re = Regex::new(
        r"(?i)^(\d{1,2})\s+(January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\.?\s+(\d{4})$",
    )
    .expect("static pattern");
    if let Some(caps) = eu_re.captures(text) {
        let day = caps.get(1).map_or("", |m| m.as_str());
        let month = caps.get(2).map_or("", |m| m.as_str());
        let year = caps.get(3).map_or("", |m| m.as_str());
        if let Some(m) = month_name_to_number(month) {
            return format!("{}-{:02}-{:02}", year, m, day.parse::<u8>().unwrap_or(1));
        }
    }

    // Month-year: "March 2024"
    let my_re = Regex::new(
        r"(?i)^(January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\.?\s+(\d{4})$",
    )
    .expect("static pattern");
    if let Some(caps) = my_re.captures(text) {
        let month = caps.get(1).map_or("", |m| m.as_str());
        let year = caps.get(2).map_or("", |m| m.as_str());
        if let Some(m) = month_name_to_number(month) {
            return format!("{}-{:02}", year, m);
        }
    }

    text.to_owned()
}

/// Convert a duration expression to an ISO 8601 / TIMEX3 duration string (PxYxMxDTxHxMxS).
fn normalise_duration(text: &str) -> String {
    // Already ISO duration.
    let iso = Regex::new(r"^P").expect("static");
    if iso.is_match(text) {
        return text.to_owned();
    }

    let for_re = Regex::new(
        r"(?i)for\s+(\w+)\s+(second|minute|hour|day|week|month|year)s?",
    )
    .expect("static");
    if let Some(caps) = for_re.captures(text) {
        let qty_str = caps.get(1).map_or("", |m| m.as_str());
        let unit = caps.get(2).map_or("", |m| m.as_str()).to_lowercase();
        let qty: u32 = qty_str
            .parse()
            .ok()
            .or_else(|| word_to_number(qty_str))
            .unwrap_or(1);
        return duration_to_iso(qty, &unit);
    }

    // Attributive: "two-hour"
    let attr = Regex::new(r"(?i)(\w+)-(second|minute|hour|day|week|month|year)").expect("static");
    if let Some(caps) = attr.captures(text) {
        let qty_str = caps.get(1).map_or("", |m| m.as_str());
        let unit = caps.get(2).map_or("", |m| m.as_str()).to_lowercase();
        let qty: u32 = qty_str
            .parse()
            .ok()
            .or_else(|| word_to_number(qty_str))
            .unwrap_or(1);
        return duration_to_iso(qty, &unit);
    }

    format!("DURATION:{}", text)
}

fn duration_to_iso(qty: u32, unit: &str) -> String {
    match unit {
        "second" => format!("PT{}S", qty),
        "minute" => format!("PT{}M", qty),
        "hour" => format!("PT{}H", qty),
        "day" => format!("P{}D", qty),
        "week" => format!("P{}W", qty),
        "month" => format!("P{}M", qty),
        "year" => format!("P{}Y", qty),
        _ => format!("P{}{}", qty, unit.chars().next().unwrap_or('X')),
    }
}

/// Convert a frequency expression to a TIMEX3 SET string.
fn normalise_frequency(text: &str) -> String {
    let lower = text.to_lowercase();
    if lower.contains("daily") || lower.contains("every day") || lower.contains("each day") {
        return "FREQ=DAILY".to_owned();
    }
    if lower.contains("weekly") || lower.contains("every week") || lower.contains("each week") {
        return "FREQ=WEEKLY".to_owned();
    }
    if lower.contains("monthly") || lower.contains("every month") {
        return "FREQ=MONTHLY".to_owned();
    }
    if lower.contains("annually") || lower.contains("yearly") || lower.contains("every year") {
        return "FREQ=YEARLY".to_owned();
    }
    if lower.contains("hourly") || lower.contains("every hour") {
        return "FREQ=HOURLY".to_owned();
    }
    format!("SET:{}", text)
}

fn normalise_time_of_day(text: &str) -> String {
    let lower = text.to_lowercase();
    match lower.as_str() {
        "noon" | "midday" => return "T12:00".to_owned(),
        "midnight" => return "T00:00".to_owned(),
        "dawn" | "sunrise" => return "T06:00".to_owned(),
        "dusk" | "sunset" => return "T18:00".to_owned(),
        "morning" => return "TMO".to_owned(),
        "afternoon" => return "TAF".to_owned(),
        "evening" => return "TEV".to_owned(),
        "night" => return "TNI".to_owned(),
        _ => {}
    }

    // HH:MM AM/PM
    let clock = Regex::new(r"(?i)(\d{1,2}):(\d{2})\s*(AM|PM)?").expect("static");
    if let Some(caps) = clock.captures(text) {
        let h: u8 = caps
            .get(1)
            .and_then(|m| m.as_str().parse().ok())
            .unwrap_or(0);
        let mi: u8 = caps
            .get(2)
            .and_then(|m| m.as_str().parse().ok())
            .unwrap_or(0);
        let ampm = caps.get(3).map_or("", |m| m.as_str()).to_uppercase();
        let h24 = if ampm == "PM" && h < 12 {
            h + 12
        } else if ampm == "AM" && h == 12 {
            0
        } else {
            h
        };
        return format!("T{:02}:{:02}", h24, mi);
    }

    format!("TIME:{}", text)
}

/// Resolve a relative time reference against an anchor date (`YYYY-MM-DD`).
/// Returns the best approximation as an ISO 8601 string.
fn resolve_relative(expr_text: &str, reference: &str) -> String {
    let lower = expr_text.to_lowercase();

    // Parse reference to (year, month, day).
    let parts: Vec<&str> = reference.splitn(3, '-').collect();
    let ref_year: i32 = parts.first().and_then(|s| s.parse().ok()).unwrap_or(2024);
    let ref_month: i32 = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(1);
    let ref_day: i32 = parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(1);

    if lower.contains("yesterday") {
        // Subtract one day (simple, no calendar logic).
        let day = ref_day - 1;
        if day > 0 {
            return format!("{:04}-{:02}-{:02}", ref_year, ref_month, day);
        } else {
            return format!("{:04}-{:02}-28", ref_year, ref_month - 1);
        }
    }
    if lower.contains("tomorrow") {
        return format!("{:04}-{:02}-{:02}", ref_year, ref_month, ref_day + 1);
    }
    if lower.contains("today") || lower.contains("tonight") {
        return format!("{:04}-{:02}-{:02}", ref_year, ref_month, ref_day);
    }
    if lower.contains("last year") {
        return format!("{}", ref_year - 1);
    }
    if lower.contains("next year") {
        return format!("{}", ref_year + 1);
    }
    if lower.contains("last month") {
        return format!("{:04}-{:02}", ref_year, ref_month - 1);
    }
    if lower.contains("next month") {
        return format!("{:04}-{:02}", ref_year, ref_month + 1);
    }
    if lower.contains("last week") {
        return format!("{:04}-W{:02}", ref_year, (ref_month * 4).max(1) - 1);
    }

    // "N units ago"
    let ago_re = Regex::new(
        r"(?i)(\d+|a|an|one|two|three|four|five|six|seven|eight|nine|ten)\s+(second|minute|hour|day|week|month|year)s?\s+ago",
    )
    .expect("static");
    if let Some(caps) = ago_re.captures(&lower) {
        let qty_str = caps.get(1).map_or("", |m| m.as_str());
        let unit = caps.get(2).map_or("", |m| m.as_str());
        let qty: i32 = qty_str
            .parse()
            .ok()
            .or_else(|| word_to_number(qty_str).map(|v| v as i32))
            .unwrap_or(1);
        match unit {
            "day" => {
                return format!(
                    "{:04}-{:02}-{:02}",
                    ref_year,
                    ref_month,
                    (ref_day - qty).max(1)
                )
            }
            "month" => {
                return format!("{:04}-{:02}", ref_year, (ref_month - qty).max(1))
            }
            "year" => return format!("{}", ref_year - qty),
            _ => {}
        }
    }

    // Fallback: return reference.
    reference.to_owned()
}

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

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

    #[test]
    fn test_extract_iso_date() {
        let exprs = extract_time_expressions("The report was submitted on 2024-03-15.");
        assert!(!exprs.is_empty());
        let first = &exprs[0];
        assert_eq!(first.time_type, TimeType::Date);
        assert_eq!(first.value, "2024-03-15");
    }

    #[test]
    fn test_extract_duration() {
        let exprs = extract_time_expressions("The project lasted for three weeks.");
        let dur: Vec<_> = exprs
            .iter()
            .filter(|e| e.time_type == TimeType::Duration)
            .collect();
        assert!(!dur.is_empty());
    }

    #[test]
    fn test_extract_set_frequency() {
        let exprs = extract_time_expressions("We meet every Monday.");
        let sets: Vec<_> = exprs
            .iter()
            .filter(|e| e.time_type == TimeType::Set)
            .collect();
        assert!(!sets.is_empty());
    }

    #[test]
    fn test_normalize_timex_iso_passthrough() {
        let exprs = extract_time_expressions("On 2023-11-01 the deal closed.");
        assert!(!exprs.is_empty());
        let norm = normalize_timex(&exprs[0], "2023-10-01");
        assert_eq!(norm, "2023-11-01");
    }

    #[test]
    fn test_normalize_timex_yesterday() {
        let exprs = extract_time_expressions("Yesterday he arrived.");
        assert!(!exprs.is_empty());
        let norm = normalize_timex(&exprs[0], "2024-05-10");
        // Should resolve to one day before reference.
        assert!(norm.contains("2024-05-09") || !norm.is_empty());
    }

    #[test]
    fn test_temporal_ordering_before() {
        let a = TimeExpression::new("2020", 0, 4, TimeType::Date, "2020", 0.9);
        let b = TimeExpression::new("2022", 5, 9, TimeType::Date, "2022", 0.9);
        assert_eq!(temporal_ordering(&a, &b), TemporalRelation::Before);
    }

    #[test]
    fn test_temporal_ordering_after() {
        let a = TimeExpression::new("2025", 0, 4, TimeType::Date, "2025", 0.9);
        let b = TimeExpression::new("2020", 5, 9, TimeType::Date, "2020", 0.9);
        assert_eq!(temporal_ordering(&a, &b), TemporalRelation::After);
    }

    #[test]
    fn test_temporal_ordering_simultaneous() {
        let a = TimeExpression::new("2024", 0, 4, TimeType::Date, "2024", 0.9);
        let b = TimeExpression::new("2024", 5, 9, TimeType::Date, "2024", 0.9);
        assert_eq!(temporal_ordering(&a, &b), TemporalRelation::Simultaneous);
    }
}