transmutation 0.3.2

High-performance document conversion engine for AI/LLM embeddings - 27 formats supported
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
//! Text sanitization and normalization utilities
//!
//! Based on docling's page_assemble_model.py text processing

#![allow(missing_docs, clippy::unused_self)]

use once_cell::sync::Lazy;
use regex::Regex;

/// Hyphen patterns for joining across line breaks
static HYPHEN_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\w+)-\s*\n\s*(\w+)").unwrap());

/// Multiple whitespace pattern
static MULTI_SPACE_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s{2,}").unwrap());

/// Line break pattern for joining
static LINE_BREAK_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"([^\n])\n([^\n])").unwrap());

/// Character normalization map (Unicode → ASCII/common forms)
/// Reference: docling/models/page_assemble_model.py lines 34-65
/// Extended with mathematical symbols, Greek letters, and special characters
const CHAR_NORMALIZATION_MAP: &[(&str, &str)] = &[
    // Fractions and special symbols
    ("", "/"),
    ("", "/"),
    // Quotes
    ("'", "'"),         // U+2018 Left single quotation mark
    ("'", "'"),         // U+2019 Right single quotation mark
    ("\u{201C}", "\""), // U+201C Left double quotation mark
    ("\u{201D}", "\""), // U+201D Right double quotation mark
    ("\u{201E}", "\""), // U+201E Double low-9 quotation mark
    ("\u{201F}", "\""), // U+201F Double high-reversed-9 quotation mark
    ("", "<"),         // single left-pointing angle quotation mark
    ("", ">"),         // single right-pointing angle quotation mark
    ("«", "<<"),        // left-pointing double angle quotation mark
    ("»", ">>"),        // right-pointing double angle quotation mark
    // Dashes and hyphens
    ("", "-"), // en dash
    ("", "-"), // em dash
    ("", "-"), // hyphen
    ("", "-"), // non-breaking hyphen
    ("", "-"), // minus sign
    ("", "-"), // figure dash
    ("", "-"), // horizontal bar
    // Bullets
    ("", "·"),
    ("", "·"),
    ("", "·"),
    ("", "o"),
    ("", "·"),
    ("", "o"),
    // Ellipsis
    ("", "..."),
    // Spaces
    (" ", " "), // non-breaking space
    (" ", " "), // thin space
    (" ", " "), // hair space
    (" ", " "), // en space
    (" ", " "), // em space
    // Math symbols - basic
    ("×", "x"),
    ("÷", "/"),
    ("±", "+/-"),
    ("", "-/+"),
    // Math symbols - comparison
    ("", "<="),
    ("", ">="),
    ("", "!="),
    ("", "~="),
    ("", "==="),
    ("", "!=="),
    // Math symbols - advanced
    ("", "infinity"),
    ("", "integral"),
    ("", "sum"),
    ("", "product"),
    ("", "sqrt"),
    ("", "cbrt"),
    ("", "fourthrt"),
    ("", "d"), // partial derivative
    ("", "delta"),
    ("", "nabla"),
    // Arrows
    ("", "->"),
    ("", "<-"),
    ("", "^"),
    ("", "v"),
    ("", "<->"),
    ("", "=>"),
    ("", "<="),
    ("", "<=>"),
    ("", "|->"),
    // Superscripts (for exponents in scientific notation)
    ("", "0"),
    ("¹", "1"),
    ("²", "2"),
    ("³", "3"),
    ("", "4"),
    ("", "5"),
    ("", "6"),
    ("", "7"),
    ("", "8"),
    ("", "9"),
    ("", "+"),
    ("", "-"),
    ("", "="),
    ("", "("),
    ("", ")"),
    // Subscripts
    ("", "0"),
    ("", "1"),
    ("", "2"),
    ("", "3"),
    ("", "4"),
    ("", "5"),
    ("", "6"),
    ("", "7"),
    ("", "8"),
    ("", "9"),
    ("", "+"),
    ("", "-"),
    ("", "="),
    ("", "("),
    ("", ")"),
    // Greek letters (common in scientific papers)
    ("α", "alpha"),
    ("β", "beta"),
    ("γ", "gamma"),
    ("δ", "delta"),
    ("ε", "epsilon"),
    ("ζ", "zeta"),
    ("η", "eta"),
    ("θ", "theta"),
    ("ι", "iota"),
    ("κ", "kappa"),
    ("λ", "lambda"),
    ("μ", "mu"),
    ("ν", "nu"),
    ("ξ", "xi"),
    ("ο", "omicron"),
    ("π", "pi"),
    ("ρ", "rho"),
    ("σ", "sigma"),
    ("τ", "tau"),
    ("υ", "upsilon"),
    ("φ", "phi"),
    ("χ", "chi"),
    ("ψ", "psi"),
    ("ω", "omega"),
    // Capital Greek letters
    ("Α", "Alpha"),
    ("Β", "Beta"),
    ("Γ", "Gamma"),
    ("Δ", "Delta"),
    ("Ε", "Epsilon"),
    ("Ζ", "Zeta"),
    ("Η", "Eta"),
    ("Θ", "Theta"),
    ("Ι", "Iota"),
    ("Κ", "Kappa"),
    ("Λ", "Lambda"),
    ("Μ", "Mu"),
    ("Ν", "Nu"),
    ("Ξ", "Xi"),
    ("Ο", "Omicron"),
    ("Π", "Pi"),
    ("Ρ", "Rho"),
    ("Σ", "Sigma"),
    ("Τ", "Tau"),
    ("Υ", "Upsilon"),
    ("Φ", "Phi"),
    ("Χ", "Chi"),
    ("Ψ", "Psi"),
    ("Ω", "Omega"),
    // Ligatures
    ("", "fi"),
    ("", "fl"),
    ("", "ff"),
    ("", "ffi"),
    ("", "ffl"),
    ("", "ft"),
    ("", "st"),
    // Special symbols
    ("©", "(c)"),
    ("®", "(R)"),
    ("", "(TM)"),
    ("°", " degrees"),
    ("§", "section"),
    ("", "paragraph"),
    ("", "+"),  // dagger
    ("", "++"), // double dagger
    ("¢", "cents"),
    ("£", "GBP"),
    ("¥", "JPY"),
    ("", "EUR"),
];

