ruvector-scipix 2.0.4

Rust OCR engine for scientific documents - extract LaTeX, MathML from math equations, research papers, and technical diagrams with ONNX GPU acceleration
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
//! Output Decoding Module
//!
//! This module provides various decoding strategies for converting
//! model output logits into text strings.

use super::{OcrError, Result};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::debug;

/// Decoder trait for converting logits to text
pub trait Decoder: Send + Sync {
    /// Decode logits to text
    fn decode(&self, logits: &[Vec<f32>]) -> Result<String>;

    /// Decode with confidence scores per character
    fn decode_with_confidence(&self, logits: &[Vec<f32>]) -> Result<(String, Vec<f32>)> {
        // Default implementation just returns uniform confidence
        let text = self.decode(logits)?;
        let confidences = vec![1.0; text.len()];
        Ok((text, confidences))
    }
}

/// Vocabulary mapping for character recognition
#[derive(Debug, Clone)]
pub struct Vocabulary {
    /// Index to character mapping
    idx_to_char: HashMap<usize, char>,
    /// Character to index mapping
    char_to_idx: HashMap<char, usize>,
    /// Blank token index for CTC
    blank_idx: usize,
}

impl Vocabulary {
    /// Create a new vocabulary
    pub fn new(chars: Vec<char>, blank_idx: usize) -> Self {
        let idx_to_char: HashMap<usize, char> =
            chars.iter().enumerate().map(|(i, &c)| (i, c)).collect();
        let char_to_idx: HashMap<char, usize> =
            chars.iter().enumerate().map(|(i, &c)| (c, i)).collect();

        Self {
            idx_to_char,
            char_to_idx,
            blank_idx,
        }
    }

    /// Get character by index
    pub fn get_char(&self, idx: usize) -> Option<char> {
        self.idx_to_char.get(&idx).copied()
    }

    /// Get index by character
    pub fn get_idx(&self, ch: char) -> Option<usize> {
        self.char_to_idx.get(&ch).copied()
    }

    /// Get blank token index
    pub fn blank_idx(&self) -> usize {
        self.blank_idx
    }

    /// Get vocabulary size
    pub fn size(&self) -> usize {
        self.idx_to_char.len()
    }
}

impl Default for Vocabulary {
    fn default() -> Self {
        // Default vocabulary: lowercase letters + digits + space + blank
        let mut chars = Vec::new();

        // Add lowercase letters
        for c in 'a'..='z' {
            chars.push(c);
        }

        // Add digits
        for c in '0'..='9' {
            chars.push(c);
        }

        // Add space
        chars.push(' ');

        // Blank token is at the end
        let blank_idx = chars.len();

        Self::new(chars, blank_idx)
    }
}

/// Greedy decoder - selects the character with highest probability at each step
pub struct GreedyDecoder {
    vocabulary: Arc<Vocabulary>,
}

impl GreedyDecoder {
    /// Create a new greedy decoder
    pub fn new(vocabulary: Arc<Vocabulary>) -> Self {
        Self { vocabulary }
    }

    /// Find the index with maximum value in a slice
    fn argmax(values: &[f32]) -> usize {
        values
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(idx, _)| idx)
            .unwrap_or(0)
    }
}

impl Decoder for GreedyDecoder {
    fn decode(&self, logits: &[Vec<f32>]) -> Result<String> {
        debug!("Greedy decoding {} frames", logits.len());

        let mut result = String::new();
        let mut prev_idx = None;

        for frame_logits in logits {
            let idx = Self::argmax(frame_logits);

            // Skip blank tokens and repeated characters
            if idx != self.vocabulary.blank_idx() && Some(idx) != prev_idx {
                if let Some(ch) = self.vocabulary.get_char(idx) {
                    result.push(ch);
                }
            }

            prev_idx = Some(idx);
        }

        Ok(result)
    }

    fn decode_with_confidence(&self, logits: &[Vec<f32>]) -> Result<(String, Vec<f32>)> {
        let mut result = String::new();
        let mut confidences = Vec::new();
        let mut prev_idx = None;

        for frame_logits in logits {
            let idx = Self::argmax(frame_logits);
            let confidence = softmax_max(frame_logits);

            // Skip blank tokens and repeated characters
            if idx != self.vocabulary.blank_idx() && Some(idx) != prev_idx {
                if let Some(ch) = self.vocabulary.get_char(idx) {
                    result.push(ch);
                    confidences.push(confidence);
                }
            }

            prev_idx = Some(idx);
        }

        Ok((result, confidences))
    }
}

