anyseg 0.1.0

Fast segmentation of text into tokens and sentences with positions in the source 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
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
//! The document model: tokens, words with the columns of CoNLL-U, sentences, paragraphs and lines

use std::error::Error;
use std::fmt;
use std::ops::Range;

use crate::Span;
use crate::layout::{self, is_separator};

/// How a line of a paragraph begins after the line before it
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LineBreak {
    /// A break the author made: a verse line, a list item, a paragraph per line
    Author,
    /// A wrap at the width of the page, as in text extracted from PDF
    Typeset,
}

impl LineBreak {
    /// The name of the kind: `"author"` or `"typeset"`
    pub fn name(self) -> &'static str {
        match self {
            LineBreak::Author => "author",
            LineBreak::Typeset => "typeset",
        }
    }
}

/// A line of a paragraph
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Line {
    /// The line without the separators around it
    pub span: Span,
    /// `None` for the first line of a paragraph
    pub break_before: Option<LineBreak>,
}

/// A sentence of a [`Document`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Sentence {
    /// From the start of its first token to the end of its last one
    pub span: Span,
    /// The indices of its tokens
    pub tokens: Range<usize>,
    /// The indices of its words
    pub words: Range<usize>,
}

/// A paragraph of a [`Document`]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Paragraph {
    /// From the start of its first token to the end of its last one
    pub span: Span,
    /// The indices of its sentences
    pub sentences: Range<usize>,
    /// The indices of its tokens
    pub tokens: Range<usize>,
}

/// A column of CoNLL-U that a word holds as text; `head` is a number and `form` comes from the tokens
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Column {
    /// LEMMA: the lemma or the stem
    Lemma,
    /// UPOS: the universal part of speech
    Upos,
    /// XPOS: the part of speech of the language
    Xpos,
    /// FEATS: the morphological features
    Feats,
    /// DEPREL: the relation to the head
    Deprel,
    /// DEPS: the enhanced dependency graph
    Deps,
    /// MISC: any other annotation
    Misc,
}

impl Column {
    /// The columns in the order of CoNLL-U
    pub const ALL: [Column; 7] = [
        Column::Lemma,
        Column::Upos,
        Column::Xpos,
        Column::Feats,
        Column::Deprel,
        Column::Deps,
        Column::Misc,
    ];

    /// The name of the column in lowercase: `"lemma"`, `"upos"`
    pub fn name(self) -> &'static str {
        match self {
            Column::Lemma => "lemma",
            Column::Upos => "upos",
            Column::Xpos => "xpos",
            Column::Feats => "feats",
            Column::Deprel => "deprel",
            Column::Deps => "deps",
            Column::Misc => "misc",
        }
    }

    /// The column with a name in lowercase
    pub fn from_name(name: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|column| column.name() == name)
    }
}

/// Wrong tokens, sentence starts or column values
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DocumentError(pub(crate) String);

impl fmt::Display for DocumentError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl Error for DocumentError {}

/// A vector that grew by doubling, copied to one of its length: a document keeps only what it holds. Shrinking it
/// in place leaves the rest of a large block that some allocators do not reuse (macOS)
fn fitted<T: Copy>(vector: Vec<T>) -> Vec<T> {
    if vector.capacity() > vector.len() {
        vector.as_slice().to_vec()
    } else {
        vector
    }
}

/// The parts of CoNLL-U that a document read from it writes back as they were
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Verbatim {
    /// The comment lines of every sentence
    pub(crate) comments: Vec<Vec<String>>,
    /// Columns 2-10 of the line of every multiword token, by token
    pub(crate) multiword: Vec<(usize, String)>,
    /// The empty nodes of every sentence, with the number of token and word lines before each
    pub(crate) empty: Vec<Vec<(usize, String)>>,
}

/// A segmented text. Tokens are spans of the text, which is never changed; a token holds one word or, for a
/// multiword token, several. Sentences hold whole tokens, paragraphs hold whole sentences, and lines are those of
/// the paragraphs. Words have the columns of CoNLL-U, empty until set.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Document {
    pub(crate) text: String,
    pub(crate) tokens: Vec<Span>,
    /// The first word of every token, and the number of words
    pub(crate) token_words: Vec<usize>,
    pub(crate) word_tokens: Vec<usize>,
    /// Empty when every word is the text of its token
    pub(crate) forms: Vec<Option<String>>,
    /// Every column is empty until set
    pub(crate) columns: [Vec<Option<String>>; 7],
    pub(crate) heads: Vec<Option<u32>>,
    /// The first token of every sentence, and the number of tokens
    pub(crate) sentence_tokens: Vec<usize>,
    /// The first sentence of every paragraph, and the number of sentences
    pub(crate) paragraph_sentences: Vec<usize>,
    pub(crate) lines: Vec<Line>,
    pub(crate) verbatim: Option<Box<Verbatim>>,
}