/// Text sanitizer for document text
#[derive(Debug)]
pub struct TextSanitizer {
    join_hyphens: bool,
    join_lines: bool,
    normalize_chars: bool,
    normalize_whitespace: bool,
}

impl TextSanitizer {
    /// Create new sanitizer with all options enabled
    pub fn new() -> Self {
        Self {
            join_hyphens: true,
            join_lines: true,
            normalize_chars: true,
            normalize_whitespace: true,
        }
    }

    /// Create sanitizer with custom options
    pub fn with_options(
        join_hyphens: bool,
        join_lines: bool,
        normalize_chars: bool,
        normalize_whitespace: bool,
    ) -> Self {
        Self {
            join_hyphens,
            join_lines,
            normalize_chars,
            normalize_whitespace,
        }
    }

    /// Sanitize text with all configured options
    pub fn sanitize(&self, text: &str) -> String {
        let mut result = text.to_string();

        if self.normalize_chars {
            result = self.normalize_characters(&result);
        }

        if self.join_hyphens {
            result = self.join_hyphenated_words(&result);
        }

        if self.join_lines {
            result = self.join_lines_with_space(&result);
        }

        if self.normalize_whitespace {
            result = self.normalize_whitespace_chars(&result);
        }

        result.trim().to_string()
    }

    /// Join hyphenated words across line breaks
    /// "word-\nword" → "wordword"
    fn join_hyphenated_words(&self, text: &str) -> String {
        HYPHEN_PATTERN.replace_all(text, "$1$2").to_string()
    }

    /// Join lines with spaces (but preserve paragraph breaks)
    /// "line1\nline2" → "line1 line2"
    /// But "line1\n\nline2" stays as is
    fn join_lines_with_space(&self, text: &str) -> String {
        LINE_BREAK_PATTERN.replace_all(text, "$1 $2").to_string()
    }

    /// Normalize special characters to common forms
    fn normalize_characters(&self, text: &str) -> String {
        let mut result = text.to_string();

        for (from, to) in CHAR_NORMALIZATION_MAP {
            result = result.replace(from, to);
        }

        result
    }

    /// Normalize multiple whitespace to single space
    fn normalize_whitespace_chars(&self, text: &str) -> String {
        MULTI_SPACE_PATTERN.replace_all(text, " ").to_string()
    }
}

impl Default for TextSanitizer {
    fn default() -> Self {
        Self::new()
    }
}

/// Quick sanitize function with default options
pub fn sanitize_text(text: &str) -> String {
    TextSanitizer::new().sanitize(text)
}

/// Join text from multiple cells/lines with proper spacing
///
/// This handles spacing based on position (if available) or falls back
/// to simple joining with spaces.
pub fn join_text_cells(texts: &[&str], add_spaces: bool) -> String {
    if texts.is_empty() {
        return String::new();
    }

    if add_spaces {
        texts.join(" ")
    } else {
        texts.concat()
    }
}

