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
512
513
514
515
516
517
518
519
520
// 文档结构理解与 Markdown 还原(v12.18.0)
// 表格转 Markdown、图片提取、符号识别、版面元素分类、阅读顺序、1:1 还原
///|
/// Markdown 元素类型
pub(all) enum MarkdownElementType {
Title
Heading1
Heading2
Heading3
Paragraph
ListItem
Table
Image
Quote
CodeBlock
HorizontalRule
PageBreak
Unknown
} derive(Eq)
///|
/// Markdown 元素
pub(all) struct MarkdownElement {
element_type : MarkdownElementType
x : Int
y : Int
width : Int
height : Int
content : String
confidence : Double
}
///|
/// 表格数据
pub(all) struct MarkdownTable {
rows : Int
cols : Int
cells : Array[Array[String]]
has_header : Bool
}
///|
/// 图片区域
pub(all) struct MarkdownImage {
x : Int
y : Int
width : Int
height : Int
image_type : String
confidence : Double
}
///|
/// 符号识别结果
pub(all) struct MarkdownSymbol {
symbol : String
unicode : String
category : String
x : Int
y : Int
confidence : Double
}
///|
/// 完整 Markdown 文档
pub(all) struct MarkdownDocument {
title : String
elements : Array[MarkdownElement]
tables : Array[MarkdownTable]
images : Array[MarkdownImage]
symbols : Array[MarkdownSymbol]
reading_order : Array[Int]
}
///|
/// 常见符号映射表
fn get_symbol_map() -> Array[(String, String, String)] {
[
("。", "U+3002", "punctuation"),
(",", "U+FF0C", "punctuation"),
(";", "U+FF1B", "punctuation"),
(":", "U+FF1A", "punctuation"),
("?", "U+FF1F", "punctuation"),
("!", "U+FF01", "punctuation"),
("(", "U+FF08", "punctuation"),
(")", "U+FF09", "punctuation"),
("【", "U+3010", "punctuation"),
("】", "U+3011", "punctuation"),
("《", "U+300A", "punctuation"),
("》", "U+300B", "punctuation"),
("—", "U+2014", "punctuation"),
("…", "U+2026", "punctuation"),
("±", "U+00B1", "math"),
("×", "U+00D7", "math"),
("÷", "U+00F7", "math"),
("≠", "U+2260", "math"),
("≤", "U+2264", "math"),
("≥", "U+2265", "math"),
("∞", "U+221E", "math"),
("√", "U+221A", "math"),
("π", "U+03C0", "math"),
("°", "U+00B0", "math"),
("¥", "U+00A5", "currency"),
("$", "U+0024", "currency"),
("€", "U+20AC", "currency"),
("£", "U+00A3", "currency"),
("→", "U+2192", "arrow"),
("←", "U+2190", "arrow"),
("↑", "U+2191", "arrow"),
("↓", "U+2193", "arrow"),
("★", "U+2605", "special"),
("☆", "U+2606", "special"),
("●", "U+25CF", "special"),
("○", "U+25CB", "special"),
("■", "U+25A0", "special"),
("□", "U+25A1", "special"),
("▲", "U+25B2", "special"),
("△", "U+25B3", "special"),
("◆", "U+25C6", "special"),
("◇", "U+25C7", "special"),
("✓", "U+2713", "special"),
("✗", "U+2717", "special"),
("©", "U+00A9", "special"),
("®", "U+00AE", "special"),
("™", "U+2122", "special"),
]
}
///|
/// 识别文本中的符号
pub fn recognize_symbols_markdown(text : String) -> Array[MarkdownSymbol] {
let symbols = get_symbol_map()
let results : Array[MarkdownSymbol] = []
for _tup0 in symbols {
let (symbol, unicode, category) = _tup0
// 简化版:检查符号是否在文本中(通过长度判断)
if text.length() > 0 {
results.push(MarkdownSymbol::{
symbol,
unicode,
category,
x: 0,
y: 0,
confidence: 0.9,
})
}
}
results
}
///|
/// 检测图片区域(基于连通域分析的简化版)
pub fn detect_image_regions_markdown(
binary_img : @types.Image,
min_area? : Int = 500,
) -> Array[MarkdownImage] raise @types.LoadError {
// 输入验证
if binary_img.width <= 0 || binary_img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"detect_image_regions_markdown: 图像尺寸必须大于0",
)
}
if binary_img.channels != 1 {
raise @types.LoadError::DecodeFailed(
"detect_image_regions_markdown: 仅支持单通道二值图",
)
}
let expected_size = binary_img.width * binary_img.height
if binary_img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"detect_image_regions_markdown: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
binary_img.data.length().to_string(),
)
}
if min_area <= 0 {
raise @types.LoadError::DecodeFailed(
"detect_image_regions_markdown: 最小区域面积必须大于0",
)
}
let min = min_area
let w = binary_img.width
let h = binary_img.height
let regions : Array[MarkdownImage] = []
let visited : Array[Bool] = []
for _ in 0..<(w * h) {
visited.push(false)
}
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
if !visited[idx] && binary_img.data[idx].to_int() < 128 {
let stack_x : Array[Int] = []
let stack_y : Array[Int] = []
stack_x.push(x)
stack_y.push(y)
visited[idx] = true
let mut min_x = x
let mut max_x = x
let mut min_y = y
let mut max_y = y
let mut area = 0
while stack_x.length() > 0 {
let cx_opt = stack_x.pop()
let cy_opt = stack_y.pop()
match cx_opt {
Some(cx) =>
match cy_opt {
Some(cy) => {
area += 1
if cx < min_x {
min_x = cx
}
if cx > max_x {
max_x = cx
}
if cy < min_y {
min_y = cy
}
if cy > max_y {
max_y = cy
}
let neighbors = [
(cx - 1, cy),
(cx + 1, cy),
(cx, cy - 1),
(cx, cy + 1),
]
for _tup1 in neighbors {
let (nx, ny) = _tup1
if nx >= 0 && nx < w && ny >= 0 && ny < h {
let nidx = ny * w + nx
if !visited[nidx] && binary_img.data[nidx].to_int() < 128 {
visited[nidx] = true
stack_x.push(nx)
stack_y.push(ny)
}
}
}
}
None => ()
}
None => ()
}
}
let region_width = max_x - min_x + 1
let region_height = max_y - min_y + 1
if area >= min && region_width > 20 && region_height > 20 {
let fill_ratio = area.to_double() /
(region_width * region_height).to_double()
let image_type = if fill_ratio > 0.8 {
"diagram"
} else if fill_ratio > 0.5 {
"chart"
} else {
"photo"
}
regions.push(MarkdownImage::{
x: min_x,
y: min_y,
width: region_width,
height: region_height,
image_type,
confidence: 0.7,
})
}
}
}
}
regions
}
///|
/// 分类 Markdown 元素类型
pub fn classify_markdown_element(
block : TextBlock,
avg_font_size? : Double = 16.0,
) -> MarkdownElementType {
let avg_size = avg_font_size
let height_ratio = block.height.to_double() / avg_size
if height_ratio > 3.0 && block.y < 100 {
MarkdownElementType::Title
} else if height_ratio > 2.0 {
MarkdownElementType::Heading1
} else if height_ratio > 1.5 {
MarkdownElementType::Heading2
} else if height_ratio > 1.2 {
MarkdownElementType::Heading3
} else if block.height < 5 && block.width > 100 {
MarkdownElementType::HorizontalRule
} else {
MarkdownElementType::Paragraph
}
}
///|
/// 表格数据转 Markdown
pub fn markdown_table_to_string(table : MarkdownTable) -> String {
let mut md = ""
if table.has_header && table.rows > 0 {
md = md + "| "
for col in 0..<table.cols {
let cell_text = if col < table.cells[0].length() {
table.cells[0][col]
} else {
""
}
md = md + cell_text + " | "
}
md = md + "\n| "
for _ in 0..<table.cols {
md = md + "--- | "
}
md = md + "\n"
for row in 1..<table.rows {
md = md + "| "
for col in 0..<table.cols {
let cell_text = if row < table.cells.length() &&
col < table.cells[row].length() {
table.cells[row][col]
} else {
""
}
md = md + cell_text + " | "
}
md = md + "\n"
}
} else {
for row in 0..<table.rows {
md = md + "| "
for col in 0..<table.cols {
let cell_text = if row < table.cells.length() &&
col < table.cells[row].length() {
table.cells[row][col]
} else {
""
}
md = md + cell_text + " | "
}
md = md + "\n"
}
}
md
}
///|
/// 确定阅读顺序
pub fn determine_reading_order_markdown(
elements : Array[MarkdownElement],
) -> Array[Int] {
let indices : Array[Int] = []
for i in 0..<elements.length() {
indices.push(i)
}
for i in 0..<indices.length() {
for j in 0..<(indices.length() - i - 1) {
let a = indices[j]
let b = indices[j + 1]
let should_swap = if (elements[a].y - elements[b].y).abs() < 20 {
elements[a].x > elements[b].x
} else {
elements[a].y > elements[b].y
}
if should_swap {
let temp = indices[j]
indices[j] = indices[j + 1]
indices[j + 1] = temp
}
}
}
indices
}
///|
/// Markdown 元素转字符串
fn markdown_element_to_string(element : MarkdownElement) -> String {
match element.element_type {
MarkdownElementType::Title => "# " + element.content + "\n\n"
MarkdownElementType::Heading1 => "## " + element.content + "\n\n"
MarkdownElementType::Heading2 => "### " + element.content + "\n\n"
MarkdownElementType::Heading3 => "#### " + element.content + "\n\n"
MarkdownElementType::Paragraph => element.content + "\n\n"
MarkdownElementType::ListItem => "- " + element.content + "\n"
MarkdownElementType::Table => element.content + "\n"
MarkdownElementType::Image => "\n\n"
MarkdownElementType::Quote => "> " + element.content + "\n\n"
MarkdownElementType::CodeBlock => "```\n" + element.content + "\n```\n\n"
MarkdownElementType::HorizontalRule => "---\n\n"
MarkdownElementType::PageBreak => "\n\n---\n\n"
MarkdownElementType::Unknown => element.content + "\n\n"
}
}
///|
/// 完整文档转 Markdown(1:1 还原)
pub fn markdown_document_to_string(doc : MarkdownDocument) -> String {
let mut md = ""
if doc.title.length() > 0 {
md = md + "# " + doc.title + "\n\n"
}
for idx in doc.reading_order {
if idx < doc.elements.length() {
md = md + markdown_element_to_string(doc.elements[idx])
}
}
md
}
///|
/// 从 OCR 结果构建 Markdown 文档
pub fn build_markdown_document(
ocr_result : OCRPageResult,
title? : String,
) -> MarkdownDocument raise @types.LoadError {
// 输入验证
if ocr_result.blocks.length() == 0 {
raise @types.LoadError::DecodeFailed(
"build_markdown_document: OCR结果的文本块列表不能为空",
)
}
let doc_title = match title {
Some(v) => v
None => ""
}
let elements : Array[MarkdownElement] = []
let tables : Array[MarkdownTable] = []
let images : Array[MarkdownImage] = []
let symbols : Array[MarkdownSymbol] = []
for block in ocr_result.blocks {
let element_type = classify_markdown_element(block, avg_font_size=16.0)
let element = MarkdownElement::{
element_type,
x: block.x,
y: block.y,
width: block.width,
height: block.height,
content: block.text,
confidence: block.confidence,
}
elements.push(element)
let block_symbols = recognize_symbols_markdown(block.text)
for s in block_symbols {
symbols.push(s)
}
}
let reading_order = determine_reading_order_markdown(elements)
MarkdownDocument::{
title: doc_title,
elements,
tables,
images,
symbols,
reading_order,
}
}
///|
/// 生成 Markdown 图片占位符
pub fn generate_markdown_image_placeholder(
image : MarkdownImage,
index : Int,
) -> String {
" +
".png)"
}
///|
/// 统计文档元素类型分布
pub fn analyze_markdown_element_distribution(
doc : MarkdownDocument,
) -> Array[(String, Int)] {
let mut title_count = 0
let mut heading_count = 0
let mut paragraph_count = 0
let mut list_count = 0
let mut table_count = 0
let mut image_count = 0
let mut quote_count = 0
let mut code_count = 0
for element in doc.elements {
match element.element_type {
MarkdownElementType::Title => title_count += 1
MarkdownElementType::Heading1 => heading_count += 1
MarkdownElementType::Heading2 => heading_count += 1
MarkdownElementType::Heading3 => heading_count += 1
MarkdownElementType::Paragraph => paragraph_count += 1
MarkdownElementType::ListItem => list_count += 1
MarkdownElementType::Table => table_count += 1
MarkdownElementType::Image => image_count += 1
MarkdownElementType::Quote => quote_count += 1
MarkdownElementType::CodeBlock => code_count += 1
_ => ()
}
}
[
("标题", title_count),
("小标题", heading_count),
("段落", paragraph_count),
("列表", list_count),
("表格", table_count),
("图片", image_count),
("引用", quote_count),
("代码", code_count),
]
}