ocr 0.1.1

A pure Rust CLI OCR tool for printed text extraction
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
//! CJK (Chinese, Japanese, Korean) language support for OCR
//!
//! This module provides comprehensive support for CJK languages including
//! character detection, text segmentation, and language-specific processing.

use crate::utils::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// CJK language variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CJKLanguage {
    ChineseSimplified,
    ChineseTraditional,
    Japanese,
    Korean,
    Vietnamese,
}

/// CJK character categories
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CJKCharacterCategory {
    // Chinese characters
    ChineseSimplified,
    ChineseTraditional,
    ChineseCommon, // Characters common to both simplified and traditional

    // Japanese characters
    Hiragana,
    Katakana,
    Kanji, // Chinese characters used in Japanese

    // Korean characters
    HangulSyllables,
    HangulJamo,
    HangulCompatibilityJamo,

    // Punctuation and symbols
    CJKPunctuation,
    CJKSymbols,
    CJKRadicals,
    CJKStrokes,
}

/// CJK text segmentation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CJKSegmentationResult {
    pub segments: Vec<CJKSegment>,
    pub language: CJKLanguage,
    pub confidence: f32,
}

/// A segment of CJK text
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CJKSegment {
    pub text: String,
    pub start_pos: usize,
    pub end_pos: usize,
    pub category: CJKCharacterCategory,
    pub confidence: f32,
}

/// CJK text processor
pub struct CJKProcessor {
    chinese_dictionary: Option<ChineseDictionary>,
    japanese_dictionary: Option<JapaneseDictionary>,
    korean_dictionary: Option<KoreanDictionary>,
}

impl CJKProcessor {
    /// Create a new CJK processor
    pub fn new() -> Self {
        Self {
            chinese_dictionary: None,
            japanese_dictionary: None,
            korean_dictionary: None,
        }
    }

    /// Load Chinese dictionary
    pub async fn load_chinese_dictionary(&mut self, path: &str) -> Result<()> {
        // TODO: Implement Chinese dictionary loading
        self.chinese_dictionary = Some(ChineseDictionary::new());
        Ok(())
    }

    /// Load Japanese dictionary
    pub async fn load_japanese_dictionary(&mut self, path: &str) -> Result<()> {
        // TODO: Implement Japanese dictionary loading
        self.japanese_dictionary = Some(JapaneseDictionary::new());
        Ok(())
    }

    /// Load Korean dictionary
    pub async fn load_korean_dictionary(&mut self, path: &str) -> Result<()> {
        // TODO: Implement Korean dictionary loading
        self.korean_dictionary = Some(KoreanDictionary::new());
        Ok(())
    }

    /// Detect CJK language from text
    pub fn detect_cjk_language(&self, text: &str) -> Vec<(CJKLanguage, f32)> {
        let mut scores = HashMap::new();

        for c in text.chars() {
            if Self::is_chinese_character(c) {
                *scores.entry(CJKLanguage::ChineseSimplified).or_insert(0.0) += 1.0;
                *scores.entry(CJKLanguage::ChineseTraditional).or_insert(0.0) += 1.0;
            } else if Self::is_japanese_character(c) {
                *scores.entry(CJKLanguage::Japanese).or_insert(0.0) += 1.0;
            } else if Self::is_korean_character(c) {
                *scores.entry(CJKLanguage::Korean).or_insert(0.0) += 1.0;
            }
        }

        let total_chars = text.chars().count() as f32;
        if total_chars == 0.0 {
            return vec![];
        }

        scores
            .into_iter()
            .map(|(lang, score)| (lang, score / total_chars))
            .collect()
    }

    /// Segment CJK text
    pub fn segment_text(&self, text: &str, language: CJKLanguage) -> Result<CJKSegmentationResult> {
        match language {
            CJKLanguage::ChineseSimplified | CJKLanguage::ChineseTraditional => {
                self.segment_chinese(text, language)
            }
            CJKLanguage::Japanese => self.segment_japanese(text),
            CJKLanguage::Korean => self.segment_korean(text),
            CJKLanguage::Vietnamese => self.segment_vietnamese(text),
        }
    }

    /// Segment Chinese text
    fn segment_chinese(&self, text: &str, language: CJKLanguage) -> Result<CJKSegmentationResult> {
        let mut segments = Vec::new();
        let mut current_segment = String::new();
        let mut start_pos = 0;

        for (i, c) in text.char_indices() {
            if Self::is_chinese_character(c) {
                if current_segment.is_empty() {
                    start_pos = i;
                }
                current_segment.push(c);
            } else {
                if !current_segment.is_empty() {
                    let category = match language {
                        CJKLanguage::ChineseSimplified => CJKCharacterCategory::ChineseSimplified,
                        CJKLanguage::ChineseTraditional => CJKCharacterCategory::ChineseTraditional,
                        _ => CJKCharacterCategory::ChineseCommon,
                    };

                    segments.push(CJKSegment {
                        text: current_segment.clone(),
                        start_pos,
                        end_pos: i,
                        category,
                        confidence: 1.0,
                    });
                    current_segment.clear();
                }
            }
        }

        // Handle last segment
        if !current_segment.is_empty() {
            let category = match language {
                CJKLanguage::ChineseSimplified => CJKCharacterCategory::ChineseSimplified,
                CJKLanguage::ChineseTraditional => CJKCharacterCategory::ChineseTraditional,
                _ => CJKCharacterCategory::ChineseCommon,
            };

            segments.push(CJKSegment {
                text: current_segment,
                start_pos,
                end_pos: text.len(),
                category,
                confidence: 1.0,
            });
        }

        Ok(CJKSegmentationResult {
            segments,
            language,
            confidence: 1.0,
        })
    }

