textgridde-rs 0.1.6

A library for dealing with Praat TextGrid files. MIT licensed.
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
use std::{
    cmp::Ordering,
    fmt::{self, Display, Formatter},
};

use derive_more::Constructor;
use getset::{Getters, MutGetters, Setters};

/// An "interval," used in Praat as a specific period of time with an associated label.
#[derive(Clone, Constructor, Debug, Default, Getters, Setters, PartialEq)]
pub struct Interval {
    #[getset(get = "pub")]
    xmin: f64,
    #[getset(get = "pub")]
    xmax: f64,
    #[getset(get = "pub", set = "pub")]
    text: String,
}

impl Interval {
    /// Returns the duration of the interval.
    #[must_use]
    pub fn get_duration(&self) -> f64 {
        self.xmax - self.xmin
    }

    /// Returns the midpoint of the interval.
    #[must_use]
    pub fn get_midpoint(&self) -> f64 {
        (self.xmin + self.xmax) / 2.0
    }

    /// Sets the xmin value of the interval.
    ///
    /// # Arguments
    ///
    /// * `xmin` - The xmin value to set.
    pub fn set_xmin(&mut self, xmin: f64) {
        self.xmin = xmin;
    }

    /// Sets the xmax value of the interval.
    ///
    /// # Arguments
    ///
    /// * `xmax` - The xmax value to set.
    pub fn set_xmax(&mut self, xmax: f64) {
        self.xmax = xmax;
    }
}

impl Display for Interval {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        writeln!(f, "Interval")?;
        writeln!(f, "    xmin = {}", self.xmin)?;
        writeln!(f, "    xmax = {}", self.xmax)?;
        writeln!(f, "    text = \"{}\"", self.text)?;
        Ok(())
    }
}

/// Represents an interval tier in a `TextGrid`.
#[derive(Clone, Constructor, Debug, Default, Getters, MutGetters, Setters, PartialEq)]
pub struct Tier {
    #[getset(get = "pub", set = "pub")]
    name: String,
    #[getset(get = "pub")]
    xmin: f64,
    #[getset(get = "pub")]
    xmax: f64,
    #[getset(get = "pub", get_mut = "pub")]
    intervals: Vec<Interval>,
}

impl Tier {
    /// Sets the minimum x value for the interval tier.
    ///
    /// # Arguments
    ///
    /// * `xmin` - The minimum x value to set.
    /// * `warn` - If `true`, displays a warning if the minimum point of any interval is greater than `xmin`.
    pub fn set_xmin<T: Into<Option<bool>>>(&mut self, xmin: f64, warn: T) {
        if warn.into().unwrap_or_default() {
            let min_point = self
                .intervals
                .iter()
                .filter_map(|intervals| {
                    intervals
                        .xmin
                        .partial_cmp(&f64::INFINITY)
                        .map(|_| intervals.xmin)
                })
                .min_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Greater)); // If invalid, return greater, since we're looking for the minimum

