kakemono 0.22.2

Convert PDF, Word and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
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
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
//! Chunks output: the text of the document cut into pieces ready to embed
//! for retrieval (RAG), one JSON object per line (`.jsonl`).
//!
//! Chunks follow the structure of the document: blocks are grouped by
//! section, a chunk never holds two sections, and a block is cut only when
//! it does not fit in one chunk (at sentence ends, else between words; a
//! table between rows, with its caption and header repeated).
//! Each chunk starts with its section path ("3 Model > 3.2 Attention"), so
//! that a piece of text keeps the context a reader has. The paper title is
//! not repeated in the text: retrieval within one paper gains nothing from
//! it, and measured on Qasper (100 papers, two embedding models) a title on
//! every chunk lost 3 to 5 points of evidence recall. It is in each chunk's
//! metadata instead, to filter or prefix when searching several papers.
//!
//! Figures and tables give their caption and, for a rebuilt table, its rows
//! (one field per column, joined by ` | `, empty where no cell starts);
//! formulas are written as `$$LaTeX$$` when their structure is known.
//! Pictures are left out.

mod tokens;

pub use tokens::{NAMED_TOKENIZERS, TokenCounter, estimate};

use super::json::plain_text::{block_plain, plain_of};
use super::plain;
use crate::model::{Block, BlockKind, Document, Table};
use serde::Serialize;

/// Separator of the parts of a section path in a chunk's text.
const PATH_SEPARATOR: &str = " > ";

/// Separator between the section path and the text, and between blocks.
const BLOCK_SEPARATOR: &str = "\n\n";

/// Tokens kept free in a chunk for the break before a piece: counts are
/// not quite additive (the estimate counts the space before a number, real
/// tokenizers merge across the break).
const JOIN_SLACK_TOKENS: usize = 1;

/// Separator between the cells of a table row.
const CELL_SEPARATOR: &str = " | ";
/// A table's caption and last header row, repeated on each piece when the
/// whole head is too long, are each cut to this share (1 / n) of a chunk.
const HEAD_PART_SHARE: usize = 4;

/// Everything that controls how a document is cut into chunks.
#[derive(Debug, Clone)]
pub struct ChunkOptions {
    /// Largest size of a chunk, in tokens, section path included.
    pub max_tokens: usize,
    /// How tokens are counted: an estimate by default (see
    /// [`TokenCounter`]).
    pub counter: TokenCounter,
}

impl Default for ChunkOptions {
    /// At most 512 tokens, the input limit of most embedding models,
    /// counted by the estimate.
    fn default() -> Self {
        Self {
            max_tokens: 512,
            counter: TokenCounter::estimate(),
        }
    }
}

/// One chunk: its text and where it comes from.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct Chunk {
    /// Number of the chunk, in reading order, from 0.
    pub id: usize,
    /// The text to embed: the section path, a blank line, then the text of
    /// the blocks, separated by blank lines.
    pub text: String,
    /// Size of `text` in tokens, as [`ChunkOptions::counter`] counts them.
    pub tokens: usize,
    /// Title of the document.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Headings of the sections the chunk is in, outermost first; empty
    /// before the first heading.
    pub section: Vec<String>,
    /// Ids of the blocks the text comes from (the `id` of the JSON output).
    pub blocks: Vec<usize>,
    /// Pages the text is on, from 0.
    pub pages: Vec<usize>,
}

/// Cuts a document into chunks.
pub fn render_chunks(doc: &Document, opts: &ChunkOptions) -> Vec<Chunk> {
    let mut builder = Builder::new(opts, doc.meta.title.clone());
    // The headings open above the current block: (level, text).
    let mut headings: Vec<(u8, String)> = Vec::new();
    for block in &doc.blocks {
        crate::common::cancel::check();
        if let BlockKind::Heading { level } = block.kind {
            builder.flush();
            while headings.last().is_some_and(|&(open, _)| open >= level) {
                headings.pop();
            }
            headings.push((level, plain_of(&block.spans)));
            continue;
        }
        let text = block_text(block);
        if text.is_empty() {
            continue;
        }
        builder.enter(headings.iter().map(|(_, heading)| heading.clone()).collect());
        builder.add_block(block, &text);
    }
    builder.flush();
    builder.chunks
}