    /// Segment Japanese text
    fn segment_japanese(&self, text: &str) -> Result<CJKSegmentationResult> {
        let mut segments = Vec::new();
        let mut current_segment = String::new();
        let mut start_pos = 0;
        let mut current_category = None;

        for (i, c) in text.char_indices() {
            let category = if Self::is_hiragana(c) {
                Some(CJKCharacterCategory::Hiragana)
            } else if Self::is_katakana(c) {
                Some(CJKCharacterCategory::Katakana)
            } else if Self::is_kanji(c) {
                Some(CJKCharacterCategory::Kanji)
            } else {
                None
            };

            if let Some(cat) = category {
                if current_category == Some(cat) {
                    current_segment.push(c);
                } else {
                    if !current_segment.is_empty() {
                        segments.push(CJKSegment {
                            text: current_segment.clone(),
                            start_pos,
                            end_pos: i,
                            category: current_category.unwrap(),
                            confidence: 1.0,
                        });
                    }
                    current_segment = c.to_string();
                    start_pos = i;
                    current_category = Some(cat);
                }
            } else {
                if !current_segment.is_empty() {
                    segments.push(CJKSegment {
                        text: current_segment.clone(),
                        start_pos,
                        end_pos: i,
                        category: current_category.unwrap(),
                        confidence: 1.0,
                    });
                    current_segment.clear();
                    current_category = None;
                }
            }
        }

        // Handle last segment
        if !current_segment.is_empty() {
            segments.push(CJKSegment {
                text: current_segment,
                start_pos,
                end_pos: text.len(),
                category: current_category.unwrap(),
                confidence: 1.0,
            });
        }

        Ok(CJKSegmentationResult {
            segments,
            language: CJKLanguage::Japanese,
            confidence: 1.0,
        })
    }

    /// Segment Korean text
    fn segment_korean(&self, text: &str) -> Result<CJKSegmentationResult> {
        let mut segments = Vec::new();
        let mut current_segment = String::new();
        let mut start_pos = 0;

        for (i, c) in text.char_indices() {
            if Self::is_korean_character(c) {
                if current_segment.is_empty() {
                    start_pos = i;
                }
                current_segment.push(c);
            } else {
                if !current_segment.is_empty() {
                    let category =
                        if Self::is_hangul_syllable(current_segment.chars().next().unwrap()) {
                            CJKCharacterCategory::HangulSyllables
                        } else {
                            CJKCharacterCategory::HangulJamo
                        };

                    segments.push(CJKSegment {
                        text: current_segment.clone(),
                        start_pos,
                        end_pos: i,
                        category,
                        confidence: 1.0,
                    });
                    current_segment.clear();
                }
            }
        }

        // Handle last segment
        if !current_segment.is_empty() {
            let category = if Self::is_hangul_syllable(current_segment.chars().next().unwrap()) {
                CJKCharacterCategory::HangulSyllables
            } else {
                CJKCharacterCategory::HangulJamo
            };

            segments.push(CJKSegment {
                text: current_segment,
                start_pos,
                end_pos: text.len(),
                category,
                confidence: 1.0,
            });
        }

        Ok(CJKSegmentationResult {
            segments,
            language: CJKLanguage::Korean,
            confidence: 1.0,
        })
    }

    /// Segment Vietnamese text
    fn segment_vietnamese(&self, text: &str) -> Result<CJKSegmentationResult> {
        // Vietnamese uses Latin script with diacritics
        // For now, treat as regular text segmentation
        Ok(CJKSegmentationResult {
            segments: vec![CJKSegment {
                text: text.to_string(),
                start_pos: 0,
                end_pos: text.len(),
                category: CJKCharacterCategory::CJKSymbols, // Placeholder
                confidence: 1.0,
            }],
            language: CJKLanguage::Vietnamese,
            confidence: 1.0,
        })
    }

    /// Check if a character is Chinese
    pub fn is_chinese_character(c: char) -> bool {
        let code = c as u32;
        matches!(
            code,
            0x4E00..=0x9FFF | // CJK Unified Ideographs
            0x3400..=0x4DBF | // CJK Unified Ideographs Extension A
            0x20000..=0x2A6DF | // CJK Unified Ideographs Extension B
            0x2A700..=0x2B73F | // CJK Unified Ideographs Extension C
            0x2B740..=0x2B81F | // CJK Unified Ideographs Extension D
            0x2B820..=0x2CEAF | // CJK Unified Ideographs Extension E
            0x2CEB0..=0x2EBEF | // CJK Unified Ideographs Extension F
            0x30000..=0x3134F | // CJK Unified Ideographs Extension G
            0x31350..=0x323AF | // CJK Unified Ideographs Extension H
            0x323B0..=0x32B2F   // CJK Unified Ideographs Extension I
        )
    }

