Skip to content

Fast, accurate OCR with correctness assessment.

sdk: set paragraph-title depth from the leading numbering only

caowanxia3bba6fc34042
diff --git a/sdk/minicpmvocr/postprocess/markdown.py b/sdk/minicpmvocr/postprocess/markdown.py
index aaad3c2..34983c8 100644
--- a/sdk/minicpmvocr/postprocess/markdown.py
+++ b/sdk/minicpmvocr/postprocess/markdown.py
@@ -289,11 +289,15 @@ def _collapse_soft_newlines(text: str) -> str:
def format_numbered_title(content: str) -> str:
'Paragraph title whose depth follows its numbering: "2" -> ##, "2.1" -> ###, unnumbered -> ##.'
title = content
+ numbering = ""
match = TITLE_RE.match(title)
if match:
- title = match.group(1).strip() + " " + match.group(3).lstrip()
- title = title.rstrip(".")
- level = title.count(".") + 1 if "." in title else 1
+ numbering = match.group(1).strip()
+ title = numbering + " " + match.group(3).lstrip()
+ title = title.rstrip(".").rstrip()
+ # Only the dots of the leading numbering set the depth. PaddleX counts every dot in the title, which pushes
+ # "Vol. 17", "Dr. B. K. B. College" or "2023.01.30" down to #### and below.
+ level = numbering.count(".") + 1
return _collapse_soft_newlines("#%s %s" % ("#" * level, title))
diff --git a/sdk/tests/test_paddle_alignment.py b/sdk/tests/test_paddle_alignment.py
index 3c57e63..ab61aa4 100644
--- a/sdk/tests/test_paddle_alignment.py
+++ b/sdk/tests/test_paddle_alignment.py
@@ -60,6 +60,12 @@ def test_paddlex_provider_hands_paddlex_bgr():
# PaddleX keeps the numbering's trailing dot, and counts it as one more level.
("2.1.3. Setup.", "##### 2.1.3. Setup"),
("(一)总体要求", "## (一) 总体要求"),
+ ("5.", "### 5."),
+ ("3.3. Compatibility. Throughout", "#### 3.3. Compatibility. Throughout"),
+ # Dots outside the leading numbering do not deepen the title.
+ ("Vol. 17: Cumulative Index", "## Vol. 17: Cumulative Index"),
+ ("Diane S. Rosenberg, Mark D. Meyer, et al., Trustees", "## Diane S. Rosenberg, Mark D. Meyer, et al., Trustees"),
+ ("1、关于支持新能源商品汽车铁路运输 2023.01.30", "## 1、 关于支持新能源商品汽车铁路运输 2023.01.30"),
("Related\nWork", "## Related Work"),
],
)