/// The chunks as JSON Lines: one object per line, each line ending with a
/// line break.
pub fn chunks_jsonl(chunks: &[Chunk]) -> String {
    let mut out = String::new();
    for chunk in chunks {
        out.push_str(&serde_json::to_string(chunk).expect("a chunk serialises"));
        out.push('\n');
    }
    out
}

/// The text of one block as a reader (or a language model) reads it; empty
/// for a block without text.
fn block_text(block: &Block) -> String {
    let BlockKind::Figure { .. } = block.kind else {
        return block_plain(block).unwrap_or_default().trim().to_string();
    };
    let mut parts = vec![plain_of(&block.spans)];
    match &block.table {
        Some(table) => parts.extend(table_rows(table)),
        None => parts.extend(block.rows.first().map(|row| plain(row))),
    }
    parts.retain(|part| !part.trim().is_empty());
    parts.join("\n").trim().to_string()
}

/// The rows of `table` as text, one string per row: one field per column
/// of the table, joined by [`CELL_SEPARATOR`], so that a field is under
/// its header in every row. A cell's text is at its first column; the
/// other columns it spans, those covered by a cell from a row above, and
/// those without a cell are empty fields; a column empty in every row is
/// left out. The line is then trimmed: a row whose last fields are empty
/// ends with `" |"`, but every row has as many `|` as the table has kept
/// columns less one.
fn table_rows(table: &Table) -> Vec<String> {
    let n_cols = table.n_cols.max(1);
    let rows: Vec<Vec<String>> = table
        .rows
        .iter()
        .map(|row| {
            let mut fields = vec![String::new(); n_cols];
            for cell in row.iter().filter(|cell| cell.col < n_cols) {
                fields[cell.col] = plain_of(&cell.spans);
            }
            fields
        })
        .collect();
    // A column empty in every row says nothing.
    let keep: Vec<bool> = (0..n_cols)
        .map(|col| rows.iter().any(|row| !row[col].trim().is_empty()))
        .collect();
    rows.iter()
        .map(|fields| {
            let kept: Vec<&str> = fields
                .iter()
                .zip(&keep)
                .filter(|(_, keep)| **keep)
                .map(|(field, _)| field.as_str())
                .collect();
            kept.join(CELL_SEPARATOR).trim().to_string()
        })
        .collect()
}