/// Beam search decoder - maintains top-k hypotheses for better accuracy
pub struct BeamSearchDecoder {
    vocabulary: Arc<Vocabulary>,
    beam_width: usize,
}

impl BeamSearchDecoder {
    /// Create a new beam search decoder
    pub fn new(vocabulary: Arc<Vocabulary>, beam_width: usize) -> Self {
        Self {
            vocabulary,
            beam_width: beam_width.max(1),
        }
    }

    /// Get beam width
    pub fn beam_width(&self) -> usize {
        self.beam_width
    }
}

impl Decoder for BeamSearchDecoder {
    fn decode(&self, logits: &[Vec<f32>]) -> Result<String> {
        debug!(
            "Beam search decoding {} frames (beam_width: {})",
            logits.len(),
            self.beam_width
        );

        if logits.is_empty() {
            return Ok(String::new());
        }

        // Initialize beams: (text, score, last_idx)
        let mut beams: Vec<(String, f32, Option<usize>)> = vec![(String::new(), 0.0, None)];

        for frame_logits in logits {
            let mut new_beams = Vec::new();

            for (text, score, last_idx) in &beams {
                // Get top-k predictions for this frame
                let mut indexed_logits: Vec<(usize, f32)> = frame_logits
                    .iter()
                    .enumerate()
                    .map(|(i, &v)| (i, v))
                    .collect();
                indexed_logits.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());

                // Expand each beam with top-k predictions
                for (idx, logit) in indexed_logits.iter().take(self.beam_width) {
                    let new_score = score + logit;

                    // Skip blank tokens
                    if *idx == self.vocabulary.blank_idx() {
                        new_beams.push((text.clone(), new_score, Some(*idx)));
                        continue;
                    }

                    // Skip repeated characters (CTC collapse)
                    if Some(*idx) == *last_idx {
                        new_beams.push((text.clone(), new_score, Some(*idx)));
                        continue;
                    }

                    // Add character to beam
                    if let Some(ch) = self.vocabulary.get_char(*idx) {
                        let mut new_text = text.clone();
                        new_text.push(ch);
                        new_beams.push((new_text, new_score, Some(*idx)));
                    }
                }
            }

            // Keep top beam_width beams
            new_beams.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
            new_beams.truncate(self.beam_width);
            beams = new_beams;
        }

        // Return the best beam
        Ok(beams
            .first()
            .map(|(text, _, _)| text.clone())
            .unwrap_or_default())
    }
}

/// CTC (Connectionist Temporal Classification) decoder
pub struct CTCDecoder {
    vocabulary: Arc<Vocabulary>,
}

impl CTCDecoder {
    /// Create a new CTC decoder
    pub fn new(vocabulary: Arc<Vocabulary>) -> Self {
        Self { vocabulary }
    }

    /// Collapse repeated characters and remove blanks
    fn collapse_repeats(&self, indices: &[usize]) -> Vec<usize> {
        let mut result = Vec::new();
        let mut prev_idx = None;

        for &idx in indices {
            // Skip blanks
            if idx == self.vocabulary.blank_idx() {
                prev_idx = Some(idx);
                continue;
            }

            // Skip repeats
            if Some(idx) != prev_idx {
                result.push(idx);
            }

            prev_idx = Some(idx);
        }

        result
    }
}

impl Decoder for CTCDecoder {
    fn decode(&self, logits: &[Vec<f32>]) -> Result<String> {
        debug!("CTC decoding {} frames", logits.len());

        // Get best path (greedy)
        let indices: Vec<usize> = logits
            .iter()
            .map(|frame| GreedyDecoder::argmax(frame))
            .collect();

        // Collapse repeats and remove blanks
        let collapsed = self.collapse_repeats(&indices);

        // Convert to text
        let text: String = collapsed
            .iter()
            .filter_map(|&idx| self.vocabulary.get_char(idx))
            .collect();

        Ok(text)
    }