impl Document {
    /// A document of the given tokens of a text, one word per token. Paragraphs are the parts of the text between
    /// blank lines, every paragraph starts a sentence; without sentence starts a paragraph is one sentence.
    ///
    /// # Errors
    ///
    /// [`DocumentError`] if a token is empty, out of the text, not on character boundaries, overlaps the token
    /// before it, starts or ends with whitespace, or runs over a paragraph boundary, if the text between the
    /// tokens holds anything but whitespace, or if the sentence starts are not increasing token indices.
    pub fn from_tokens(
        text: String,
        tokens: Vec<Span>,
        sentence_starts: Option<&[usize]>,
    ) -> Result<Self, DocumentError> {
        let mut previous = 0;
        for (index, span) in tokens.iter().enumerate() {
            let error = |message: &str| Err(DocumentError(format!("token {index}: {message}")));
            if span.start >= span.end {
                return error("is empty");
            }
            if span.end > text.len() {
                return error("ends after the text");
            }
            if !text.is_char_boundary(span.start) || !text.is_char_boundary(span.end) {
                return error("is not on character boundaries");
            }
            if span.start < previous {
                return error("overlaps or precedes the token before it");
            }
            let piece = span.slice(&text);
            if piece.starts_with(is_separator) || piece.ends_with(is_separator) {
                return error("starts or ends with whitespace");
            }
            if !text[previous..span.start].chars().all(is_separator) {
                return error("the text before it holds characters that no token has");
            }
            previous = span.end;
        }
        if !text[previous..].chars().all(is_separator) {
            return Err(DocumentError(
                "the text after the last token holds characters that no token has".to_owned(),
            ));
        }
        if let Some(starts) = sentence_starts {
            for (position, &start) in starts.iter().enumerate() {
                if start >= tokens.len() || (position > 0 && start <= starts[position - 1]) {
                    return Err(DocumentError(
                        "sentence starts must be increasing token indices".to_owned(),
                    ));
                }
            }
        }
        Self::assemble(text, tokens, sentence_starts)
    }

    /// The document of a text as the segmenter splits it: tokens, the first token of every sentence, the first
    /// sentence of every paragraph and the lines
    pub(crate) fn segmented(
        text: String,
        tokens: Vec<Span>,
        mut sentence_tokens: Vec<usize>,
        mut paragraph_sentences: Vec<usize>,
        lines: Vec<Line>,
    ) -> Self {
        let count = tokens.len();
        paragraph_sentences.push(sentence_tokens.len());
        sentence_tokens.push(count);
        Document {
            text,
            tokens: fitted(tokens),
            token_words: (0..=count).collect(),
            word_tokens: (0..count).collect(),
            forms: Vec::new(),
            columns: Default::default(),
            heads: Vec::new(),
            sentence_tokens: fitted(sentence_tokens),
            paragraph_sentences: fitted(paragraph_sentences),
            lines: fitted(lines),
            verbatim: None,
        }
    }

    /// The document of valid tokens; fails only on a token that runs over a paragraph boundary
    pub(crate) fn assemble(
        text: String,
        tokens: Vec<Span>,
        sentence_starts: Option<&[usize]>,
    ) -> Result<Self, DocumentError> {
        let mut starts = Vec::new();
        let mut paragraph_starts = Vec::new();
        let mut lines = Vec::new();
        let mut token = 0;
        for paragraph in layout::paragraphs(&text) {
            let end = paragraph.last().map_or(0, |line| line.end);
            let first = token;
            while token < tokens.len() && tokens[token].start < end {
                if tokens[token].end > end {
                    return Err(DocumentError(format!(
                        "token {token}: runs over a paragraph boundary"
                    )));
                }
                token += 1;
            }
            if token == first {
                continue;
            }
            paragraph_starts.push(first);
            starts.push(first);
            lines.extend(paragraph.iter().enumerate().map(|(index, &span)| Line {
                span,
                break_before: (index > 0).then_some(LineBreak::Author),
            }));
        }
        if let Some(given) = sentence_starts {
            starts.extend_from_slice(given);
            starts.sort_unstable();
            starts.dedup();
        }
        let paragraph_sentences = paragraph_starts
            .iter()
            .map(|first| starts.partition_point(|start| start < first))
            .chain([starts.len()])
            .collect();
        let count = tokens.len();
        starts.push(count);
        Ok(Document {
            text,
            tokens,
            token_words: (0..=count).collect(),
            word_tokens: (0..count).collect(),
            forms: Vec::new(),
            columns: Default::default(),
            heads: Vec::new(),
            sentence_tokens: starts,
            paragraph_sentences,
            lines,
            verbatim: None,
        })
    }