/// Cuts the text of a rebuilt table into pieces of at most `room` tokens,
/// between rows, each piece starting with the caption and the header rows;
/// when they take more than half of `room`, they are pieces of their own
/// first, and each piece starts with the caption's start and the last
/// header row, each cut to a quarter of `room`. A row that does not
/// fit even alone is cut between words, the head repeated on each part; a
/// part with nothing but the separators of empty cells is left out.
/// `None` when `block` has no rebuilt table, or no row to cut at.
fn table_pieces(block: &Block, room: usize, counter: &TokenCounter) -> Option<Vec<String>> {
    let table = block.table.as_ref()?;
    let rows = table_rows(table);
    let (header, body) = rows.split_at(table.header_rows.min(rows.len()));
    let mut head_lines = vec![plain_of(&block.spans)];
    head_lines.extend(header.iter().cloned());
    head_lines.retain(|line| !line.trim().is_empty());
    let mut head = head_lines.join("\n");
    let mut pieces = Vec::new();
    if counter.count(&head) * 2 > room {
        // A head too long to repeat is a piece (or pieces) of its own,
        // and the rows go on under its caption's start and last header
        // row, each cut to a quarter of the room.
        pieces.extend(split_long(&head, room.max(1), counter));
        let budget = (room / HEAD_PART_SHARE).max(1);
        let caption = plain_of(&block.spans);
        let short: Vec<String> = [caption.as_str(), header.last().map_or("", String::as_str)]
            .iter()
            .filter(|part| !part.trim().is_empty())
            .filter_map(|part| split_long(part, budget, counter).into_iter().next())
            .collect();
        head = short.join("\n");
        if counter.count(&head) * 2 > room {
            head.clear();
        }
    }
    let with_head = |text: &str| {
        if head.is_empty() {
            text.to_string()
        } else {
            format!("{head}\n{text}")
        }
    };
    // The body rows of the piece being built.
    let mut current: Vec<&str> = Vec::new();
    for row in body.iter().filter(|row| !row.trim().is_empty()) {
        current.push(row);
        if counter.count(&with_head(&current.join("\n"))) <= room {
            continue;
        }
        current.pop();
        if !current.is_empty() {
            pieces.push(with_head(&current.join("\n")));
            current.clear();
        }
        if counter.count(&with_head(row)) <= room {
            current.push(row);
            continue;
        }
        let budget = room.saturating_sub(counter.count(&format!("{head}\n"))).max(1);
        // A part made only of the separators of empty cells says nothing.
        pieces.extend(
            split_long(row, budget, counter)
                .iter()
                .filter(|part| part.chars().any(|ch| !(ch.is_whitespace() || ch == '|')))
                .map(|part| with_head(part)),
        );
    }
    if !current.is_empty() {
        pieces.push(with_head(&current.join("\n")));
    }
    (!pieces.is_empty()).then_some(pieces)
}

/// Builds the chunks one block at a time.
struct Builder<'a> {
    /// The options.
    opts: &'a ChunkOptions,
    /// Title of the document, copied into each chunk.
    title: Option<String>,
    /// The chunks written so far.
    chunks: Vec<Chunk>,
    /// Headings of the current section, outermost first.
    section: Vec<String>,
    /// The section path at the start of the current chunk's text (empty
    /// when it is left out).
    head: String,
    /// The texts of the current chunk.
    body: Vec<String>,
    /// Ids of the blocks of the current chunk.
    blocks: Vec<usize>,
    /// Pages of the current chunk.
    pages: Vec<usize>,
}

impl<'a> Builder<'a> {
    /// An empty builder.
    fn new(opts: &'a ChunkOptions, title: Option<String>) -> Self {
        Self {
            opts,
            title,
            chunks: Vec::new(),
            section: Vec::new(),
            head: String::new(),
            body: Vec::new(),
            blocks: Vec::new(),
            pages: Vec::new(),
        }
    }

    /// Moves to the section `section`: a new section starts a new chunk.
    /// The section path heads the chunks of the section, unless it takes
    /// more than half of a chunk.
    fn enter(&mut self, section: Vec<String>) {
        if section == self.section {
            return;
        }
        self.flush();
        let head = section.join(PATH_SEPARATOR);
        self.head = if self.count(&head) * 2 > self.opts.max_tokens {
            String::new()
        } else {
            head
        };
        self.section = section;
    }

    /// Adds the text of `block` to the chunks, cut into pieces when it does
    /// not fit in one.
    fn add_block(&mut self, block: &Block, text: &str) {
        // What a chunk holds besides its section path, less the token that
        // the break before a piece can cost (before a number).
        let room = self
            .opts
            .max_tokens
            .saturating_sub(self.head_tokens() + JOIN_SLACK_TOKENS)
            .max(1);
        let pieces = if self.count(text) > room {
            // A table too long for a chunk is cut between rows.
            table_pieces(block, room, &self.opts.counter).unwrap_or_else(|| split_long(text, room, &self.opts.counter))
        } else {
            vec![text.to_string()]
        };
        for piece in pieces {
            if !self.body.is_empty() && self.count(&self.text_with(&[&piece])) > self.opts.max_tokens {
                self.flush();
            }
            self.body.push(piece);
            if self.blocks.last() != Some(&block.id) {
                self.blocks.push(block.id);
            }
            if !self.pages.contains(&block.page) {
                self.pages.push(block.page);
            }
        }
    }

