image.vision\ocr\chinese_dictionary.mbt

82.7% · 115 / 139 points · 868 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/chinese_dictionary
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 中文字库管理与部件分解识别(v12.23.0 P2 中文增强)
  7. 7// 3500 常用字库管理、部首/部件分解、结构分析、相似度匹配
  8. 8
  9. 9///|
  10. 10/// 汉字结构类型
  11. 11/// `#non_exhaustive` 表示未来可能添加新构造器
  12. 12#derive(Eq, @debug.Debug)
  13. 13#non_exhaustive
  14. 14pub(all) enum DictCharStructure {
  15. 15 LeftRight // 左右结构
  16. 16 UpDown // 上下结构
  17. 17 LeftMiddleRight // 左中右结构
  18. 18 UpMiddleDown // 上中下结构
  19. 19 Surround // 包围结构
  20. 20 HalfSurround // 半包围结构
  21. 21 Independent // 独体字
  22. 22 Complex // 复杂结构
  23. 23}
  24. 24
  25. 25///|
  26. 26pub extend DictCharStructure with @debug.Debug::{to_repr}
  27. 27
  28. 28///|
  29. 29pub extend DictCharStructure with Eq::{not_equal, equal}
  30. 30
  31. 31///|
  32. 32/// 汉字部件
  33. 33#derive(Eq, @debug.Debug)
  34. 34pub(all) struct DictComponent {
  35. 35 name : String
  36. 36 stroke_count : Int32
  37. 37 position : String // "left" / "right" / "top" / "bottom" / "outer" / "inner"
  38. 38 width_ratio : Double
  39. 39 height_ratio : Double
  40. 40}
  41. 41
  42. 42///|
  43. 43pub extend DictComponent with Eq::{not_equal, equal}
  44. 44
  45. 45///|
  46. 46pub extend DictComponent with @debug.Debug::{to_repr}
  47. 47
  48. 48///|
  49. 49/// 汉字条目
  50. 50#derive(Eq, @debug.Debug)
  51. 51pub(all) struct DictCharEntry {
  52. 52 char : String
  53. 53 unicode : String
  54. 54 pinyin : Array[String]
  55. 55 stroke_count : Int32
  56. 56 structure : DictCharStructure
  57. 57 radical : String
  58. 58 components : Array[DictComponent]
  59. 59 frequency : Int32 // 使用频率(1-10000)
  60. 60 level : String // "一级" / "二级" / "三级"
  61. 61}
  62. 62
  63. 63///|
  64. 64pub extend DictCharEntry with Eq::{not_equal, equal}
  65. 65
  66. 66///|
  67. 67pub extend DictCharEntry with @debug.Debug::{to_repr}
  68. 68
  69. 69///|
  70. 70/// 中文字库
  71. 71#derive(Eq, @debug.Debug)
  72. 72pub(all) struct DictChineseDict {
  73. 73 chars : Array[DictCharEntry]
  74. 74 total_chars : Int32
  75. 75 level1_count : Int32
  76. 76 level2_count : Int32
  77. 77 level3_count : Int32
  78. 78 radical_index : Array[(String, Array[Int32])]
  79. 79 pinyin_index : Array[(String, Array[Int32])]
  80. 80}
  81. 81
  82. 82///|
  83. 83pub extend DictChineseDict with Eq::{not_equal, equal}
  84. 84
  85. 85///|
  86. 86pub extend DictChineseDict with @debug.Debug::{to_repr}
  87. 87
  88. 88///|
  89. 89/// 部件识别结果
  90. 90#derive(Eq, @debug.Debug)
  91. 91pub(all) struct DictComponentResult {
  92. 92 char : String
  93. 93 detected_components : Array[DictComponent]
  94. 94 detected_structure : DictCharStructure
  95. 95 confidence : Double
  96. 96 candidate_chars : Array[(String, Double)]
  97. 97}
  98. 98
  99. 99///|
  100. 100pub extend DictComponentResult with Eq::{not_equal, equal}
  101. 101
  102. 102///|
  103. 103pub extend DictComponentResult with @debug.Debug::{to_repr}
  104. 104
  105. 105///|
  106. 106/// 汉字相似度匹配结果
  107. 107#derive(Eq, @debug.Debug)
  108. 108pub(all) struct DictCharMatch {
  109. 109 target_char : String
  110. 110 matched_char : String
  111. 111 similarity : Double
  112. 112 structure_match : Bool
  113. 113 component_match_count : Int32
  114. 114 total_components : Int32
  115. 115 stroke_diff : Int32
  116. 116}
  117. 117
  118. 118///|
  119. 119pub extend DictCharMatch with Eq::{not_equal, equal}
  120. 120
  121. 121///|
  122. 122pub extend DictCharMatch with @debug.Debug::{to_repr}
  123. 123
  124. 124///|
  125. 125/// 获取常用汉字结构信息(简化版,覆盖高频字)
  126. 126fn get_dict_common_chinese_chars() -> Array[DictCharEntry] {
  127. 127● [
  128. 128 DictCharEntry::{
  129. 129 char: "的",
  130. 130 unicode: "U+7684",
  131. 131 pinyin: ["de", "di"],
  132. 132 stroke_count: 8,
  133. 133 structure: DictCharStructure::LeftRight,
  134. 134 radical: "白",
  135. 135 components: [
  136. 136 DictComponent::{
  137. 137 name: "白",
  138. 138 stroke_count: 5,
  139. 139 position: "left",
  140. 140 width_ratio: 0.45,
  141. 141 height_ratio: 1.0,
  142. 142 },
  143. 143 DictComponent::{
  144. 144 name: "勺",
  145. 145 stroke_count: 3,
  146. 146 position: "right",
  147. 147 width_ratio: 0.55,
  148. 148 height_ratio: 1.0,
  149. 149 },
  150. 150 ],
  151. 151 frequency: 10000,
  152. 152 level: "一级",
  153. 153 },
  154. 154 DictCharEntry::{
  155. 155 char: "一",
  156. 156 unicode: "U+4E00",
  157. 157 pinyin: ["yi"],
  158. 158 stroke_count: 1,
  159. 159 structure: DictCharStructure::Independent,
  160. 160 radical: "一",
  161. 161 components: [],
  162. 162 frequency: 9900,
  163. 163 level: "一级",
  164. 164 },
  165. 165 DictCharEntry::{
  166. 166 char: "是",
  167. 167 unicode: "U+662F",
  168. 168 pinyin: ["shi"],
  169. 169 stroke_count: 9,
  170. 170 structure: DictCharStructure::UpDown,
  171. 171 radical: "日",
  172. 172 components: [
  173. 173 DictComponent::{
  174. 174 name: "日",
  175. 175 stroke_count: 4,
  176. 176 position: "top",
  177. 177 width_ratio: 1.0,
  178. 178 height_ratio: 0.45,
  179. 179 },
  180. 180 DictComponent::{
  181. 181 name: "疋",
  182. 182 stroke_count: 5,
  183. 183 position: "bottom",
  184. 184 width_ratio: 1.0,
  185. 185 height_ratio: 0.55,
  186. 186 },
  187. 187 ],
  188. 188 frequency: 9800,
  189. 189 level: "一级",
  190. 190 },
  191. 191 DictCharEntry::{
  192. 192 char: "不",
  193. 193 unicode: "U+4E0D",
  194. 194 pinyin: ["bu"],
  195. 195 stroke_count: 4,
  196. 196 structure: DictCharStructure::Independent,
  197. 197 radical: "一",
  198. 198 components: [],
  199. 199 frequency: 9700,
  200. 200 level: "一级",
  201. 201 },
  202. 202 DictCharEntry::{
  203. 203 char: "了",
  204. 204 unicode: "U+4E86",
  205. 205 pinyin: ["le", "liao"],
  206. 206 stroke_count: 2,
  207. 207 structure: DictCharStructure::Independent,
  208. 208 radical: "乙",
  209. 209 components: [],
  210. 210 frequency: 9600,
  211. 211 level: "一级",
  212. 212 },
  213. 213 DictCharEntry::{
  214. 214 char: "人",
  215. 215 unicode: "U+4EBA",
  216. 216 pinyin: ["ren"],
  217. 217 stroke_count: 2,
  218. 218 structure: DictCharStructure::Independent,
  219. 219 radical: "人",
  220. 220 components: [],
  221. 221 frequency: 9500,
  222. 222 level: "一级",
  223. 223 },
  224. 224 DictCharEntry::{
  225. 225 char: "我",
  226. 226 unicode: "U+6211",
  227. 227 pinyin: ["wo"],
  228. 228 stroke_count: 7,
  229. 229 structure: DictCharStructure::LeftRight,
  230. 230 radical: "戈",
  231. 231 components: [
  232. 232 DictComponent::{
  233. 233 name: "手",
  234. 234 stroke_count: 4,
  235. 235 position: "left",
  236. 236 width_ratio: 0.4,
  237. 237 height_ratio: 1.0,
  238. 238 },
  239. 239 DictComponent::{
  240. 240 name: "戈",
  241. 241 stroke_count: 3,
  242. 242 position: "right",
  243. 243 width_ratio: 0.6,
  244. 244 height_ratio: 1.0,
  245. 245 },
  246. 246 ],
  247. 247 frequency: 9400,
  248. 248 level: "一级",
  249. 249 },
  250. 250 DictCharEntry::{
  251. 251 char: "在",
  252. 252 unicode: "U+5728",
  253. 253 pinyin: ["zai"],
  254. 254 stroke_count: 6,
  255. 255 structure: DictCharStructure::HalfSurround,
  256. 256 radical: "土",
  257. 257 components: [
  258. 258 DictComponent::{
  259. 259 name: "土",
  260. 260 stroke_count: 3,
  261. 261 position: "left",
  262. 262 width_ratio: 0.4,
  263. 263 height_ratio: 1.0,
  264. 264 },
  265. 265 DictComponent::{
  266. 266 name: "才",
  267. 267 stroke_count: 3,
  268. 268 position: "right",
  269. 269 width_ratio: 0.6,
  270. 270 height_ratio: 1.0,
  271. 271 },
  272. 272 ],
  273. 273 frequency: 9300,
  274. 274 level: "一级",
  275. 275 },
  276. 276 DictCharEntry::{
  277. 277 char: "有",
  278. 278 unicode: "U+6709",
  279. 279 pinyin: ["you"],
  280. 280 stroke_count: 6,
  281. 281 structure: DictCharStructure::UpDown,
  282. 282 radical: "月",
  283. 283 components: [
  284. 284 DictComponent::{
  285. 285 name: "𠂇",
  286. 286 stroke_count: 2,
  287. 287 position: "top",
  288. 288 width_ratio: 1.0,
  289. 289 height_ratio: 0.35,
  290. 290 },
  291. 291 DictComponent::{
  292. 292 name: "月",
  293. 293 stroke_count: 4,
  294. 294 position: "bottom",
  295. 295 width_ratio: 1.0,
  296. 296 height_ratio: 0.65,
  297. 297 },
  298. 298 ],
  299. 299 frequency: 9200,
  300. 300 level: "一级",
  301. 301 },
  302. 302 DictCharEntry::{
  303. 303 char: "中",
  304. 304 unicode: "U+4E2D",
  305. 305 pinyin: ["zhong"],
  306. 306 stroke_count: 4,
  307. 307 structure: DictCharStructure::Independent,
  308. 308 radical: "丨",
  309. 309 components: [],
  310. 310 frequency: 9100,
  311. 311 level: "一级",
  312. 312 },
  313. 313 DictCharEntry::{
  314. 314 char: "大",
  315. 315 unicode: "U+5927",
  316. 316 pinyin: ["da", "dai"],
  317. 317 stroke_count: 3,
  318. 318 structure: DictCharStructure::Independent,
  319. 319 radical: "大",
  320. 320 components: [],
  321. 321 frequency: 9000,
  322. 322 level: "一级",
  323. 323 },
  324. 324 DictCharEntry::{
  325. 325 char: "国",
  326. 326 unicode: "U+56FD",
  327. 327 pinyin: ["guo"],
  328. 328 stroke_count: 8,
  329. 329 structure: DictCharStructure::Surround,
  330. 330 radical: "囗",
  331. 331 components: [
  332. 332 DictComponent::{
  333. 333 name: "囗",
  334. 334 stroke_count: 3,
  335. 335 position: "outer",
  336. 336 width_ratio: 1.0,
  337. 337 height_ratio: 1.0,
  338. 338 },
  339. 339 DictComponent::{
  340. 340 name: "玉",
  341. 341 stroke_count: 5,
  342. 342 position: "inner",
  343. 343 width_ratio: 0.7,
  344. 344 height_ratio: 0.7,
  345. 345 },
  346. 346 ],
  347. 347 frequency: 8900,
  348. 348 level: "一级",
  349. 349 },
  350. 350 DictCharEntry::{
  351. 351 char: "和",
  352. 352 unicode: "U+548C",
  353. 353 pinyin: ["he", "huo", "hu"],
  354. 354 stroke_count: 8,
  355. 355 structure: DictCharStructure::LeftRight,
  356. 356 radical: "口",
  357. 357 components: [
  358. 358 DictComponent::{
  359. 359 name: "口",
  360. 360 stroke_count: 3,
  361. 361 position: "left",
  362. 362 width_ratio: 0.35,
  363. 363 height_ratio: 0.6,
  364. 364 },
  365. 365 DictComponent::{
  366. 366 name: "禾",
  367. 367 stroke_count: 5,
  368. 368 position: "right",
  369. 369 width_ratio: 0.65,
  370. 370 height_ratio: 1.0,
  371. 371 },
  372. 372 ],
  373. 373 frequency: 8800,
  374. 374 level: "一级",
  375. 375 },
  376. 376 DictCharEntry::{
  377. 377 char: "时",
  378. 378 unicode: "U+65F6",
  379. 379 pinyin: ["shi"],
  380. 380 stroke_count: 7,
  381. 381 structure: DictCharStructure::LeftRight,
  382. 382 radical: "日",
  383. 383 components: [
  384. 384 DictComponent::{
  385. 385 name: "日",
  386. 386 stroke_count: 4,
  387. 387 position: "left",
  388. 388 width_ratio: 0.4,
  389. 389 height_ratio: 0.7,
  390. 390 },
  391. 391 DictComponent::{
  392. 392 name: "寸",
  393. 393 stroke_count: 3,
  394. 394 position: "right",
  395. 395 width_ratio: 0.6,
  396. 396 height_ratio: 0.6,
  397. 397 },
  398. 398 ],
  399. 399 frequency: 8700,
  400. 400 level: "一级",
  401. 401 },
  402. 402 DictCharEntry::{
  403. 403 char: "地",
  404. 404 unicode: "U+5730",
  405. 405 pinyin: ["di", "de"],
  406. 406 stroke_count: 6,
  407. 407 structure: DictCharStructure::LeftRight,
  408. 408 radical: "土",
  409. 409 components: [
  410. 410 DictComponent::{
  411. 411 name: "土",
  412. 412 stroke_count: 3,
  413. 413 position: "left",
  414. 414 width_ratio: 0.35,
  415. 415 height_ratio: 1.0,
  416. 416 },
  417. 417 DictComponent::{
  418. 418 name: "也",
  419. 419 stroke_count: 3,
  420. 420 position: "right",
  421. 421 width_ratio: 0.65,
  422. 422 height_ratio: 1.0,
  423. 423 },
  424. 424 ],
  425. 425 frequency: 8600,
  426. 426 level: "一级",
  427. 427 },
  428. 428 DictCharEntry::{
  429. 429 char: "出",
  430. 430 unicode: "U+51FA",
  431. 431 pinyin: ["chu"],
  432. 432 stroke_count: 5,
  433. 433 structure: DictCharStructure::UpDown,
  434. 434 radical: "凵",
  435. 435 components: [
  436. 436 DictComponent::{
  437. 437 name: "凵",
  438. 438 stroke_count: 2,
  439. 439 position: "top",
  440. 440 width_ratio: 1.0,
  441. 441 height_ratio: 0.5,
  442. 442 },
  443. 443 DictComponent::{
  444. 444 name: "凵",
  445. 445 stroke_count: 2,
  446. 446 position: "bottom",
  447. 447 width_ratio: 1.0,
  448. 448 height_ratio: 0.5,
  449. 449 },
  450. 450 ],
  451. 451 frequency: 8500,
  452. 452 level: "一级",
  453. 453 },
  454. 454 DictCharEntry::{
  455. 455 char: "用",
  456. 456 unicode: "U+7528",
  457. 457 pinyin: ["yong"],
  458. 458 stroke_count: 5,
  459. 459 structure: DictCharStructure::Independent,
  460. 460 radical: "用",
  461. 461 components: [],
  462. 462 frequency: 8400,
  463. 463 level: "一级",
  464. 464 },
  465. 465 DictCharEntry::{
  466. 466 char: "工",
  467. 467 unicode: "U+5DE5",
  468. 468 pinyin: ["gong"],
  469. 469 stroke_count: 3,
  470. 470 structure: DictCharStructure::Independent,
  471. 471 radical: "工",
  472. 472 components: [],
  473. 473 frequency: 8300,
  474. 474 level: "一级",
  475. 475 },
  476. 476 DictCharEntry::{
  477. 477 char: "要",
  478. 478 unicode: "U+8981",
  479. 479 pinyin: ["yao"],
  480. 480 stroke_count: 9,
  481. 481 structure: DictCharStructure::UpDown,
  482. 482 radical: "覀",
  483. 483 components: [
  484. 484 DictComponent::{
  485. 485 name: "覀",
  486. 486 stroke_count: 6,
  487. 487 position: "top",
  488. 488 width_ratio: 1.0,
  489. 489 height_ratio: 0.55,
  490. 490 },
  491. 491 DictComponent::{
  492. 492 name: "女",
  493. 493 stroke_count: 3,
  494. 494 position: "bottom",
  495. 495 width_ratio: 0.6,
  496. 496 height_ratio: 0.45,
  497. 497 },
  498. 498 ],
  499. 499 frequency: 8200,
  500. 500 level: "一级",
  501. 501 },
  502. 502 DictCharEntry::{
  503. 503 char: "会",
  504. 504 unicode: "U+4F1A",
  505. 505 pinyin: ["hui", "kuai"],
  506. 506 stroke_count: 6,
  507. 507 structure: DictCharStructure::UpDown,
  508. 508 radical: "人",
  509. 509 components: [
  510. 510 DictComponent::{
  511. 511 name: "人",
  512. 512 stroke_count: 2,
  513. 513 position: "top",
  514. 514 width_ratio: 0.8,
  515. 515 height_ratio: 0.35,
  516. 516 },
  517. 517 DictComponent::{
  518. 518 name: "云",
  519. 519 stroke_count: 4,
  520. 520 position: "bottom",
  521. 521 width_ratio: 1.0,
  522. 522 height_ratio: 0.65,
  523. 523 },
  524. 524 ],
  525. 525 frequency: 8100,
  526. 526 level: "一级",
  527. 527 },
  528. 528 ]
  529. 529}
  530. 530
  531. 531///|
  532. 532/// 创建默认中文字库(高频 20 字 + 可扩展)
  533. 533pub fn create_default_chinese_dictionary() -> DictChineseDict {
  534. 534● let chars = get_dict_common_chinese_chars()
  535. 535 let mut level1 = 0
  536. 536 let mut level2 = 0
  537. 537 let mut level3 = 0
  538. 538 for char in chars {
  539. 539● match char.level {
  540. 540● "一级" => level1 += 1
  541. 541○ "二级" => level2 += 1
  542. 542○ _ => level3 += 1
  543. 543 }
  544. 544 }
  545. 545 let radical_index : Array[(String, Array[Int32])] = []
  546. 546 let pinyin_index : Array[(String, Array[Int32])] = []
  547. 547 DictChineseDict::{
  548. 548 chars,
  549. 549● total_chars: chars.length(),
  550. 550 level1_count: level1,
  551. 551 level2_count: level2,
  552. 552 level3_count: level3,
  553. 553 radical_index,
  554. 554 pinyin_index,
  555. 555 }
  556. 556}
  557. 557
  558. 558///|
  559. 559/// 根据部首查找汉字
  560. 560pub fn lookup_by_radical(
  561. 561 dict : DictChineseDict,
  562. 562 radical : String,
  563. 563) -> Array[DictCharEntry] raise @types.LoadError {
  564. 564 // 输入验证
  565. 565● if radical.length() == 0 {
  566. 566● raise @types.LoadError::DecodeFailed(
  567. 567 "lookup_by_radical: 部首不能为空",
  568. 568 )
  569. 569 }
  570. 570 let results : Array[DictCharEntry] = []
  571. 571 for char in dict.chars {
  572. 572● if char.radical == radical {
  573. 573● results.push(char)
  574. 574 }
  575. 575 }
  576. 576 results
  577. 577}
  578. 578
  579. 579///|
  580. 580/// 根据拼音查找汉字
  581. 581pub fn lookup_by_pinyin(
  582. 582 dict : DictChineseDict,
  583. 583 pinyin : String,
  584. 584) -> Array[DictCharEntry] raise @types.LoadError {
  585. 585 // 输入验证
  586. 586● if pinyin.length() == 0 {
  587. 587● raise @types.LoadError::DecodeFailed("lookup_by_pinyin: 拼音不能为空")
  588. 588 }
  589. 589 let results : Array[DictCharEntry] = []
  590. 590 for char in dict.chars {
  591. 591● for py in char.pinyin {
  592. 592● if py == pinyin {
  593. 593● results.push(char)
  594. 594 break
  595. 595 }
  596. 596 }
  597. 597 }
  598. 598 results
  599. 599}
  600. 600
  601. 601///|
  602. 602/// 根据结构类型查找汉字
  603. 603pub fn lookup_by_structure(
  604. 604 dict : DictChineseDict,
  605. 605 structure : DictCharStructure,
  606. 606) -> Array[DictCharEntry] {
  607. 607● let results : Array[DictCharEntry] = []
  608. 608 for char in dict.chars {
  609. 609● if char.structure == structure {
  610. 610● results.push(char)
  611. 611 }
  612. 612 }
  613. 613 results
  614. 614}
  615. 615
  616. 616///|
  617. 617/// 计算两个汉字的结构相似度
  618. 618pub fn calculate_chinese_similarity(
  619. 619 char1 : DictCharEntry,
  620. 620 char2 : DictCharEntry,
  621. 621) -> Double {
  622. 622● let mut score = 0.0
  623. 623 // 结构匹配(30%)
  624. 624 if char1.structure == char2.structure {
  625. 625● score += 0.3
  626. 626 }
  627. 627 // 部首匹配(20%)
  628. 628 if char1.radical == char2.radical {
  629. 629● score += 0.2
  630. 630 }
  631. 631 // 笔画数接近(20%)
  632. 632● let stroke_diff = (char1.stroke_count - char2.stroke_count).abs()
  633. 633 if stroke_diff == 0 {
  634. 634● score += 0.2
  635. 635● } else if stroke_diff <= 2 {
  636. 636● score += 0.15
  637. 637● } else if stroke_diff <= 4 {
  638. 638● score += 0.1
  639. 639 }
  640. 640 // 部件匹配(30%)
  641. 641 let mut component_match = 0
  642. 642 for c1 in char1.components {
  643. 643● for c2 in char2.components {
  644. 644● if c1.name == c2.name {
  645. 645● component_match += 1
  646. 646 break
  647. 647 }
  648. 648 }
  649. 649 }
  650. 650● let total_components = if char1.components.length() >
  651. 651● char2.components.length() {
  652. 652● char1.components.length()
  653. 653 } else {
  654. 654● char2.components.length()
  655. 655 }
  656. 656 if total_components > 0 {
  657. 657● score += component_match.to_double() / total_components.to_double() * 0.3
  658. 658 } else {
  659. 659● score += 0.15
  660. 660 }
  661. 661 // 都是独体字
  662. 662 score
  663. 663}
  664. 664
  665. 665///|
  666. 666/// 查找最相似的汉字(用于 OCR 纠错)
  667. 667pub fn find_similar_chinese_chars(
  668. 668 dict : DictChineseDict,
  669. 669 target_char : String,
  670. 670 top_n~ : Int32 = 5,
  671. 671) -> Array[DictCharMatch] raise @types.LoadError {
  672. 672 // 输入验证
  673. 673● if target_char.length() == 0 {
  674. 674● raise @types.LoadError::DecodeFailed(
  675. 675 "find_similar_chinese_chars: 目标字符不能为空",
  676. 676 )
  677. 677 }
  678. 678 if top_n <= 0 {
  679. 679○ raise @types.LoadError::DecodeFailed(
  680. 680 "find_similar_chinese_chars: 返回数量必须大于0",
  681. 681 )
  682. 682 }
  683. 683 let n = top_n
  684. 684● let target_entry = lookup_char(dict, target_char)
  685. 685 let matches : Array[DictCharMatch] = []
  686. 686● match target_entry {
  687. 687● Some(target) => {
  688. 688 for char in dict.chars {
  689. 689● if char.char != target_char {
  690. 690● let similarity = calculate_chinese_similarity(target, char)
  691. 691 if similarity > 0.3 {
  692. 692● let mut component_match = 0
  693. 693 for c1 in target.components {
  694. 694● for c2 in char.components {
  695. 695● if c1.name == c2.name {
  696. 696○ component_match += 1
  697. 697 break
  698. 698 }
  699. 699 }
  700. 700 }
  701. 701● matches.push(DictCharMatch::{
  702. 702 target_char,
  703. 703 matched_char: char.char,
  704. 704 similarity,
  705. 705 structure_match: target.structure == char.structure,
  706. 706 component_match_count: component_match,
  707. 707● total_components: if target.components.length() >
  708. 708● char.components.length() {
  709. 709○ target.components.length()
  710. 710 } else {
  711. 711● char.components.length()
  712. 712 },
  713. 713● stroke_diff: (target.stroke_count - char.stroke_count).abs(),
  714. 714 })
  715. 715 }
  716. 716 }
  717. 717 }
  718. 718 // 按相似度排序
  719. 719● for i in 0..<matches.length() {
  720. 720● for j in 0..<(matches.length() - i - 1) {
  721. 721● if matches[j].similarity < matches[j + 1].similarity {
  722. 722● let temp = matches[j]
  723. 723 matches[j] = matches[j + 1]
  724. 724 matches[j + 1] = temp
  725. 725 }
  726. 726 }
  727. 727 }
  728. 728 }
  729. 729○ None => ()
  730. 730 }
  731. 731 // 返回前 n 个
  732. 732 let results : Array[DictCharMatch] = []
  733. 733◐ let limit = if n < matches.length() { n } else { matches.length() }
  734. 734 for i in 0..<limit {
  735. 735● results.push(matches[i])
  736. 736 }
  737. 737 results
  738. 738}
  739. 739
  740. 740///|
  741. 741/// 查找汉字条目
  742. 742pub fn lookup_char(
  743. 743 dict : DictChineseDict,
  744. 744 char : String,
  745. 745) -> DictCharEntry? raise @types.LoadError {
  746. 746 // 输入验证
  747. 747● if char.length() == 0 {
  748. 748● raise @types.LoadError::DecodeFailed("lookup_char: 字符不能为空")
  749. 749 }
  750. 750 for entry in dict.chars {
  751. 751● if entry.char == char {
  752. 752● return Some(entry)
  753. 753 }
  754. 754 }
  755. 755 None
  756. 756}
  757. 757
  758. 758///|
  759. 759/// 基于部件的汉字识别(简化版)
  760. 760pub fn recognize_by_components(
  761. 761 dict : DictChineseDict,
  762. 762 detected_structure : DictCharStructure,
  763. 763 detected_components : Array[String],
  764. 764 stroke_count_hint~ : Int32 = 0,
  765. 765) -> Array[(String, Double)] {
  766. 766● let stroke_hint = stroke_count_hint
  767. 767 let candidates : Array[(String, Double)] = []
  768. 768 for char in dict.chars {
  769. 769● let mut score = 0.0
  770. 770 // 结构匹配
  771. 771 if char.structure == detected_structure {
  772. 772● score += 0.4
  773. 773 }
  774. 774 // 部件匹配
  775. 775 let mut component_match = 0
  776. 776 for detected in detected_components {
  777. 777● for comp in char.components {
  778. 778● if comp.name == detected {
  779. 779● component_match += 1
  780. 780 break
  781. 781 }
  782. 782 }
  783. 783 }
  784. 784● let total_detected = if detected_components.length() > 0 {
  785. 785● detected_components.length()
  786. 786 } else {
  787. 787○ 1
  788. 788 }
  789. 789● score += component_match.to_double() / total_detected.to_double() * 0.4
  790. 790 // 笔画数匹配
  791. 791 if stroke_hint > 0 {
  792. 792○ let stroke_diff = (char.stroke_count - stroke_hint).abs()
  793. 793 if stroke_diff == 0 {
  794. 794○ score += 0.2
  795. 795○ } else if stroke_diff <= 2 {
  796. 796○ score += 0.15
  797. 797○ } else if stroke_diff <= 4 {
  798. 798○ score += 0.1
  799. 799 }
  800. 800 } else {
  801. 801● score += 0.1
  802. 802 }
  803. 803 if score > 0.3 {
  804. 804● candidates.push((char.char, score))
  805. 805 }
  806. 806 }
  807. 807 // 排序
  808. 808● for i in 0..<candidates.length() {
  809. 809● for j in 0..<(candidates.length() - i - 1) {
  810. 810● if candidates[j].1 < candidates[j + 1].1 {
  811. 811● let temp = candidates[j]
  812. 812 candidates[j] = candidates[j + 1]
  813. 813 candidates[j + 1] = temp
  814. 814 }
  815. 815 }
  816. 816 }
  817. 817 candidates
  818. 818}
  819. 819
  820. 820///|
  821. 821/// 生成中文字库统计报告
  822. 822pub fn generate_chinese_dictionary_report(dict : DictChineseDict) -> String {
  823. 823● let mut report = "## 中文字库统计报告\n\n"
  824. 824 report = report + "### 整体统计\n\n"
  825. 825 report = report + "| 指标 | 数值 |\n"
  826. 826 report = report + "|------|------|\n"
  827. 827● report = report + "| 总字数 | " + dict.total_chars.to_string() + " |\n"
  828. 828● report = report + "| 一级字 | " + dict.level1_count.to_string() + " |\n"
  829. 829● report = report + "| 二级字 | " + dict.level2_count.to_string() + " |\n"
  830. 830● report = report + "| 三级字 | " + dict.level3_count.to_string() + " |\n\n"
  831. 831 report = report + "### 高频汉字 TOP 10\n\n"
  832. 832 report = report +
  833. 833 "| 排名 | 汉字 | 拼音 | 笔画 | 结构 | 部首 | 频率 |\n"
  834. 834 report = report + "|------|------|------|------|------|------|------|\n"
  835. 835 // 按频率排序(简化:直接取前10个)
  836. 836◐ let limit = if dict.chars.length() < 10 { dict.chars.length() } else { 10 }
  837. 837 for i in 0..<limit {
  838. 838● let char = dict.chars[i]
  839. 839◐ let pinyin_str = if char.pinyin.length() > 0 { char.pinyin[0] } else { "" }
  840. 840● let structure_str = match char.structure {
  841. 841● DictCharStructure::LeftRight => "左右"
  842. 842● DictCharStructure::UpDown => "上下"
  843. 843○ DictCharStructure::LeftMiddleRight => "左中右"
  844. 844○ DictCharStructure::UpMiddleDown => "上中下"
  845. 845○ DictCharStructure::Surround => "包围"
  846. 846● DictCharStructure::HalfSurround => "半包围"
  847. 847● DictCharStructure::Independent => "独体"
  848. 848○ DictCharStructure::Complex => "复杂"
  849. 849 }
  850. 850 report = report +
  851. 851 "| " +
  852. 852● (i + 1).to_string() +
  853. 853 " | " +
  854. 854 char.char +
  855. 855 " | " +
  856. 856 pinyin_str +
  857. 857 " | " +
  858. 858● char.stroke_count.to_string() +
  859. 859 " | " +
  860. 860 structure_str +
  861. 861 " | " +
  862. 862 char.radical +
  863. 863 " | " +
  864. 864● char.frequency.to_string() +
  865. 865 " |\n"
  866. 866 }
  867. 867 report
  868. 868}