            if min_point.is_some_and(|min| xmin > min) {
                eprintln!("Warning: Tier `{}` has a minimum point of {} but the TextGrid has an xmin of {}", self.name, min_point.unwrap_or_default(), xmin);
            }
        }

        self.xmin = xmin;
    }

    /// Sets the maximum x value for the interval tier.
    ///
    /// # Arguments
    ///
    /// * `xmax` - The maximum x value to set.
    /// * `warn` - If `true`, displays a warning if the maximum point of any interval is less than `xmax`.
    pub fn set_xmax<W: Into<Option<bool>>>(&mut self, xmax: f64, warn: W) {
        if warn.into().unwrap_or_default() {
            let max_point = self
                .intervals
                .iter()
                .filter_map(|interval| {
                    interval
                        .xmax
                        .partial_cmp(&f64::INFINITY)
                        .map(|_| interval.xmax)
                })
                .max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Less)); // If invalid, return less, since we're looking for the maximum

            if max_point.is_some_and(|max| xmax < max) {
                eprintln!("Warning: Tier `{}` has a minimum point of {} but the TextGrid has an xmax of {}", self.name, max_point.unwrap_or_default(), xmax);
            }
        }

        self.xmax = xmax;
    }

    /// Returns the number of intervals in the interval tier.
    #[must_use]
    pub fn get_size(&self) -> usize {
        self.intervals.len()
    }

    /// Pushes an interval to the interval tier.
    /// Calls `reorder()` to ensure the intervals are sorted by their minimum x value after pushing the interval.
    ///
    /// # Arguments
    ///
    /// * `interval` - The interval to push.
    /// * `warn` - If `Some(true)`, displays a warning if the minimum point of the interval is less than the minimum x value of the interval tier.
    pub fn push_interval<W: Into<Option<bool>>>(&mut self, interval: Interval, warn: W) {
        if warn.into().unwrap_or_default() && interval.xmin < self.xmin {
            eprintln!(
                "Warning: Tier `{}` has a minimum point of {} but the TextGrid has an xmin of {}",
                self.name, interval.xmin, self.xmin
            );
        }
        self.intervals.push(interval);

        self.reorder();
    }

    /// Pushes multiple intervals to the interval tier vector.
    /// Calls `reorder()` afterwards to ensure the intervals are sorted by their minimum x value after pushing the intervals.
    ///
    /// # Arguments
    ///
    /// * `intervals` - The intervals to push.
    /// * `warn` - If `Some(true)`, displays a warning if the minimum point of any interval is less than the minimum x value of the interval tier.
    pub fn push_intervals<W: Into<Option<bool>> + Copy>(
        &mut self,
        intervals: Vec<Interval>,
        warn: W,
    ) {
        for interval in &intervals {
            if warn.into().unwrap_or_default() {
                if interval.xmin < self.xmin {
                    eprintln!(
                        "Warning: Tier `{}` has a minimum point of {} but the TextGrid has an xmin of {}",
                        self.name, interval.xmin, self.xmin
                    );
                }
                if interval.xmax > self.xmax {
                    eprintln!(
                        "Warning: Tier `{}` has a maximum point of {} but the TextGrid has an xmax of {}",
                        self.name, interval.xmax, self.xmax
                    );
                }
            }
        }

        self.intervals.extend(intervals);

        self.reorder();
    }

    /// Sets the intervals of the interval tier.
    ///
    /// # Arguments
    ///
    /// * `intervals` - The intervals to set.
    /// * `warn` - If `Some(true)`, displays a warning if any interval's minimum point is less than the minimum x value of the interval tier or if any interval's maximum point is greater than the maximum x value of the interval tier.
    pub fn set_intervals<W: Into<Option<bool>>>(&mut self, intervals: Vec<Interval>, warn: W) {
        if warn.into().unwrap_or_default() {
            for interval in &intervals {
                if interval.xmin < self.xmin {
                    eprintln!(
                        "Warning: Tier `{}` has a minimum point of {} but the TextGrid has an xmin of {}",
                        self.name, interval.xmin, self.xmin
                    );
                }
                if interval.xmax > self.xmax {
                    eprintln!(
                        "Warning: Tier `{}` has a maximum point of {} but the TextGrid has an xmax of {}",
                        self.name, interval.xmax, self.xmax
                    );
                }
            }
        }

        self.intervals = intervals;
    }

    /// Sorts the intervals in the interval tier by their minimum x value.
    fn reorder(&mut self) {
        self.intervals
            .sort_by(|a, b| a.xmin.partial_cmp(&b.xmin).unwrap_or(Ordering::Equal));
    }

    /// Checks for overlaps in the interval tier.
    /// Calls `reorder` to ensure the intervals are sorted by their minimum x value before checking for overlaps.
    ///
    /// # Returns
    ///
    /// A vector of pairs of the overlapping intervals' indices or `None` if there are no overlaps.
    #[must_use]
    pub fn check_overlaps(&self) -> Option<Vec<(u64, u64)>> {
        let mut overlaps: Vec<(u64, u64)> = Vec::new();

        // iterate over each pair of intervals, checking to make sure the xmax of the first interval is perfectly equal to the xmin of the second interval
        for (i, window) in self.intervals.windows(2).enumerate() {
            let interval = &window[0];
            let next_interval = &window[1];

            #[allow(clippy::float_cmp)]
            if interval.xmax != next_interval.xmin {
                overlaps.push((i as u64, (i + 1) as u64));
            }
        }

        if overlaps.is_empty() {
            None
        } else {
            Some(overlaps)
        }
    }

    /// Fixes gaps/overlaps in the interval tier.
    /// Calls `reorder` to ensure the intervals are sorted by their minimum x value before fixing gaps/overlaps.
    ///
    /// # Arguments
    ///
    /// * `prefer_first` - `true` by default. If `true`, prefers the first interval in the case of a gap. If `false`, prefers the second interval in the case of a gap.
    ///
    /// # Panics
    ///
    /// If the amount of intervals exceeds `isize::MAX`.
    pub fn fix_boundaries<P: Into<Option<bool>> + Copy>(&mut self, prefer_first: P) {
        if self.intervals.len() < 2 {
            return;
        }

        self.reorder();

        // Iterate over each pair of intervals, checking to make sure the xmax of the first
        // interval is perfectly equal to the xmin of the second interval. If not, handle
        // the gap by either modifying the xmax of the first interval or the xmin of the
        // second interval, depending on the value of `prefer_first`.
        if prefer_first.into().unwrap_or(true) {
            // Iterate in reverse, so we can modify the less-preferred interval without
            // affecting the preferred interval
            for i in (1..self.intervals.len()).rev() {
                let prev_interval = self.intervals[i - 1].clone();
                let interval = &mut self.intervals[i];

                #[allow(clippy::float_cmp)]
                if interval.xmin != prev_interval.xmax {
                    interval.xmin = prev_interval.xmax;
                }
            }
        } else {
            for i in 0..self.intervals.len() - 1 {
                let next_interval = self.intervals[i + 1].clone();
                let interval = &mut self.intervals[i];

                #[allow(clippy::float_cmp)]
                if interval.xmax != next_interval.xmin {
                    interval.xmax = next_interval.xmin;
                }
            }
        }
    }

    /// Fills gaps within an `IntervalTier` with the specified text, ensuring no time period
    /// is left empty.
    ///
    /// # Arguments
    ///
    /// * `text` - The text to fill the gaps with.
    ///
    /// # Panics
    ///
    /// If the amount of intervals exceeds `isize::MAX`.
    #[allow(clippy::float_cmp)]
    pub fn fill_gaps(&mut self, text: &str) {
        if self.intervals.len() < 2 {
            return;
        }

        self.reorder();

        let first_xmin = self.intervals.first().unwrap().xmin;
        if first_xmin != self.xmin {
            let new_interval = Interval::new(self.xmin, first_xmin, text.to_string());
            self.intervals.insert(0, new_interval);
        }

        let last_xmax = self.intervals.last().unwrap().xmax;
        if last_xmax != self.xmax {
            let new_interval = Interval::new(last_xmax, self.xmax, text.to_string());
            self.intervals.push(new_interval);
        }

        for (index, window) in self.intervals.clone().windows(2).enumerate() {
            let interval = &window[0];
            let next_interval = &window[1];

            #[allow(clippy::float_cmp)]
            if interval.xmax != next_interval.xmin {
                let new_interval =
                    Interval::new(interval.xmax, next_interval.xmin, text.to_string());
                self.intervals.insert(index + 1, new_interval);
            }
        }
    }
}

