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