image.vision\ocr\postprocess_correction.mbt

80% · 96 / 120 points · 696 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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