libgrammstein 0.1.0

Hybrid language model (N-gram + Embeddings) for WFST text correction
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
//! Smart routing between PDF extraction backends.
//!
//! The router analyzes PDF documents and selects the optimal backend
//! based on document characteristics.

use super::backend::Backend;
use super::error::{PdfError, PdfResult};
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Configuration for the PDF router.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RouterConfig {
    /// Default backend when Auto is not specified.
    pub default_backend: Backend,

    /// Math density threshold for switching to Nougat (0.0 to 1.0).
    /// Documents with math density above this threshold use Nougat.
    pub math_density_threshold: f32,

    /// Page count threshold for preferring Marker (speed).
    /// Documents with more pages than this prefer Marker.
    pub page_count_threshold: usize,

    /// Whether to enable parallel page processing.
    pub parallel_pages: bool,

    /// Whether to fallback to alternate backend on failure.
    pub fallback_on_error: bool,

    /// Timeout in seconds before switching backends.
    pub timeout_switch_seconds: u64,
}

impl Default for RouterConfig {
    fn default() -> Self {
        Self {
            default_backend: Backend::Auto,
            math_density_threshold: 0.3,
            page_count_threshold: 50,
            parallel_pages: true,
            fallback_on_error: true,
            timeout_switch_seconds: 120,
        }
    }
}

impl RouterConfig {
    /// Create a config that always uses Marker.
    pub fn marker_only() -> Self {
        Self {
            default_backend: Backend::Marker,
            fallback_on_error: false,
            ..Default::default()
        }
    }

    /// Create a config that always uses Nougat.
    pub fn nougat_only() -> Self {
        Self {
            default_backend: Backend::Nougat,
            fallback_on_error: false,
            ..Default::default()
        }
    }

    /// Create a config optimized for math-heavy documents.
    pub fn math_optimized() -> Self {
        Self {
            default_backend: Backend::Auto,
            math_density_threshold: 0.1, // Lower threshold = more Nougat usage
            fallback_on_error: true,
            ..Default::default()
        }
    }

    /// Create a config optimized for speed.
    pub fn speed_optimized() -> Self {
        Self {
            default_backend: Backend::Auto,
            math_density_threshold: 0.5, // Higher threshold = more Marker usage
            page_count_threshold: 20,
            parallel_pages: true,
            ..Default::default()
        }
    }
}

/// Decision made by the router.
#[derive(Debug, Clone)]
pub struct RouterDecision {
    /// Selected backend.
    pub backend: Backend,
    /// Reason for selection.
    pub reason: String,
    /// Document analysis results.
    pub analysis: DocumentAnalysis,
    /// Fallback backend if primary fails.
    pub fallback: Option<Backend>,
}

/// Analysis of a PDF document.
#[derive(Debug, Clone, Default)]
pub struct DocumentAnalysis {
    /// Estimated number of pages.
    pub page_count: usize,
    /// Estimated math density (0.0 to 1.0).
    pub math_density: f32,
    /// Whether document contains tables.
    pub has_tables: bool,
    /// Whether document contains figures.
    pub has_figures: bool,
    /// Whether document appears to be an academic paper.
    pub is_academic: bool,
    /// File size in bytes.
    pub file_size: u64,
    /// PDF version if detectable.
    pub pdf_version: Option<String>,
}

/// Router for selecting PDF extraction backends.
pub struct PdfRouter {
    config: RouterConfig,
}

impl PdfRouter {
    /// Create a new router with the given configuration.
    pub fn new(config: RouterConfig) -> PdfResult<Self> {
        // Validate that at least one backend is available
        if !Backend::Marker.is_available() && !Backend::Nougat.is_available() {
            return Err(PdfError::BackendNotAvailable {
                backend: "any".into(),
                reason: "No PDF extraction backends available. Install marker-pdf or nougat-ocr."
                    .into(),
            });
        }

        Ok(Self { config })
    }

    /// Create a router with default configuration.
    pub fn default_router() -> PdfResult<Self> {
        Self::new(RouterConfig::default())
    }

    /// Route a PDF to the appropriate backend.
    pub fn route<P: AsRef<Path>>(&self, path: P) -> PdfResult<RouterDecision> {
        let path = path.as_ref();

        // Check file exists
        if !path.exists() {
            return Err(PdfError::FileNotFound(path.to_path_buf()));
        }

        // Analyze document
        let analysis = self.analyze_document(path)?;

        // If explicit backend configured, use it
        if self.config.default_backend != Backend::Auto {
            let backend = self.config.default_backend;
            let fallback = if self.config.fallback_on_error {
                self.get_fallback(backend)
            } else {
                None
            };

            return Ok(RouterDecision {
                backend,
                reason: format!("Explicitly configured to use {}", backend.name()),
                analysis,
                fallback,
            });
        }

        // Auto-select based on analysis
        self.auto_select(analysis)
    }

    /// Analyze a PDF document.
    fn analyze_document<P: AsRef<Path>>(&self, path: P) -> PdfResult<DocumentAnalysis> {
        let path = path.as_ref();

        let metadata = std::fs::metadata(path)?;
        let file_size = metadata.len();

        // Try to get page count and detect math from PDF
        let (page_count, math_density, has_tables, has_figures) = self
            .quick_pdf_analysis(path)
            .unwrap_or((0, 0.0, false, false));

        // Heuristic: academic papers typically have certain patterns
        let is_academic = self.detect_academic_paper(path, &metadata);

        Ok(DocumentAnalysis {
            page_count,
            math_density,
            has_tables,
            has_figures,
            is_academic,
            file_size,
            pdf_version: None,
        })
    }