    /// Ends the current chunk, if it has text.
    fn flush(&mut self) {
        if self.body.is_empty() {
            return;
        }
        let text = self.text_with(&[]);
        self.pages.sort_unstable();
        self.chunks.push(Chunk {
            id: self.chunks.len(),
            tokens: self.count(&text),
            text,
            title: self.title.clone(),
            section: self.section.clone(),
            blocks: std::mem::take(&mut self.blocks),
            pages: std::mem::take(&mut self.pages),
        });
        self.body.clear();
    }

    /// The text of the current chunk with `more` pieces added: the section
    /// path, then the pieces.
    fn text_with(&self, more: &[&str]) -> String {
        let body = self.body.iter().map(String::as_str).chain(more.iter().copied());
        std::iter::once(self.head.as_str())
            .filter(|head| !head.is_empty())
            .chain(body)
            .collect::<Vec<_>>()
            .join(BLOCK_SEPARATOR)
    }

    /// Tokens taken by the section path at the start of a chunk.
    fn head_tokens(&self) -> usize {
        if self.head.is_empty() {
            0
        } else {
            self.count(&format!("{}{BLOCK_SEPARATOR}", self.head))
        }
    }

    /// The number of tokens of `text`.
    fn count(&self, text: &str) -> usize {
        self.opts.counter.count(text)
    }
}

/// Cuts `text` into pieces of at most `budget` tokens: whole sentences
/// where they fit, a sentence longer than the budget between words, and a
/// word longer than the budget between characters.
fn split_long(text: &str, budget: usize, counter: &TokenCounter) -> Vec<String> {
    let mut pieces = Vec::new();
    let mut current = String::new();
    for sentence in sentences(text) {
        let candidate = if current.is_empty() {
            sentence.to_string()
        } else {
            format!("{current} {sentence}")
        };
        if counter.count(&candidate) <= budget {
            current = candidate;
            continue;
        }
        if !current.is_empty() {
            pieces.push(std::mem::take(&mut current));
        }
        let mut rest = sentence;
        while over_budget(rest, budget, counter).is_some() {
            let (head, tail) = cut(rest, budget, counter);
            pieces.push(head.to_string());
            rest = tail;
        }
        current = rest.to_string();
    }
    if !current.is_empty() {
        pieces.push(current);
    }
    pieces
}

/// The sentences of `text`: it is cut at white space that follows `.`, `!`
/// or `?` and comes before a capital, a digit or an opening bracket, so
/// that "e.g. the" and "Fig. 2a" are not cut while "end. The" is (a cut
/// after an abbreviation before a capital is harmless).
fn sentences(text: &str) -> Vec<&str> {
    let mut out = Vec::new();
    let mut start = 0;
    let mut prev: Option<char> = None;
    let mut chars = text.char_indices().peekable();
    while let Some((i, ch)) = chars.next() {
        if ch.is_whitespace() && prev.is_some_and(|last| matches!(last, '.' | '!' | '?')) {
            let mut end_ws = i + ch.len_utf8();
            while let Some(&(j, next)) = chars.peek() {
                if !next.is_whitespace() {
                    break;
                }
                end_ws = j + next.len_utf8();
                chars.next();
            }
            let next = text[end_ws..].chars().next();
            if next
                .is_some_and(|first| first.is_ascii_uppercase() || first.is_ascii_digit() || matches!(first, '(' | '['))
            {
                out.push(&text[start..i]);
                start = end_ws;
            }
            prev = Some(' ');
            continue;
        }
        prev = Some(ch);
    }
    out.push(&text[start..]);
    out.retain(|sentence| !sentence.trim().is_empty());
    out
}