impl Display for Tier {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(
            f,
            "IntervalTier {}:
                xmin:  {}
                xmax:  {}
                interval count: {}",
            self.name,
            self.xmin,
            self.xmax,
            self.intervals.len()
        )
    }
}

#[cfg(test)]
#[allow(clippy::float_cmp)]
mod test_interval_tier {
    use crate::interval::Interval;

    #[test]
    fn get_duration() {
        let interval = Interval::new(0.0, 2.3, "test".to_string());

        assert_eq!(interval.get_duration(), 2.3);
    }

    #[test]
    fn get_midpoint() {
        let interval = Interval::new(0.0, 2.3, "test".to_string());

        assert_eq!(interval.get_midpoint(), 1.15);
    }

    #[test]
    fn set_xmin() {
        let mut interval = Interval::new(0.0, 2.3, "test".to_string());

        interval.set_xmin(1.0);

        assert_eq!(interval.xmin, 1.0);
    }

    #[test]
    fn set_xmax() {
        let mut interval = Interval::new(0.0, 2.3, "test".to_string());

        interval.set_xmax(1.0);

        assert_eq!(interval.xmax, 1.0);
    }

    #[test]
    fn to_string() {
        let interval = Interval::new(0.0, 2.3, "test".to_string());

        assert_eq!(
            interval.to_string(),
            "Interval\n    xmin = 0\n    xmax = 2.3\n    text = \"test\"\n"
        );
    }
}

#[cfg(test)]
#[allow(clippy::float_cmp)]
mod test_tier {
    use crate::interval::{Interval, Tier};

    #[test]
    fn set_xmin() {
        let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        tier.set_xmin(1.0, Some(true));

        assert_eq!(tier.xmin, 1.0);
    }

    #[test]
    fn set_xmax() {
        let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        tier.set_xmax(1.0, Some(true));

        assert_eq!(tier.xmax, 1.0);
    }

    #[test]
    fn get_size() {
        let tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        assert_eq!(tier.get_size(), 0);
    }

    #[test]
    fn push_interval() {
        let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        tier.push_interval(Interval::new(0.0, 1.0, "test".to_string()), Some(true));

        assert_eq!(tier.intervals.len(), 1);
    }

    #[test]
    fn push_intervals() {
        let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        tier.push_intervals(
            vec![
                Interval::new(0.0, 1.0, "test".to_string()),
                Interval::new(1.0, 2.0, "test".to_string()),
            ],
            Some(true),
        );

        assert_eq!(tier.intervals.len(), 2);
    }