    /// Check if a character is Japanese Hiragana
    pub fn is_hiragana(c: char) -> bool {
        let code = c as u32;
        matches!(code, 0x3040..=0x309F)
    }

    /// Check if a character is Japanese Katakana
    pub fn is_katakana(c: char) -> bool {
        let code = c as u32;
        matches!(code, 0x30A0..=0x30FF)
    }

    /// Check if a character is Japanese Kanji
    pub fn is_kanji(c: char) -> bool {
        Self::is_chinese_character(c) // Kanji are Chinese characters used in Japanese
    }

    /// Check if a character is Japanese
    pub fn is_japanese_character(c: char) -> bool {
        Self::is_hiragana(c) || Self::is_katakana(c) || Self::is_kanji(c)
    }

    /// Check if a character is Korean
    pub fn is_korean_character(c: char) -> bool {
        let code = c as u32;
        matches!(
            code,
            0xAC00..=0xD7AF | // Hangul Syllables
            0x1100..=0x11FF | // Hangul Jamo
            0x3130..=0x318F   // Hangul Compatibility Jamo
        )
    }

    /// Check if a character is Hangul syllable
    pub fn is_hangul_syllable(c: char) -> bool {
        let code = c as u32;
        matches!(code, 0xAC00..=0xD7AF)
    }

    /// Check if a character is CJK
    pub fn is_cjk_character(c: char) -> bool {
        Self::is_chinese_character(c)
            || Self::is_japanese_character(c)
            || Self::is_korean_character(c)
    }

    /// Get character category
    pub fn get_character_category(c: char) -> Option<CJKCharacterCategory> {
        if Self::is_chinese_character(c) {
            Some(CJKCharacterCategory::ChineseCommon)
        } else if Self::is_hiragana(c) {
            Some(CJKCharacterCategory::Hiragana)
        } else if Self::is_katakana(c) {
            Some(CJKCharacterCategory::Katakana)
        } else if Self::is_kanji(c) {
            Some(CJKCharacterCategory::Kanji)
        } else if Self::is_hangul_syllable(c) {
            Some(CJKCharacterCategory::HangulSyllables)
        } else if Self::is_korean_character(c) {
            Some(CJKCharacterCategory::HangulJamo)
        } else {
            None
        }
    }
}

/// Chinese dictionary (placeholder)
pub struct ChineseDictionary {
    words: Vec<String>,
}

impl ChineseDictionary {
    pub fn new() -> Self {
        Self { words: Vec::new() }
    }
}

/// Japanese dictionary (placeholder)
pub struct JapaneseDictionary {
    words: Vec<String>,
}

impl JapaneseDictionary {
    pub fn new() -> Self {
        Self { words: Vec::new() }
    }
}

/// Korean dictionary (placeholder)
pub struct KoreanDictionary {
    words: Vec<String>,
}

impl KoreanDictionary {
    pub fn new() -> Self {
        Self { words: Vec::new() }
    }
}

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

    #[test]
    fn test_cjk_character_detection() {
        assert!(CJKProcessor::is_chinese_character(''));
        assert!(CJKProcessor::is_hiragana(''));
        assert!(CJKProcessor::is_katakana(''));
        assert!(CJKProcessor::is_korean_character(''));
        assert!(CJKProcessor::is_cjk_character(''));
        assert!(CJKProcessor::is_cjk_character(''));
        assert!(CJKProcessor::is_cjk_character(''));
    }

    #[test]
    fn test_language_detection() {
        let processor = CJKProcessor::new();
        let scores = processor.detect_cjk_language("中文");
        assert!(scores
            .iter()
            .any(|(lang, _)| *lang == CJKLanguage::ChineseSimplified));

        let scores = processor.detect_cjk_language("ひらがな");
        assert!(scores
            .iter()
            .any(|(lang, _)| *lang == CJKLanguage::Japanese));

        let scores = processor.detect_cjk_language("한글");
        assert!(scores.iter().any(|(lang, _)| *lang == CJKLanguage::Korean));
    }

    #[test]
    fn test_text_segmentation() {
        let processor = CJKProcessor::new();

        // Test Chinese segmentation
        let result = processor
            .segment_text("中文测试", CJKLanguage::ChineseSimplified)
            .unwrap();
        assert_eq!(result.language, CJKLanguage::ChineseSimplified);
        assert!(!result.segments.is_empty());

        // Test Japanese segmentation
        let result = processor
            .segment_text("ひらがなカタカナ", CJKLanguage::Japanese)
            .unwrap();
        assert_eq!(result.language, CJKLanguage::Japanese);
        assert!(!result.segments.is_empty());

        // Test Korean segmentation
        let result = processor
            .segment_text("한글테스트", CJKLanguage::Korean)
            .unwrap();
        assert_eq!(result.language, CJKLanguage::Korean);
        assert!(!result.segments.is_empty());
    }
}