    /// The text of the document
    pub fn text(&self) -> &str {
        &self.text
    }

    /// The tokens in order
    pub fn tokens(&self) -> &[Span] {
        &self.tokens
    }

    /// The words of a token.
    ///
    /// # Panics
    ///
    /// If there is no such token.
    pub fn token_words(&self, token: usize) -> Range<usize> {
        self.token_words[token]..self.token_words[token + 1]
    }

    /// The number of words
    pub fn word_count(&self) -> usize {
        self.word_tokens.len()
    }

    /// The token that holds a word.
    ///
    /// # Panics
    ///
    /// If there is no such word.
    pub fn word_token(&self, word: usize) -> usize {
        self.word_tokens[word]
    }

    /// The form of a word: the text of its token unless it is a word of a multiword token.
    ///
    /// # Panics
    ///
    /// If there is no such word.
    pub fn form(&self, word: usize) -> &str {
        match self.forms.get(word) {
            Some(Some(form)) => form,
            _ => self.tokens[self.word_tokens[word]].slice(&self.text),
        }
    }

    /// The value of a column of a word, `None` until set.
    ///
    /// # Panics
    ///
    /// If there is no such word.
    pub fn column(&self, column: Column, word: usize) -> Option<&str> {
        self.check_word(word);
        self.columns[column as usize]
            .get(word)
            .and_then(Option::as_deref)
    }

    /// The head of a word: the ID of a word of its sentence or 0 for the root, `None` until set.
    ///
    /// # Panics
    ///
    /// If there is no such word.
    pub fn head(&self, word: usize) -> Option<u32> {
        self.check_word(word);
        self.heads.get(word).copied().flatten()
    }

    fn check_word(&self, word: usize) {
        let count = self.word_count();
        assert!(
            word < count,
            "word {word} is out of range for {count} words"
        );
    }

    /// The number of sentences
    pub fn sentence_count(&self) -> usize {
        self.sentence_tokens.len() - 1
    }

    /// A sentence by its index.
    ///
    /// # Panics
    ///
    /// If there is no such sentence.
    pub fn sentence(&self, index: usize) -> Sentence {
        let tokens = self.sentence_tokens[index]..self.sentence_tokens[index + 1];
        Sentence {
            span: self.cover(&tokens),
            words: self.token_words[tokens.start]..self.token_words[tokens.end],
            tokens,
        }
    }

    /// The sentence that holds a token.
    ///
    /// # Panics
    ///
    /// If there is no such token.
    pub fn token_sentence(&self, token: usize) -> usize {
        let count = self.tokens.len();
        assert!(
            token < count,
            "token {token} is out of range for {count} tokens"
        );
        self.sentence_tokens
            .partition_point(|&start| start <= token)
            - 1
    }

    /// The sentence that holds a word.
    ///
    /// # Panics
    ///
    /// If there is no such word.
    pub fn word_sentence(&self, word: usize) -> usize {
        self.token_sentence(self.word_tokens[word])
    }

    /// The ID of a word in its sentence, from 1.
    ///
    /// # Panics
    ///
    /// If there is no such word.
    pub fn word_id(&self, word: usize) -> usize {
        let first = self.sentence_tokens[self.word_sentence(word)];
        word - self.token_words[first] + 1
    }

    /// The number of paragraphs
    pub fn paragraph_count(&self) -> usize {
        self.paragraph_sentences.len() - 1
    }

    /// A paragraph by its index.
    ///
    /// # Panics
    ///
    /// If there is no such paragraph.
    pub fn paragraph(&self, index: usize) -> Paragraph {
        let sentences = self.paragraph_sentences[index]..self.paragraph_sentences[index + 1];
        let tokens = self.sentence_tokens[sentences.start]..self.sentence_tokens[sentences.end];
        Paragraph {
            span: self.cover(&tokens),
            sentences,
            tokens,
        }
    }