/// Detect if text is likely a heading/title based on patterns
///
/// Heuristics:
/// - Short length (< 100 chars)
/// - No ending punctuation (., ?, !)
/// - Mostly capitalized or all caps
/// - Contains numbers (section numbers)
pub fn is_likely_heading(text: &str) -> bool {
    let text = text.trim();

    if text.is_empty() || text.len() > 100 {
        return false;
    }

    // Check if ends with sentence-ending punctuation
    if text.ends_with('.') || text.ends_with('?') || text.ends_with('!') {
        return false;
    }

    // Check capitalization
    let uppercase_ratio = text
        .chars()
        .filter(|c| c.is_alphabetic())
        .filter(|c| c.is_uppercase())
        .count() as f32
        / text.chars().filter(|c| c.is_alphabetic()).count().max(1) as f32;

    // High uppercase ratio suggests heading
    if uppercase_ratio > 0.7 {
        return true;
    }

    // Check for section numbers (1.2, 1.2.3, etc.)
    let section_number_pattern = Regex::new(r"^\d+(\.\d+)*\.?\s").unwrap();
    if section_number_pattern.is_match(text) {
        return true;
    }

    false
}

/// Extract section number from heading text
/// "1.2.3 Introduction" → Some("1.2.3")
pub fn extract_section_number(text: &str) -> Option<String> {
    let section_pattern = Regex::new(r"^(\d+(\.\d+)*)\.?\s").unwrap();

    section_pattern
        .captures(text)
        .and_then(|caps| caps.get(1))
        .map(|m| m.as_str().to_string())
}

/// Calculate section level from number
/// "1" → 1, "1.2" → 2, "1.2.3" → 3
pub fn calculate_section_level(section_number: &str) -> usize {
    section_number.split('.').count()
}

/// Remove common PDF artifacts
/// - Zero-width spaces
/// - Soft hyphens
/// - Control characters
pub fn remove_pdf_artifacts(text: &str) -> String {
    text.chars()
        .filter(|&c| {
            // Remove zero-width and control characters
            !matches!(
                c,
                '\u{200B}'  // zero-width space
                | '\u{200C}'  // zero-width non-joiner
                | '\u{200D}'  // zero-width joiner
                | '\u{FEFF}'  // zero-width no-break space
                | '\u{00AD}'  // soft hyphen
                | '\0'..='\u{001F}' // control characters (except newline/tab)
            ) || c == '\n'
                || c == '\t'
        })
        .collect()
}

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

    #[test]
    fn test_hyphen_joining() {
        let sanitizer = TextSanitizer::new();
        let text = "This is a hyphen-\nated word.";
        let result = sanitizer.sanitize(text);
        assert_eq!(result, "This is a hyphenated word.");
    }

    #[test]
    fn test_line_joining() {
        let sanitizer = TextSanitizer::new();
        let text = "Line one\nLine two";
        let result = sanitizer.sanitize(text);
        assert_eq!(result, "Line one Line two");
    }

    #[test]
    fn test_character_normalization() {
        let sanitizer = TextSanitizer::new();
        let text = "Price: $100⁄month — \u{201C}special\u{201D} offer";
        let result = sanitizer.sanitize(text);
        assert_eq!(result, "Price: $100/month - \"special\" offer");
    }

    #[test]
    fn test_whitespace_normalization() {
        let sanitizer = TextSanitizer::new();
        let text = "Too    many     spaces";
        let result = sanitizer.sanitize(text);
        assert_eq!(result, "Too many spaces");
    }

    #[test]
    fn test_is_likely_heading() {
        assert!(is_likely_heading("1. Introduction"));
        assert!(is_likely_heading("CHAPTER 1"));
        assert!(is_likely_heading("1.2.3 Methods"));
        assert!(!is_likely_heading("This is a regular sentence."));
        assert!(!is_likely_heading(
            "This is a very long text that goes on and on and definitely should not be considered a heading because it's way too long."
        ));
    }

    #[test]
    fn test_extract_section_number() {
        assert_eq!(
            extract_section_number("1.2.3 Methods"),
            Some("1.2.3".to_string())
        );
        assert_eq!(
            extract_section_number("1. Introduction"),
            Some("1".to_string())
        );
        assert_eq!(extract_section_number("No number here"), None);
    }

    #[test]
    fn test_calculate_section_level() {
        assert_eq!(calculate_section_level("1"), 1);
        assert_eq!(calculate_section_level("1.2"), 2);
        assert_eq!(calculate_section_level("1.2.3"), 3);
        assert_eq!(calculate_section_level("1.2.3.4"), 4);
    }

    #[test]
    fn test_remove_pdf_artifacts() {
        let text = "Hello\u{200B}World\u{00AD}Test";
        let result = remove_pdf_artifacts(text);
        assert_eq!(result, "HelloWorldTest");
    }

    #[test]
    fn test_ligature_normalization() {
        let sanitizer = TextSanitizer::new();
        let text = "file with ligatures: ff, fi, fl";
        let result = sanitizer.sanitize(text);
        assert_eq!(result, "file with ligatures: ff, fi, fl");
    }
}