    /// Quick analysis of PDF content.
    fn quick_pdf_analysis<P: AsRef<Path>>(&self, path: P) -> PdfResult<(usize, f32, bool, bool)> {
        let path = path.as_ref();

        // Try to read first few KB of PDF to detect patterns
        let content = std::fs::read(path).map_err(|e| PdfError::Io(e))?;

        // Count pages (look for /Type /Page in PDF)
        let content_str = String::from_utf8_lossy(&content);
        let page_count = content_str.matches("/Type /Page").count().max(1);

        // Estimate math density by looking for math-related patterns
        let math_indicators = [
            "equation",
            "\\frac",
            "\\sum",
            "\\int",
            "\\alpha",
            "\\beta",
            "\\gamma",
            "\\delta",
            "\\theta",
            "\\pi",
            "\\sigma",
            "\\partial",
            "\\nabla",
            "\\infty",
            "\\sqrt",
            "\\lim",
            "\\rightarrow",
            "\\Rightarrow",
            "\\leq",
            "\\geq",
            "\\neq",
            "\\approx",
            "\\times",
            "\\cdot",
            "\\mathbb",
            "\\mathcal",
        ];

        let math_count: usize = math_indicators
            .iter()
            .map(|&pat| content_str.matches(pat).count())
            .sum();

        // Normalize math density (heuristic)
        let math_density = (math_count as f32 / (page_count as f32 * 100.0)).min(1.0);

        // Check for table indicators
        let has_tables = content_str.contains("/Table") || content_str.contains("tabular");

        // Check for figure indicators
        let has_figures = content_str.contains("/Figure")
            || content_str.contains("/Image")
            || content_str.contains("/XObject");

        Ok((page_count, math_density, has_tables, has_figures))
    }

    /// Detect if document appears to be an academic paper.
    fn detect_academic_paper<P: AsRef<Path>>(
        &self,
        path: P,
        _metadata: &std::fs::Metadata,
    ) -> bool {
        let path = path.as_ref();

        // Check filename patterns common in academic papers
        let filename = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

        let academic_patterns = [
            "arxiv",
            "paper",
            "manuscript",
            "preprint",
            "journal",
            "conference",
            "proceedings",
        ];

        let filename_lower = filename.to_lowercase();
        academic_patterns
            .iter()
            .any(|&pat| filename_lower.contains(pat))
    }

    /// Auto-select backend based on analysis.
    fn auto_select(&self, analysis: DocumentAnalysis) -> PdfResult<RouterDecision> {
        let marker_available = Backend::Marker.is_available();
        let nougat_available = Backend::Nougat.is_available();

        // If only one backend available, use it
        if marker_available && !nougat_available {
            return Ok(RouterDecision {
                backend: Backend::Marker,
                reason: "Only Marker is available".to_string(),
                analysis,
                fallback: None,
            });
        }

        if nougat_available && !marker_available {
            return Ok(RouterDecision {
                backend: Backend::Nougat,
                reason: "Only Nougat is available".to_string(),
                analysis,
                fallback: None,
            });
        }

        // Both available - select based on analysis
        let (backend, reason) = if analysis.math_density > self.config.math_density_threshold {
            (
                Backend::Nougat,
                format!(
                    "High math density ({:.1}% > {:.1}% threshold)",
                    analysis.math_density * 100.0,
                    self.config.math_density_threshold * 100.0
                ),
            )
        } else if analysis.page_count > self.config.page_count_threshold {
            (
                Backend::Marker,
                format!(
                    "Large document ({} pages > {} threshold), preferring speed",
                    analysis.page_count, self.config.page_count_threshold
                ),
            )
        } else if analysis.is_academic && analysis.math_density > 0.1 {
            (
                Backend::Nougat,
                "Academic paper with math content".to_string(),
            )
        } else {
            (
                Backend::Marker,
                "Default to Marker for general documents".to_string(),
            )
        };

        let fallback = if self.config.fallback_on_error {
            self.get_fallback(backend)
        } else {
            None
        };

        Ok(RouterDecision {
            backend,
            reason,
            analysis,
            fallback,
        })
    }

    /// Get fallback backend.
    fn get_fallback(&self, primary: Backend) -> Option<Backend> {
        match primary {
            Backend::Marker if Backend::Nougat.is_available() => Some(Backend::Nougat),
            Backend::Nougat if Backend::Marker.is_available() => Some(Backend::Marker),
            _ => None,
        }
    }
}

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

    #[test]
    fn test_router_config_defaults() {
        let config = RouterConfig::default();
        assert_eq!(config.default_backend, Backend::Auto);
        assert_eq!(config.math_density_threshold, 0.3);
        assert!(config.fallback_on_error);
    }

    #[test]
    fn test_router_config_presets() {
        let marker = RouterConfig::marker_only();
        assert_eq!(marker.default_backend, Backend::Marker);
        assert!(!marker.fallback_on_error);

        let nougat = RouterConfig::nougat_only();
        assert_eq!(nougat.default_backend, Backend::Nougat);

        let math = RouterConfig::math_optimized();
        assert_eq!(math.math_density_threshold, 0.1);

        let speed = RouterConfig::speed_optimized();
        assert_eq!(speed.math_density_threshold, 0.5);
    }

    #[test]
    fn test_document_analysis_default() {
        let analysis = DocumentAnalysis::default();
        assert_eq!(analysis.page_count, 0);
        assert_eq!(analysis.math_density, 0.0);
        assert!(!analysis.has_tables);
        assert!(!analysis.is_academic);
    }
}