    #[test]
    fn set_intervals() {
        let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        tier.set_intervals(
            vec![
                Interval::new(0.0, 1.0, "test".to_string()),
                Interval::new(1.0, 2.0, "test".to_string()),
            ],
            Some(true),
        );

        assert_eq!(tier.intervals.len(), 2);
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn reorder() {
        let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        tier.push_intervals(
            vec![
                Interval::new(1.0, 2.0, "test".to_string()),
                Interval::new(0.0, 1.0, "test".to_string()),
            ],
            Some(true),
        );

        tier.reorder();

        assert_eq!(tier.intervals[0].xmin, 0.0);
        assert_eq!(tier.intervals[1].xmin, 1.0);
    }

    mod check_overlaps {
        use crate::{
            interval::{Interval, Tier as IntervalTier},
            textgrid::{TextGrid, Tier},
        };

        #[test]
        fn no_overlap() {
            let mut textgrid = TextGrid::new(0.0, 2.3, Vec::new(), "test".to_string());

            textgrid.push_tier(
                Tier::IntervalTier(IntervalTier::new(
                    "John".to_string(),
                    0.0,
                    2.3,
                    vec![
                        Interval::new(0.0, 1.5, "daisy bell".to_string()),
                        Interval::new(1.5, 2.3, "daisy bell".to_string()),
                    ],
                )),
                false,
            );

            let overlaps = textgrid.check_overlaps();

            assert!(overlaps.is_none());
        }

        #[test]
        fn overlap() {
            let mut textgrid = TextGrid::new(0.0, 2.3, Vec::new(), "test".to_string());

            textgrid.push_tier(
                Tier::IntervalTier(IntervalTier::new(
                    "John".to_string(),
                    0.0,
                    2.3,
                    vec![
                        Interval::new(0.0, 1.5, "daisy bell".to_string()),
                        Interval::new(1.0, 2.3, "daisy bell".to_string()),
                    ],
                )),
                false,
            );

            let overlaps = textgrid.check_overlaps().unwrap();

            assert_eq!(overlaps.len(), 1);
            assert_eq!(overlaps[0].0, "John");
            assert_eq!(overlaps[0].1, (0, 1));
        }
    }

    #[allow(clippy::float_cmp)]
    mod fix_boundaries {
        use crate::interval::{Interval, Tier};

        #[test]
        fn prefer_first() {
            let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

            tier.push_intervals(
                vec![
                    Interval::new(0.0, 1.2, "daisy".to_string()),
                    Interval::new(1.0, 1.75, "bell".to_string()),
                    Interval::new(1.5, 2.5, "answer".to_string()),
                    Interval::new(2.0, 5.0, "do".to_string()),
                ],
                false,
            );

            tier.fix_boundaries(true);

            assert_eq!(tier.intervals()[0].xmin(), &0.0);
            assert_eq!(tier.intervals()[0].xmax(), &1.2);
            assert_eq!(tier.intervals()[1].xmin(), &1.2);
            assert_eq!(tier.intervals()[1].xmax(), &1.75);
            assert_eq!(tier.intervals()[2].xmin(), &1.75);
            assert_eq!(tier.intervals()[2].xmax(), &2.5);
            assert_eq!(tier.intervals()[3].xmin(), &2.5);
            assert_eq!(tier.intervals()[3].xmax(), &5.0);
        }

        #[test]
        fn prefer_last() {
            let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

            tier.push_intervals(
                vec![
                    Interval::new(0.0, 1.2, "daisy".to_string()),
                    Interval::new(1.0, 1.75, "bell".to_string()),
                    Interval::new(1.5, 2.5, "answer".to_string()),
                    Interval::new(2.0, 5.0, "do".to_string()),
                ],
                false,
            );

            tier.fix_boundaries(false);

            assert_eq!(tier.intervals()[0].xmin(), &0.0);
            assert_eq!(tier.intervals()[0].xmax(), &1.0);
            assert_eq!(tier.intervals()[1].xmin(), &1.0);
            assert_eq!(tier.intervals()[1].xmax(), &1.5);
            assert_eq!(tier.intervals()[2].xmin(), &1.5);
            assert_eq!(tier.intervals()[2].xmax(), &2.0);
            assert_eq!(tier.intervals()[3].xmin(), &2.0);
            assert_eq!(tier.intervals()[3].xmax(), &5.0);
        }
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn fill_gaps() {
        let mut tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        tier.push_intervals(
            vec![
                Interval::new(0.0, 1.2, "daisy".to_string()),
                Interval::new(1.5, 2.3, "bell".to_string()),
            ],
            false,
        );

        tier.fill_gaps("gap");

        assert_eq!(tier.intervals()[1].text(), "gap");
        assert_eq!(tier.intervals()[1].xmin(), &1.2);
        assert_eq!(tier.intervals()[1].xmax(), &1.5);
    }

    #[test]
    fn to_string() {
        let tier = Tier::new("test".to_string(), 0.0, 2.3, Vec::new());

        assert_eq!(
            tier.to_string(),
            "IntervalTier test:
                xmin:  0
                xmax:  2.3
                interval count: 0"
        );
    }
}