    fn decode_with_confidence(&self, logits: &[Vec<f32>]) -> Result<(String, Vec<f32>)> {
        let indices: Vec<usize> = logits
            .iter()
            .map(|frame| GreedyDecoder::argmax(frame))
            .collect();
        let confidences: Vec<f32> = logits.iter().map(|frame| softmax_max(frame)).collect();

        let collapsed = self.collapse_repeats(&indices);

        let text: String = collapsed
            .iter()
            .filter_map(|&idx| self.vocabulary.get_char(idx))
            .collect();

        // Map confidences to non-collapsed positions
        let mut result_confidences = Vec::new();
        let mut prev_idx = None;
        let mut confidence_idx = 0;

        for &idx in &indices {
            if idx != self.vocabulary.blank_idx() && Some(idx) != prev_idx {
                if confidence_idx < confidences.len() {
                    result_confidences.push(confidences[confidence_idx]);
                }
            }
            confidence_idx += 1;
            prev_idx = Some(idx);
        }

        Ok((text, result_confidences))
    }
}

/// Calculate softmax and return max probability
fn softmax_max(logits: &[f32]) -> f32 {
    if logits.is_empty() {
        return 0.0;
    }

    let max_logit = logits.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
    let exp_sum: f32 = logits.iter().map(|&x| (x - max_logit).exp()).sum();

    let max_exp = (logits.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b)) - max_logit).exp();
    max_exp / exp_sum
}

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

    fn create_test_vocabulary() -> Arc<Vocabulary> {
        Arc::new(Vocabulary::default())
    }

    #[test]
    fn test_vocabulary_default() {
        let vocab = Vocabulary::default();
        assert!(vocab.size() > 0);
        assert_eq!(vocab.get_char(0), Some('a'));
        assert_eq!(vocab.get_idx('a'), Some(0));
    }

    #[test]
    fn test_greedy_decoder() {
        let vocab = create_test_vocabulary();
        let decoder = GreedyDecoder::new(vocab.clone());

        // Mock logits for "hi"
        let h_idx = vocab.get_idx('h').unwrap();
        let i_idx = vocab.get_idx('i').unwrap();
        let blank = vocab.blank_idx();

        let mut logits = vec![
            vec![0.0; vocab.size() + 1],
            vec![0.0; vocab.size() + 1],
            vec![0.0; vocab.size() + 1],
        ];

        logits[0][h_idx] = 10.0;
        logits[1][blank] = 10.0;
        logits[2][i_idx] = 10.0;

        let result = decoder.decode(&logits).unwrap();
        assert_eq!(result, "hi");
    }

    #[test]
    fn test_beam_search_decoder() {
        let vocab = create_test_vocabulary();
        let decoder = BeamSearchDecoder::new(vocab.clone(), 3);

        assert_eq!(decoder.beam_width(), 3);

        let logits = vec![vec![0.0; vocab.size() + 1]; 5];
        let result = decoder.decode(&logits);
        assert!(result.is_ok());
    }

    #[test]
    fn test_ctc_decoder() {
        let vocab = create_test_vocabulary();
        let decoder = CTCDecoder::new(vocab.clone());

        // Test collapse repeats
        let a_idx = vocab.get_idx('a').unwrap();
        let b_idx = vocab.get_idx('b').unwrap();
        let blank = vocab.blank_idx();

        let indices = vec![a_idx, a_idx, blank, b_idx, b_idx, b_idx];
        let collapsed = decoder.collapse_repeats(&indices);

        assert_eq!(collapsed, vec![a_idx, b_idx]);
    }

    #[test]
    fn test_softmax_max() {
        let logits = vec![1.0, 2.0, 3.0, 2.0, 1.0];
        let max_prob = softmax_max(&logits);
        assert!(max_prob > 0.0 && max_prob <= 1.0);
        assert!(max_prob > 0.5); // The max should have high probability
    }

    #[test]
    fn test_empty_logits() {
        let vocab = create_test_vocabulary();
        let decoder = GreedyDecoder::new(vocab);

        let empty_logits: Vec<Vec<f32>> = vec![];
        let result = decoder.decode(&empty_logits).unwrap();
        assert_eq!(result, "");
    }
}