image.vision\ocr\postprocess_correction.mbt
80% · 96 / 120 points · 696 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 后处理校正 ocr/postprocess_correction - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// OCR 后处理纠错模块(v12.22.0 P1 准确率提升) - 7
// 词典纠错、相似字符纠正、语言模型、上下文纠错、常见错误模式纠正 - 8
- 9
///| - 10
/// 纠错结果 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct CorrectionResult { - 13
original : String - 14
corrected : String - 15
is_corrected : Bool - 16
confidence : Double - 17
corrections : Array[CorrectionEntry] - 18
} - 19
- 20
///| - 21
pub extend CorrectionResult with Eq::{not_equal, equal} - 22
- 23
///| - 24
pub extend CorrectionResult with @debug.Debug::{to_repr} - 25
- 26
///| - 27
/// 纠错条目 - 28
#derive(Eq, @debug.Debug) - 29
pub(all) struct CorrectionEntry { - 30
position : Int32 - 31
original_char : String - 32
corrected_char : String - 33
reason : String - 34
confidence : Double - 35
} - 36
- 37
///| - 38
pub extend CorrectionEntry with Eq::{not_equal, equal} - 39
- 40
///| - 41
pub extend CorrectionEntry with @debug.Debug::{to_repr} - 42
- 43
///| - 44
/// 相似字符映射 - 45
#derive(Eq, @debug.Debug) - 46
pub(all) struct SimilarCharMapping { - 47
char : String - 48
similar_chars : Array[String] - 49
context_hint : String - 50
} - 51
- 52
///| - 53
pub extend SimilarCharMapping with Eq::{not_equal, equal} - 54
- 55
///| - 56
pub extend SimilarCharMapping with @debug.Debug::{to_repr} - 57
- 58
///| - 59
/// 常见错误模式 - 60
#derive(Eq, @debug.Debug) - 61
pub(all) struct CommonErrorPattern { - 62
pattern : String - 63
correction : String - 64
frequency : Int32 - 65
context_before : String - 66
context_after : String - 67
} - 68
- 69
///| - 70
pub extend CommonErrorPattern with Eq::{not_equal, equal} - 71
- 72
///| - 73
pub extend CommonErrorPattern with @debug.Debug::{to_repr} - 74
- 75
///| - 76
/// 词典条目 - 77
#derive(Eq, @debug.Debug) - 78
pub(all) struct DictionaryEntry { - 79
word : String - 80
frequency : Int32 - 81
category : String - 82
} - 83
- 84
///| - 85
pub extend DictionaryEntry with Eq::{not_equal, equal} - 86
- 87
///| - 88
pub extend DictionaryEntry with @debug.Debug::{to_repr} - 89
- 90
///| - 91
/// 语言模型状态 - 92
#derive(Eq, @debug.Debug) - 93
pub(all) struct LanguageModelState { - 94
dictionary : Array[DictionaryEntry] - 95
common_errors : Array[CommonErrorPattern] - 96
similar_chars : Array[SimilarCharMapping] - 97
total_words : Int32 - 98
total_corrections : Int32 - 99
} - 100
- 101
///| - 102
pub extend LanguageModelState with Eq::{not_equal, equal} - 103
- 104
///| - 105
pub extend LanguageModelState with @debug.Debug::{to_repr} - 106
- 107
///| - 108
/// 获取常见相似字符映射(英文/数字) - 109
fn get_english_similar_chars() -> Array[SimilarCharMapping] { - 110●
[ - 111
SimilarCharMapping::{ - 112
char: "0", - 113
similar_chars: ["O", "Q", "D"], - 114
context_hint: "数字中", - 115
}, - 116
SimilarCharMapping::{ - 117
char: "1", - 118
similar_chars: ["I", "l", "|"], - 119
context_hint: "数字中", - 120
}, - 121
SimilarCharMapping::{ - 122
char: "2", - 123
similar_chars: ["Z", "z"], - 124
context_hint: "数字中", - 125
}, - 126
SimilarCharMapping::{ - 127
char: "5", - 128
similar_chars: ["S", "s"], - 129
context_hint: "数字中", - 130
}, - 131
SimilarCharMapping::{ - 132
char: "8", - 133
similar_chars: ["B"], - 134
context_hint: "数字中", - 135
}, - 136
SimilarCharMapping::{ - 137
char: "O", - 138
similar_chars: ["0", "Q"], - 139
context_hint: "字母中", - 140
}, - 141
SimilarCharMapping::{ - 142
char: "I", - 143
similar_chars: ["1", "l", "|"], - 144
context_hint: "字母中", - 145
}, - 146
SimilarCharMapping::{ - 147
char: "l", - 148
similar_chars: ["1", "I", "|"], - 149
context_hint: "字母中", - 150
}, - 151
SimilarCharMapping::{ - 152
char: "Z", - 153
similar_chars: ["2"], - 154
context_hint: "字母中", - 155
}, - 156
SimilarCharMapping::{ - 157
char: "S", - 158
similar_chars: ["5"], - 159
context_hint: "字母中", - 160
}, - 161
SimilarCharMapping::{ - 162
char: "B", - 163
similar_chars: ["8"], - 164
context_hint: "字母中", - 165
}, - 166
SimilarCharMapping::{ - 167
char: "rn", - 168
similar_chars: ["m"], - 169
context_hint: "字母组合", - 170
}, - 171
SimilarCharMapping::{ - 172
char: "cl", - 173
similar_chars: ["d"], - 174
context_hint: "字母组合", - 175
}, - 176
SimilarCharMapping::{ - 177
char: "ol", - 178
similar_chars: ["al"], - 179
context_hint: "字母组合", - 180
}, - 181
] - 182
} - 183
- 184
///| - 185
/// 获取中文常见相似字符 - 186
fn get_chinese_similar_chars() -> Array[SimilarCharMapping] { - 187○
[ - 188
SimilarCharMapping::{ - 189
char: "己", - 190
similar_chars: ["已", "巳"], - 191
context_hint: "形近字", - 192
}, - 193
SimilarCharMapping::{ - 194
char: "已", - 195
similar_chars: ["己", "巳"], - 196
context_hint: "形近字", - 197
}, - 198
SimilarCharMapping::{ - 199
char: "未", - 200
similar_chars: ["末"], - 201
context_hint: "形近字", - 202
}, - 203
SimilarCharMapping::{ - 204
char: "末", - 205
similar_chars: ["未"], - 206
context_hint: "形近字", - 207
}, - 208
SimilarCharMapping::{ - 209
char: "日", - 210
similar_chars: ["曰"], - 211
context_hint: "形近字", - 212
}, - 213
SimilarCharMapping::{ - 214
char: "大", - 215
similar_chars: ["太", "犬"], - 216
context_hint: "形近字", - 217
}, - 218
SimilarCharMapping::{ - 219
char: "人", - 220
similar_chars: ["入", "八"], - 221
context_hint: "形近字", - 222
}, - 223
SimilarCharMapping::{ - 224
char: "土", - 225
similar_chars: ["士"], - 226
context_hint: "形近字", - 227
}, - 228
SimilarCharMapping::{ - 229
char: "王", - 230
similar_chars: ["玉", "主"], - 231
context_hint: "形近字", - 232
}, - 233
SimilarCharMapping::{ - 234
char: "刀", - 235
similar_chars: ["力", "刁"], - 236
context_hint: "形近字", - 237
}, - 238
SimilarCharMapping::{ - 239
char: "了", - 240
similar_chars: ["子", "孑"], - 241
context_hint: "形近字", - 242
}, - 243
SimilarCharMapping::{ - 244
char: "我", - 245
similar_chars: ["找", "伐"], - 246
context_hint: "形近字", - 247
}, - 248
SimilarCharMapping::{ - 249
char: "的", - 250
similar_chars: ["约", "钓"], - 251
context_hint: "形近字", - 252
}, - 253
SimilarCharMapping::{ - 254
char: "是", - 255
similar_chars: ["足", "走"], - 256
context_hint: "形近字", - 257
}, - 258
SimilarCharMapping::{ - 259
char: "不", - 260
similar_chars: ["卞", "木"], - 261
context_hint: "形近字", - 262
}, - 263
SimilarCharMapping::{ - 264
char: "在", - 265
similar_chars: ["有", "存"], - 266
context_hint: "形近字", - 267
}, - 268
] - 269
} - 270
- 271
///| - 272
/// 获取常见错误模式 - 273
fn get_common_error_patterns() -> Array[CommonErrorPattern] { - 274●
[ - 275
CommonErrorPattern::{ - 276
pattern: "OCR", - 277
correction: "OCR", - 278
frequency: 100, - 279
context_before: "", - 280
context_after: "", - 281
}, - 282
CommonErrorPattern::{ - 283
pattern: "the the", - 284
correction: "the", - 285
frequency: 80, - 286
context_before: "", - 287
context_after: "", - 288
}, - 289
CommonErrorPattern::{ - 290
pattern: "a a", - 291
correction: "a", - 292
frequency: 70, - 293
context_before: "", - 294
context_after: "", - 295
}, - 296
CommonErrorPattern::{ - 297
pattern: "is is", - 298
correction: "is", - 299
frequency: 60, - 300
context_before: "", - 301
context_after: "", - 302
}, - 303
CommonErrorPattern::{ - 304
pattern: "。。", - 305
correction: "。", - 306
frequency: 90, - 307
context_before: "", - 308
context_after: "", - 309
}, - 310
CommonErrorPattern::{ - 311
pattern: ",,", - 312
correction: ",", - 313
frequency: 85, - 314
context_before: "", - 315
context_after: "", - 316
}, - 317
CommonErrorPattern::{ - 318
pattern: "的的", - 319
correction: "的", - 320
frequency: 75, - 321
context_before: "", - 322
context_after: "", - 323
}, - 324
CommonErrorPattern::{ - 325
pattern: "了了", - 326
correction: "了", - 327
frequency: 65, - 328
context_before: "", - 329
context_after: "", - 330
}, - 331
CommonErrorPattern::{ - 332
pattern: "是是", - 333
correction: "是", - 334
frequency: 55, - 335
context_before: "", - 336
context_after: "", - 337
}, - 338
CommonErrorPattern::{ - 339
pattern: "在在", - 340
correction: "在", - 341
frequency: 50, - 342
context_before: "", - 343
context_after: "", - 344
}, - 345
] - 346
} - 347
- 348
///| - 349
/// 创建默认语言模型 - 350
pub fn create_default_language_model() -> LanguageModelState { - 351●
let dictionary : Array[DictionaryEntry] = [] - 352
LanguageModelState::{ - 353
dictionary, - 354●
common_errors: get_common_error_patterns(), - 355●
similar_chars: get_english_similar_chars(), - 356
total_words: 0, - 357
total_corrections: 0, - 358
} - 359
} - 360
- 361
///| - 362
/// 基于相似字符的单字符纠错 - 363
pub fn correct_similar_chars( - 364
text : String, - 365
context_type : String, // "number" / "english" / "chinese" / "mixed" - 366
) -> CorrectionResult raise @types.LoadError { - 367
// 输入验证 - 368●
if context_type != "number" && - 369
context_type != "english" && - 370
context_type != "chinese" && - 371
context_type != "mixed" { - 372●
raise @types.LoadError::DecodeFailed( - 373
"correct_similar_chars: 上下文类型必须是 number/english/chinese/mixed 之一", - 374
) - 375
} - 376●
let _ = match context_type { - 377●
"number" => get_english_similar_chars() - 378●
"english" => get_english_similar_chars() - 379○
"chinese" => get_chinese_similar_chars() - 380○
_ => get_english_similar_chars() - 381
} - 382
- 383
let corrections : Array[CorrectionEntry] = [] - 384
let mut corrected = text - 385
let mut is_corrected = false - 386
let mut total_confidence = 0.0 - 387
let mut correction_count = 0 - 388
- 389
// 简化版:基于上下文类型的字符替换 - 390
// 数字上下文中:O→0, I→1, l→1, Z→2, S→5, B→8 - 391
if context_type == "number" { - 392●
let mut new_text = "" - 393●
for i in 0..<text.length() { - 394●
let c = text[i] - 395●
let mut new_c = c.to_string() - 396
let mut corrected_here = false - 397
if c == 'O' || c == 'o' { - 398●
new_c = "0" - 399
corrected_here = true - 400●
} else if c == 'I' || c == 'l' || c == '|' { - 401●
new_c = "1" - 402
corrected_here = true - 403●
} else if c == 'Z' || c == 'z' { - 404○
new_c = "2" - 405
corrected_here = true - 406●
} else if c == 'S' || c == 's' { - 407●
new_c = "5" - 408
corrected_here = true - 409●
} else if c == 'B' { - 410○
new_c = "8" - 411
corrected_here = true - 412
} - 413
if corrected_here { - 414●
corrections.push(CorrectionEntry::{ - 415
position: i, - 416●
original_char: c.to_string(), - 417
corrected_char: new_c, - 418
reason: "数字上下文相似字符纠正", - 419
confidence: 0.85, - 420
}) - 421
is_corrected = true - 422
total_confidence += 0.85 - 423
correction_count += 1 - 424
} - 425
new_text = new_text + new_c - 426
} - 427
corrected = new_text - 428
} - 429
- 430
let avg_confidence = if correction_count > 0 { - 431●
total_confidence / correction_count.to_double() - 432
} else { - 433●
1.0 - 434
} - 435
- 436
CorrectionResult::{ - 437
original: text, - 438
corrected, - 439
is_corrected, - 440
confidence: avg_confidence, - 441
corrections, - 442
} - 443
} - 444
- 445
///| - 446
/// 基于常见错误模式的纠错 - 447
pub fn correct_common_patterns(text : String) -> CorrectionResult { - 448●
let patterns = get_common_error_patterns() - 449
let corrections : Array[CorrectionEntry] = [] - 450
let mut corrected = text - 451
let mut is_corrected = false - 452
let mut total_confidence = 0.0 - 453
let mut correction_count = 0 - 454
- 455
for pattern in patterns { - 456
// 简化版:只处理重复字符模式 - 457●
if pattern.pattern.length() == pattern.correction.length() * 2 { - 458
// 检测重复 - 459●
let _ = pattern.pattern.length() / 2 - 460
let mut i = 0 - 461●
while i < corrected.length() - pattern.pattern.length() + 1 { - 462●
let mut match_found = true - 463●
for j in 0..<pattern.pattern.length() { - 464●
if i + j < corrected.length() && - 465
corrected[i + j] != pattern.pattern[j] { - 466●
match_found = false - 467
break - 468
} - 469
} - 470
if match_found { - 471○
corrections.push(CorrectionEntry::{ - 472
position: i, - 473
original_char: pattern.pattern, - 474
corrected_char: pattern.correction, - 475
reason: "常见重复错误模式纠正", - 476
confidence: 0.9, - 477
}) - 478
is_corrected = true - 479
total_confidence += 0.9 - 480
correction_count += 1 - 481
// 替换 - 482
let mut new_text = "" - 483
for k in 0..<i { - 484○
new_text = new_text + corrected[k].to_string() - 485
} - 486
new_text = new_text + pattern.correction - 487○
for k in (i + pattern.pattern.length())..<corrected.length() { - 488○
new_text = new_text + corrected[k].to_string() - 489
} - 490
corrected = new_text - 491○
i += pattern.correction.length() - 492
} else { - 493●
i += 1 - 494
} - 495
} - 496
} - 497
} - 498
- 499
let avg_confidence = if correction_count > 0 { - 500○
total_confidence / correction_count.to_double() - 501
} else { - 502●
1.0 - 503
} - 504
- 505
CorrectionResult::{ - 506
original: text, - 507
corrected, - 508
is_corrected, - 509
confidence: avg_confidence, - 510
corrections, - 511
} - 512
} - 513
- 514
///| - 515
/// 完整的后处理纠错流水线 - 516
pub fn full_postprocess_correction( - 517
text : String, - 518
context_type~ : String = "mixed", - 519
) -> CorrectionResult raise @types.LoadError { - 520
// 输入验证 - 521●
if context_type != "number" && - 522
context_type != "english" && - 523
context_type != "chinese" && - 524
context_type != "mixed" { - 525●
raise @types.LoadError::DecodeFailed( - 526
"full_postprocess_correction: 上下文类型必须是 number/english/chinese/mixed 之一", - 527
) - 528
} - 529
let ctx = context_type - 530
- 531
// 第一步:相似字符纠正 - 532●
let step1 = correct_similar_chars(text, ctx) - 533
- 534
// 第二步:常见错误模式纠正 - 535●
let step2 = correct_common_patterns(step1.corrected) - 536
- 537
// 合并结果 - 538
let all_corrections : Array[CorrectionEntry] = [] - 539
for c in step1.corrections { - 540●
all_corrections.push(c) - 541
} - 542
for c in step2.corrections { - 543○
all_corrections.push(c) - 544
} - 545
- 546
let is_corrected = step1.is_corrected || step2.is_corrected - 547●
let avg_confidence = if all_corrections.length() > 0 { - 548●
let mut total = 0.0 - 549
for c in all_corrections { - 550●
total += c.confidence - 551
} - 552●
total / all_corrections.length().to_double() - 553
} else { - 554○
1.0 - 555
} - 556
- 557
CorrectionResult::{ - 558
original: text, - 559
corrected: step2.corrected, - 560
is_corrected, - 561
confidence: avg_confidence, - 562
corrections: all_corrections, - 563
} - 564
} - 565
- 566
///| - 567
/// 批量文本纠错 - 568
pub fn batch_correct_texts( - 569
texts : Array[String], - 570
context_type~ : String = "mixed", - 571
) -> Array[CorrectionResult] raise @types.LoadError { - 572
// 输入验证 - 573●
if texts.length() == 0 { - 574●
raise @types.LoadError::DecodeFailed( - 575
"batch_correct_texts: 文本列表不能为空", - 576
) - 577
} - 578
if context_type != "number" && - 579
context_type != "english" && - 580
context_type != "chinese" && - 581
context_type != "mixed" { - 582○
raise @types.LoadError::DecodeFailed( - 583
"batch_correct_texts: 上下文类型必须是 number/english/chinese/mixed 之一", - 584
) - 585
} - 586
let results : Array[CorrectionResult] = [] - 587
for text in texts { - 588●
let result = full_postprocess_correction(text, context_type~) - 589●
results.push(result) - 590
} - 591
results - 592
} - 593
- 594
///| - 595
/// 生成纠错报告 - 596
pub fn generate_correction_report(results : Array[CorrectionResult]) -> String { - 597●
let mut report = "## OCR 后处理纠错报告\n\n" - 598●
let total_texts = results.length() - 599
let mut corrected_texts = 0 - 600
let mut total_corrections = 0 - 601
let mut total_confidence = 0.0 - 602
- 603
for result in results { - 604●
if result.is_corrected { - 605●
corrected_texts += 1 - 606●
total_corrections += result.corrections.length() - 607
total_confidence += result.confidence - 608
} - 609
} - 610
- 611
let avg_confidence = if corrected_texts > 0 { - 612●
total_confidence / corrected_texts.to_double() - 613
} else { - 614○
1.0 - 615
} - 616
let correction_rate = if total_texts > 0 { - 617●
corrected_texts.to_double() / total_texts.to_double() * 100.0 - 618
} else { - 619○
0.0 - 620
} - 621
- 622
report = report + "### 整体统计\n\n" - 623
report = report + "| 指标 | 数值 |\n" - 624
report = report + "|------|------|\n" - 625●
report = report + "| 处理文本数 | " + total_texts.to_string() + " |\n" - 626
report = report + - 627
"| 纠错文本数 | " + - 628●
corrected_texts.to_string() + - 629
" |\n" - 630●
report = report + "| 纠错率 | " + correction_rate.to_string() + "% |\n" - 631
report = report + - 632
"| 总纠错次数 | " + - 633●
total_corrections.to_string() + - 634
" |\n" - 635
report = report + - 636
"| 平均置信度 | " + - 637●
(avg_confidence * 100.0).to_string() + - 638
"% |\n\n" - 639
- 640
report = report + "### 纠错详情\n\n" - 641●
for i in 0..<results.length() { - 642●
let result = results[i] - 643
if result.is_corrected { - 644●
report = report + "**文本 " + (i + 1).to_string() + "**\n\n" - 645
report = report + "- 原文: " + result.original + "\n" - 646
report = report + "- 纠正: " + result.corrected + "\n" - 647
report = report + - 648
"- 置信度: " + - 649●
(result.confidence * 100.0).to_string() + - 650
"%\n" - 651
report = report + - 652
"- 纠错次数: " + - 653●
result.corrections.length().to_string() + - 654
"\n\n" - 655
} - 656
} - 657
- 658
report - 659
} - 660
- 661
///| - 662
/// 计算纠错前后准确率提升 - 663
pub fn calculate_accuracy_improvement( - 664
before_results : Array[CorrectionResult], - 665
expected_texts : Array[String], - 666
) -> (Double, Double, Double) { - 667●
let mut before_accuracy = 0.0 - 668
let mut after_accuracy = 0.0 - 669
- 670●
for i in 0..<before_results.length() { - 671●
if i < expected_texts.length() { - 672●
let before = before_results[i].original - 673
let after = before_results[i].corrected - 674
let expected = expected_texts[i] - 675
- 676●
let before_correct = calculate_char_accuracy(expected, before) - 677●
let after_correct = calculate_char_accuracy(expected, after) - 678
- 679
before_accuracy += before_correct - 680
after_accuracy += after_correct - 681
} - 682
} - 683
- 684●
let count = if before_results.length() < expected_texts.length() { - 685○
before_results.length() - 686
} else { - 687●
expected_texts.length() - 688
} - 689
if count > 0 { - 690●
before_accuracy = before_accuracy / count.to_double() - 691●
after_accuracy = after_accuracy / count.to_double() - 692
} - 693
- 694
let improvement = after_accuracy - before_accuracy - 695
(before_accuracy, after_accuracy, improvement) - 696
}