/// Bytes of text first looked at for each token of a budget, by
/// [`over_budget`]: the window doubles until it holds more tokens.
const CUT_WINDOW_BYTES_PER_TOKEN: usize = 8;

/// The length of a head of `text` that holds more than `budget` tokens,
/// if the text does: heads twice as long each time are counted, so that
/// a long text is not counted whole for each piece cut from it. The count
/// grows with the head, so the text holds more than `budget` tokens when
/// a head does.
fn over_budget(text: &str, budget: usize, counter: &TokenCounter) -> Option<usize> {
    let mut window = text.len().min(budget.saturating_mul(CUT_WINDOW_BYTES_PER_TOKEN).max(1));
    loop {
        while !text.is_char_boundary(window) {
            window += 1;
        }
        if counter.count(&text[..window]) > budget {
            return Some(window);
        }
        if window == text.len() {
            return None;
        }
        window = text.len().min(window.saturating_mul(2));
    }
}

/// Cuts `text` (more than `budget` tokens) into its longest head of at most
/// `budget` tokens and the rest: between words, or between characters when
/// the first word alone is too long. The head is never empty.
fn cut<'t>(text: &'t str, budget: usize, counter: &TokenCounter) -> (&'t str, &'t str) {
    // The head is in the first bytes that hold more than `budget` tokens:
    // the ends are looked for there only, not in the whole text at each
    // cut (a block of 100,000 lines of code).
    let window = over_budget(text, budget, counter).unwrap_or(text.len());
    let head = &text[..window];
    // Ends of the candidate heads: after each word, else after each char.
    let mut ends: Vec<usize> = head
        .char_indices()
        .filter(|&(i, ch)| ch.is_whitespace() && i > 0 && !text[..i].ends_with(char::is_whitespace))
        .map(|(i, _)| i)
        .collect();
    if ends.first().is_none_or(|&end| counter.count(&text[..end]) > budget) {
        // The end of the window too: the end of the first char when the
        // window holds only it.
        let window_end = (head.len() < text.len()).then_some(head.len());
        ends = head.char_indices().skip(1).map(|(i, _)| i).chain(window_end).collect();
    }
    // The count grows with the head: find the last end that fits.
    let fits = ends.partition_point(|&end| counter.count(&text[..end]) <= budget);
    let end = if fits == 0 {
        ends.first().copied().unwrap_or(text.len())
    } else {
        ends[fits - 1]
    };
    (text[..end].trim_end(), text[end..].trim_start())
}

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

    /// A block of `kind` with `text`, on page 0.
    fn block(id: usize, kind: BlockKind, text: &str) -> Block {
        let mut block = Block::new(id, kind);
        block.spans = vec![Span::plain(text)];
        block
    }

    /// A document titled "A Paper" with these blocks.
    fn doc(blocks: Vec<Block>) -> Document {
        Document {
            meta: crate::model::Meta {
                title: Some("A Paper".into()),
                ..Default::default()
            },
            pages: Vec::new(),
            body_size: 10.0,
            leading: 12.0,
            body_font: String::new(),
            math_font: String::new(),
            blocks,
            pictures: Vec::new(),
        }
    }

    /// `cut` as it was before it looked in a window only: every end of the
    /// text is a candidate.
    fn cut_whole<'t>(text: &'t str, budget: usize, counter: &TokenCounter) -> (&'t str, &'t str) {
        let mut ends: Vec<usize> = text
            .char_indices()
            .filter(|&(i, ch)| ch.is_whitespace() && i > 0 && !text[..i].ends_with(char::is_whitespace))
            .map(|(i, _)| i)
            .collect();
        if ends.first().is_none_or(|&end| counter.count(&text[..end]) > budget) {
            ends = text.char_indices().skip(1).map(|(i, _)| i).collect();
        }
        let fits = ends.partition_point(|&end| counter.count(&text[..end]) <= budget);
        let end = if fits == 0 {
            ends.first().copied().unwrap_or(text.len())
        } else {
            ends[fits - 1]
        };
        (text[..end].trim_end(), text[end..].trim_start())
    }

    /// A long text is cut where it was when all of it was looked at for
    /// each piece, and `over_budget` tells the texts with too many tokens.
    #[test]
    fn long_texts_are_cut_as_when_looked_at_whole() {
        let counter = TokenCounter::default();
        let words: String = (0..3_000).map(|i| format!("word{} ", i % 37)).collect();
        let long_word = "x".repeat(5_000);
        let cjk = "漢字かな交じり文".repeat(400);
        for text in [words.as_str(), long_word.as_str(), cjk.as_str(), "a b", "é"] {
            for budget in [1, 2, 7, 50, 300, 100_000] {
                assert_eq!(
                    cut(text, budget, &counter),
                    cut_whole(text, budget, &counter),
                    "{budget}"
                );
                assert_eq!(
                    over_budget(text, budget, &counter).is_some(),
                    counter.count(text) > budget,
                    "{budget}"
                );
            }
        }
    }

    #[test]
    fn chunks_follow_sections_and_carry_their_path() {
        let doc = doc(vec![
            block(0, BlockKind::Heading { level: 1 }, "1 Intro"),
            block(1, BlockKind::Paragraph { in_abstract: false }, "First."),
            block(2, BlockKind::Heading { level: 2 }, "1.1 More"),
            block(3, BlockKind::Paragraph { in_abstract: false }, "Second."),
        ]);
        let chunks = render_chunks(&doc, &ChunkOptions::default());
        assert_eq!(chunks.len(), 2);
        assert_eq!(chunks[0].text, "1 Intro\n\nFirst.");
        assert_eq!(chunks[1].text, "1 Intro > 1.1 More\n\nSecond.");
        assert_eq!(chunks[1].section, ["1 Intro", "1.1 More"]);
        assert_eq!(chunks[1].blocks, [3]);
        assert_eq!(chunks[1].title.as_deref(), Some("A Paper"));
    }

    #[test]
    fn blocks_of_a_section_share_a_chunk_until_it_is_full() {
        let para = |id| block(id, BlockKind::Paragraph { in_abstract: false }, "one two three four");
        let doc = doc(vec![
            block(0, BlockKind::Heading { level: 1 }, "S"),
            para(1),
            para(2),
            para(3),
        ]);
        let opts = ChunkOptions {
            max_tokens: 9,
            ..Default::default()
        };
        // "S" + two paragraphs of 4 tokens = 9 tokens; the third goes on.
        let chunks = render_chunks(&doc, &opts);
        assert_eq!(
            chunks.iter().map(|chunk| chunk.blocks.clone()).collect::<Vec<_>>(),
            [vec![1, 2], vec![3]]
        );
        assert!(chunks.iter().all(|chunk| chunk.tokens <= 9));
    }

    #[test]
    fn a_long_block_is_cut_at_sentence_ends() {
        // Each word and each full stop is one token: two sentences make 6.
        let pieces = split_long("One two. Three four. Five six.", 6, &TokenCounter::estimate());
        assert_eq!(pieces, ["One two. Three four.", "Five six."]);
    }

    #[test]
    fn a_long_sentence_is_cut_between_words() {
        let pieces = split_long("a b c d e", 2, &TokenCounter::estimate());
        assert_eq!(pieces, ["a b", "c d", "e"]);
    }

    #[test]
    fn abbreviations_before_lower_case_do_not_end_sentences() {
        assert_eq!(
            sentences("See e.g. the table. Then go."),
            ["See e.g. the table.", "Then go."]
        );
    }

    /// A table float: caption `caption` (none when empty), `header_rows`
    /// header rows ["Year", "Price"] and `body` rows ["2000+i", "i.5"].
    fn table_block(caption: &str, header_rows: usize, body: usize) -> Block {
        use crate::model::{FloatKind, Table, TableCell, TableGeom};
        let cell = |text: &str, col: usize| TableCell {
            col,
            colspan: 1,
            rowspan: 1,
            spans: vec![Span::plain(text)],
        };
        let mut rows: Vec<Vec<TableCell>> = (0..header_rows)
            .map(|_| vec![cell("Year", 0), cell("Price", 1)])
            .collect();
        rows.extend((0..body).map(|i| vec![cell(&format!("{}", 2000 + i), 0), cell(&format!("{i}.5"), 1)]));
        let kind = BlockKind::Figure {
            kind: FloatKind::Table,
            image: None,
            label: None,
        };
        let mut block = block(0, kind, caption);
        block.table = Some(Table {
            n_cols: 2,
            header_rows,
            col_align: vec!["left", "right"],
            rows,
            rule_above: Vec::new(),
            rule_bottom: Vec::new(),
            rule_left: Vec::new(),
            rule_right: Vec::new(),
            geom: TableGeom::default(),
        });
        block
    }

    /// Options with chunks of at most `max_tokens`.
    fn opts(max_tokens: usize) -> ChunkOptions {
        ChunkOptions {
            max_tokens,
            ..Default::default()
        }
    }

    #[test]
    fn empty_and_spanned_cells_keep_the_columns_of_a_row() {
        use crate::model::TableCell;
        let cell = |col: usize, text: &str, colspan: usize, rowspan: usize| TableCell {
            col,
            colspan,
            rowspan,
            spans: vec![Span::plain(text)],
        };
        let mut block = table_block("", 1, 0);
        let table = block.table.as_mut().unwrap();
        table.n_cols = 5;
        table.rows = vec![
            ["STT", "Chỉ tiêu", "Đơn vị tính", "Cơ quan báo cáo", "Kỳ báo cáo"]
                .iter()
                .enumerate()
                .map(|(col, text)| cell(col, text, 1, 1))
                .collect(),
            // The unit and the period are empty: no cell is stored there.
            vec![
                cell(0, "I.", 1, 1),
                cell(1, "TỔNG SẢN PHẨM", 1, 1),
                cell(3, "BKHĐT", 1, 1),
            ],
            // A cell over two columns, and one over two rows.
            vec![
                cell(0, "1", 1, 2),
                cell(1, "GDP", 2, 1),
                cell(3, "TCTK", 1, 1),
                cell(4, "Năm", 1, 1),
            ],
            vec![
                cell(1, "Tốc độ", 1, 1),
                cell(2, "%", 1, 1),
                cell(3, "TCTK", 1, 1),
                cell(4, "Quý", 1, 1),
            ],
        ];
        let text = block_text(&block);
        assert_eq!(
            text.lines().collect::<Vec<_>>(),
            [
                "STT | Chỉ tiêu | Đơn vị tính | Cơ quan báo cáo | Kỳ báo cáo",
                "I. | TỔNG SẢN PHẨM |  | BKHĐT |",
                "1 | GDP |  | TCTK | Năm",
                "| Tốc độ | % | TCTK | Quý",
            ]
        );
    }

    #[test]
    fn a_long_row_cut_between_words_gives_no_piece_of_separators_only() {
        use crate::model::TableCell;
        let mut block = table_block("", 0, 0);
        let table = block.table.as_mut().unwrap();
        table.n_cols = 40;
        // One long cell, then 39 empty columns.
        table.rows = vec![vec![TableCell {
            col: 0,
            colspan: 1,
            rowspan: 1,
            spans: vec![Span::plain("word ".repeat(60))],
        }]];
        let chunks = render_chunks(&doc(vec![block]), &opts(32));
        assert!(chunks.len() > 1);
        for chunk in &chunks {
            assert!(
                chunk.text.chars().any(|ch| !(ch.is_whitespace() || ch == '|')),
                "a chunk of separators only: {:?}",
                chunk.text
            );
        }
    }

    #[test]
    fn a_long_table_is_cut_between_rows_and_repeats_its_header() {
        let chunks = render_chunks(&doc(vec![table_block("Table 1: Prices", 1, 200)]), &opts(64));
        assert!(chunks.len() > 1);
        for chunk in &chunks {
            assert!(chunk.text.contains("Table 1: Prices\nYear | Price\n"), "{}", chunk.text);
            assert!(chunk.tokens <= 64, "{}", chunk.text);
            assert!(
                chunk
                    .text
                    .lines()
                    .filter(|line| line.contains(" | "))
                    .all(|line| line.split(" | ").count() == 2)
            );
        }
        let rows: usize = chunks
            .iter()
            .map(|chunk| chunk.text.lines().filter(|line| line.starts_with('2')).count())
            .sum();
        assert_eq!(rows, 200);
    }

    #[test]
    fn a_head_too_long_to_repeat_is_kept_and_shortened() {
        // A caption longer than half a chunk: it is a chunk of its own, and
        // each chunk of rows starts with its start and the header row.
        let caption = format!("Table 1: {}", vec!["word"; 80].join(" "));
        let chunks = render_chunks(&doc(vec![table_block(&caption, 1, 200)]), &opts(64));
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.text.contains("word word word word word word word word word word"))
        );
        let with_rows: Vec<_> = chunks
            .iter()
            .filter(|chunk| chunk.text.lines().any(|line| line.starts_with("20")))
            .collect();
        assert!(!with_rows.is_empty());
        for chunk in with_rows {
            assert!(
                chunk.text.contains("Table 1:") && chunk.text.contains("Year | Price"),
                "{}",
                chunk.text
            );
            assert!(chunk.tokens <= 64, "{}", chunk.text);
        }
    }

    #[test]
    fn a_table_without_header_is_still_cut_between_rows() {
        let chunks = render_chunks(&doc(vec![table_block("", 0, 200)]), &opts(64));
        assert!(chunks.len() > 1);
        for chunk in &chunks {
            assert!(
                chunk.text.lines().all(|line| line.split(" | ").count() == 2),
                "{}",
                chunk.text
            );
            assert!(!chunk.text.contains("Year"));
        }
        let rows: usize = chunks.iter().map(|chunk| chunk.text.lines().count()).sum();
        assert_eq!(rows, 200);
    }

    #[test]
    fn a_row_longer_than_a_chunk_is_cut_between_words() {
        let mut block = table_block("", 1, 1);
        let long = vec!["word"; 300].join(" ");
        block.table.as_mut().expect("a table").rows[1][1].spans = vec![Span::plain(&long)];
        let chunks = render_chunks(&doc(vec![block]), &opts(64));
        assert!(chunks.len() > 1);
        for chunk in &chunks {
            assert!(chunk.text.starts_with("Year | Price\n"), "{}", chunk.text);
            assert!(chunk.tokens <= 64, "{}", chunk.text);
        }
        let words: usize = chunks
            .iter()
            .map(|chunk| chunk.text.split_whitespace().filter(|word| *word == "word").count())
            .sum();
        assert_eq!(words, 300);
    }

    #[test]
    fn a_table_that_fits_stays_whole_beside_its_paragraphs() {
        let para = block(1, BlockKind::Paragraph { in_abstract: false }, "Before.");
        let chunks = render_chunks(&doc(vec![para, table_block("Table 1", 1, 3)]), &ChunkOptions::default());
        assert_eq!(chunks.len(), 1);
        assert!(
            chunks[0]
                .text
                .ends_with("Table 1\nYear | Price\n2000 | 0.5\n2001 | 1.5\n2002 | 2.5")
        );
    }
}