    /// The paragraph that holds a sentence.
    ///
    /// # Panics
    ///
    /// If there is no such sentence.
    pub fn sentence_paragraph(&self, sentence: usize) -> usize {
        let count = self.sentence_count();
        assert!(
            sentence < count,
            "sentence {sentence} is out of range for {count} sentences"
        );
        self.paragraph_sentences
            .partition_point(|&start| start <= sentence)
            - 1
    }

    /// The lines of the paragraphs in order
    pub fn lines(&self) -> &[Line] {
        &self.lines
    }

    fn cover(&self, tokens: &Range<usize>) -> Span {
        Span {
            start: self.tokens[tokens.start].start,
            end: self.tokens[tokens.end - 1].end,
        }
    }

    /// Set word columns and heads at once; nothing changes if a value is wrong.
    ///
    /// # Errors
    ///
    /// [`DocumentError`] if a list has not one value per word, a value is empty, a lemma or MISC holds a tab or
    /// a line break or another value holds whitespace, or a head is not 0 or the ID of a word of its sentence.
    pub fn set_columns(
        &mut self,
        columns: Vec<(Column, Vec<Option<String>>)>,
        heads: Option<Vec<Option<u32>>>,
    ) -> Result<(), DocumentError> {
        let count = self.word_count();
        let length = |name: &str, length: usize| {
            if length == count {
                Ok(())
            } else {
                Err(DocumentError(format!(
                    "{name} has {length} values for {count} words"
                )))
            }
        };
        for (column, values) in &columns {
            length(column.name(), values.len())?;
            // CoNLL-U allows spaces in lemmas and MISC, not in the other columns
            let (wrong, what): (fn(char) -> bool, &str) = match column {
                Column::Lemma | Column::Misc => {
                    (|c| matches!(c, '\t' | '\n' | '\r'), "a tab or a line break")
                }
                _ => (char::is_whitespace, "whitespace"),
            };
            for (word, value) in values.iter().enumerate() {
                if let Some(value) = value
                    && (value.is_empty() || value.contains(wrong))
                {
                    return Err(DocumentError(format!(
                        "{} of word {word} is empty or holds {what}",
                        column.name()
                    )));
                }
            }
        }
        if let Some(heads) = &heads {
            length("head", heads.len())?;
            for (word, head) in heads.iter().enumerate() {
                if let Some(head) = *head {
                    let words = self.sentence(self.word_sentence(word)).words.len();
                    if head as usize > words {
                        return Err(DocumentError(format!(
                            "head of word {word} is {head}, but its sentence has {words} words"
                        )));
                    }
                }
            }
        }
        for (column, values) in columns {
            self.columns[column as usize] = if values.iter().all(Option::is_none) {
                Vec::new()
            } else {
                values
            };
        }
        if let Some(heads) = heads {
            self.heads = if heads.iter().all(Option::is_none) {
                Vec::new()
            } else {
                heads
            };
        }
        Ok(())
    }
}

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

    fn document(text: &str, starts: Option<&[usize]>) -> Document {
        let mut tokens = Vec::new();
        layout::runs(
            text,
            Span {
                start: 0,
                end: text.len(),
            },
            &mut tokens,
        );
        Document::from_tokens(text.to_owned(), tokens, starts).unwrap()
    }

    #[test]
    fn paragraphs_start_sentences() {
        let doc = document("Ёж идёт.\nКот спит.\n\n Утро.", Some(&[1, 3]));
        assert_eq!(doc.sentence_tokens, [0, 1, 3, 4, 5]);
        assert_eq!(doc.paragraph_sentences, [0, 3, 4]);
        assert_eq!(doc.paragraph(1).span.slice(doc.text()), "Утро.");
        assert_eq!(doc.sentence(1).span.slice(doc.text()), "идёт.\nКот");
        assert_eq!(doc.sentence_paragraph(2), 0);
        assert_eq!(doc.token_sentence(4), 3);
        let lines: Vec<(&str, Option<LineBreak>)> = doc
            .lines()
            .iter()
            .map(|line| (line.span.slice(doc.text()), line.break_before))
            .collect();
        assert_eq!(
            lines,
            [
                ("Ёж идёт.", None),
                ("Кот спит.", Some(LineBreak::Author)),
                ("Утро.", None)
            ]
        );
    }

    #[test]
    fn words_are_tokens() {
        let doc = document("Ёж идёт", None);
        assert_eq!(doc.word_count(), 2);
        assert_eq!(doc.form(1), "идёт");
        assert_eq!(doc.word_token(1), 1);
        assert_eq!(doc.token_words(1), 1..2);
        assert_eq!(doc.word_id(1), 2);
        assert_eq!(doc.column(Column::Lemma, 0), None);
        assert_eq!(doc.head(0), None);
    }

    #[test]
    fn indices_out_of_range() {
        let doc = document("Ёж идёт", None);
        for call in [
            |doc: &Document| {
                let _ = doc.column(Column::Lemma, 2);
            },
            |doc: &Document| {
                let _ = doc.head(2);
            },
            |doc: &Document| {
                let _ = doc.form(2);
            },
            |doc: &Document| {
                let _ = doc.token_sentence(2);
            },
            |doc: &Document| {
                let _ = doc.sentence_paragraph(1);
            },
            |doc: &Document| {
                let _ = doc.sentence(1);
            },
        ] {
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| call(&doc)));
            assert!(result.is_err());
        }
    }

    #[test]
    fn fitted_vectors_are_new_blocks() {
        // Shrinking in place would keep the block, and its rest would not be reused on macOS
        let mut vector = Vec::with_capacity(200_000);
        vector.resize(150_000, Span { start: 0, end: 1 });
        let block = vector.as_ptr();
        let vector = fitted(vector);
        assert_eq!(vector.capacity(), vector.len());
        assert_ne!(vector.as_ptr(), block);
    }

    #[test]
    fn blank_text() {
        let doc = document(" \n\n ", None);
        assert_eq!(
            (
                doc.sentence_count(),
                doc.paragraph_count(),
                doc.lines().len()
            ),
            (0, 0, 0)
        );
    }

    #[test]
    fn wrong_tokens() {
        let text = "Ёж идёт\n\nдомой".to_owned();
        let span = |start, end| Span { start, end };
        for (tokens, message) in [
            (vec![span(0, 0)], "token 0: is empty"),
            (vec![span(0, 99)], "token 0: ends after the text"),
            (vec![span(0, 1)], "token 0: is not on character boundaries"),
            (
                vec![span(0, 4), span(5, 13), span(0, 4)],
                "token 2: overlaps or precedes the token before it",
            ),
            (vec![span(4, 13)], "token 0: starts or ends with whitespace"),
            (
                vec![span(5, 13), span(15, 25)],
                "token 0: the text before it holds characters that no token has",
            ),
            (
                vec![span(0, 4), span(5, 13)],
                "the text after the last token holds characters that no token has",
            ),
            (
                vec![span(0, 4), span(5, 25)],
                "token 1: runs over a paragraph boundary",
            ),
        ] {
            assert_eq!(
                Document::from_tokens(text.clone(), tokens, None).unwrap_err(),
                DocumentError(message.to_owned())
            );
        }
        let tokens = vec![span(0, 4), span(5, 13), span(15, 25)];
        for starts in [&[0, 0][..], &[1, 0], &[3]] {
            assert_eq!(
                Document::from_tokens(text.clone(), tokens.clone(), Some(starts)).unwrap_err(),
                DocumentError("sentence starts must be increasing token indices".to_owned())
            );
        }
    }

    #[test]
    fn set_columns() {
        let mut doc = document("Ёж идёт. Кот", Some(&[2]));
        doc.set_columns(
            vec![(Column::Upos, vec![Some("NOUN".to_owned()), None, None])],
            Some(vec![Some(2), Some(0), Some(0)]),
        )
        .unwrap();
        assert_eq!(doc.column(Column::Upos, 0), Some("NOUN"));
        assert_eq!(doc.head(0), Some(2));
        for (columns, heads, message) in [
            (
                vec![(Column::Lemma, vec![None])],
                None,
                "lemma has 1 values for 3 words",
            ),
            (
                vec![(Column::Feats, vec![None, Some("a b".to_owned()), None])],
                None,
                "feats of word 1 is empty or holds whitespace",
            ),
            (
                vec![(Column::Lemma, vec![None, Some("a\nb".to_owned()), None])],
                None,
                "lemma of word 1 is empty or holds a tab or a line break",
            ),
            (
                vec![],
                Some(vec![None, None, Some(2)]),
                "head of word 2 is 2, but its sentence has 1 words",
            ),
        ] {
            assert_eq!(
                doc.set_columns(columns, heads).unwrap_err(),
                DocumentError(message.to_owned())
            );
        }
        assert_eq!(doc.column(Column::Upos, 0), Some("NOUN"));
        doc.set_columns(vec![(Column::Upos, vec![None, None, None])], None)
            .unwrap();
        assert!(doc.columns[Column::Upos as usize].is_empty());
    }
}